JohannLai / openai-function-calling-tools

🛠 openai function calling tools for JS/TS
MIT License
270 stars 22 forks source link

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

Open wizd opened 12 months ago

wizd commented 12 months ago

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 commented 12 months ago

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

wizd commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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