Getting assistants to call functions

For fun, I am building a little Choose-Your-Own-Adventure Assistant.

I want it to call a function whenever the story enters a new location. I got it to a point where it is pretty good at dividing the story into Chapters with locations and describing new locations.

But, instead of going into “requires_action” state it simply puts the function call inside the output. Like it create a Markdown codeblock with the correct function call, instead of going into the requires_action state.

Wondering if someone already figured out a prompt engineering method to make sure the assistant correctly understands functions and when to call them?

Havn’t encountered this problem myself. However I do wish there was a feature simular to the ChatCompletion parameter “tool_choice” (used to be “function_call”) for the Assistant API.
I usally include a short description on when to call what function and reffer to the function names in the tools parameter. I could imagine that if your instructions include something like “only output valid json” or the json schemas again that it could lead to something like this. May I ask what your prompt looks like now? :grinning:

This was most prominent when my prompt was a bit simpler, I was at:

You are an interactive fiction adventure. You let users experience various stories about adventure, love, horror, missfortune, despair, luck, victory and more.

Initially the user will tell you what kind of story they want to experience. You then adjust your tone to fit the stories theme and start by asking them further details about their desired experience.

You tell the story from the users point of view as a first person perspective.

Every once in a while users will need to make choices in the story. Like a "pick your own adventure" book. But you are better than that, instead of the user choosing one a few, predefined options, they create their own one. You just promt their thinking. 

Your stories always move between different locations. Describe the location to the user when it changes. 

Every time the story moves to a new location, you update the UI for the user via newLocation.

With a function being defined like such:

{
      type: 'function',
      function: {
        name: 'newLocation',
        parameters: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              description:
                'The new locations name. This should be short and descriptive.',
            },
            description: {
              type: 'string',
              description:
                'A detailed description of the new location. Tells us what we can see here.',
            },
          },
          required: ['name', 'description'],
        },
        description:
          'Create a new location for later reference and to update the UI with it.',
      },
    },

Within the assistants output i got things like:

newLocation({
  name: "Village Square",
  description: "The heart of your village, usually bustling with the laughter of children and the chattering of townsfolk, now holds a solemn stillness. Torches flicker against the night, casting shadows that dance upon the cobblestone pathways. Stalls of wares and tents of colorful fabrics from the day's festivities are being taken down, but there is an unmistakable tension in the air."
});

Which is amazingly prcise in how it should call the function.

But this is in the middle of the assistants reply :sweat_smile: instead of being a requires_action state :thinking:

Okay, one weird finding.

Switching from model: 'gpt-4-1106-preview' to model: 'gpt-3.5-turbo-1106' actually helps.

With the switch to GPT-3.5 my function does get called. Super weird, I would have expected the opposite and didn’t even try 3 before. Assuming that it couldn’t work, if “not even 4” worked :thinking:

1 Like

Yeah that is weird. I would also think it should work as is. However I would try rephrasing this part:

Guessing thats what makes it send the output directly to the user instead of calling the function.

I would also try changing the name to something like “createNewLocation” instead as (just speculating) the model might interept the current version as an object format rather than a function.

Also the cookbooks include a lot of examples where the function description starts out with something like: “Call this function when X…” so maybe try adding something like “Always call this function when a new location is entered.”.

Including a disclaimer of some sort in the instructions couldn’t hurt either. For example:
“Note: When the user enters a new location you should always call the function ‘createNewLocation’. Calling the ‘createNewLocation’ function is an essential step to ensure that the new location is saved and can be used later. After calling ‘createNewLocation’ describe the new location to the user”

Hope you get it working :grinning:

Great input @Reversehobo :+1:

Updating the function description alone didn’t change much but the note part really helped. Now 4 is also consistent. Although I feel like 3.5 fits my use-case better :slight_smile:

1 Like

I updated the description to also include “You should always call the function “X” at the beginning of the run” which helped my use case.