openai-function-calling-tools icon indicating copy to clipboard operation
openai-function-calling-tools copied to clipboard

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

Open wizd opened this issue 2 years ago • 4 comments

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?

wizd avatar Jul 12 '23 07:07 wizd

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

JohannLai avatar Jul 12 '23 09:07 JohannLai

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 avatar Jul 15 '23 12:07 wizd

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;
    }

wizd avatar Jul 16 '23 06:07 wizd

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

JohannLai avatar Jul 17 '23 02:07 JohannLai