Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you pass a context object to a function call? #8

Open
wizd opened this issue Jul 12, 2023 · 4 comments
Open

How do you pass a context object to a function call? #8

wizd opened this issue Jul 12, 2023 · 4 comments

Comments

@wizd
Copy link

wizd commented Jul 12, 2023

for example, a function to store contact info:

const saveContact = (name, phoneNumber) => {...}

in the runtime, the call should be:

const saveContact = (sessionCtx, name, phoneNumber) => { ... }

I think OpenAI don't need to know about session context?

@JohannLai
Copy link
Owner

You're right, it's true that this part of the design is missing, any suggestions please?

@wizd
Copy link
Author

wizd commented Jul 15, 2023

Use a creator function with parameters:

function createContactSaver(veid: string, ssoid: string) {
  const paramsSchema = z.object({
    party: z.string(),
    name: z.string().optional(),
    phoneNumber: z.string()
  })
  const name = 'saveContact';
  const description = "Useful for store contact info. if you got a contact, you can use this tool to remember it. you will get 'success' if input data is OK. otherwise there will ba an error message.";

  const execute = async ({ party, name, phoneNumber }: z.infer<typeof paramsSchema>) => {
    const result = await executeApp(
      veid,
      ssoid,
      async (app, _) => {
        return await app.callMethod("saveContact", [party, name, phoneNumber]);
      },
      () => {
        // Do nothing for sendmsg in this case
      }
    );

    return result as string;
  };

  return new Tool<typeof paramsSchema, z.ZodType<Promise<string>, any>>(paramsSchema, name, description, execute).tool;
}

@wizd
Copy link
Author

wizd commented Jul 16, 2023

Sorry but in fact we can use free context by closure provided by javascript.

    createContactSaver = () => {
        const paramsSchema = z.object({
            contactName: z.string(),
            phoneNumber: z.string().optional(),
            party: z.string().optional(),
            catalog: z.nativeEnum(ContactCatalog).default(ContactCatalog.Person),
        });

        const name = 'saveContact';
        const description = "Useful for store contact info. if you got a contact, you can use this tool to remember it. you will get 'success' if input data is OK. otherwise there will ba an error message.";

        const execute = async ({ contactName, phoneNumber, party, catalog }: z.infer<typeof paramsSchema>) => {
            const result = await this.callMethod(name, [contactName, phoneNumber, party, catalog]);
            return result;
        };

        return new Tool<typeof paramsSchema, z.ZodType<Promise<string>, any>>(paramsSchema, name, description, execute).tool;
    }

@JohannLai
Copy link
Owner

You're really great! I feel like this could be merged onto the main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants