How to best provide instructions for formatting chat responses to function call outputs?

Hi! I have a function that returns a JSON object. I want to format this object in the resulting chat message. How do I best provide these instructions?

For example:

I have a function called articulate_values_card. I want to call it when a user in the chat has articulated something important to them. This function returns an object with a title and a short description of the value.

Now, I want to show this object in a particular way in the chat. For instance, I want the assistant to refer to the title of the card, and ask the user if they like it. How do I best provide this instruction?

So far I have tried:

  • Adding instructions in the system prompt for formatting results from articulate_values_card
  • Adding instructions in the description of the function around how outputs should be interpreted.
  • Returning an instruction string instead of the actual response from the function in the “function” message.

The latter seems to work best, but it feels like an ugly way of doing it.

To clarify, what I mean is that if my function is returning:
'{"title":"Embodied Justice","short_description":"When I sense into what feels just in my body, life feels authentic and real"}'

I actually send this string as the function result for the chat completion:

<A card (${card.title}) was articulated and shown to the user. The preview of the card is shown in the UI, no need to repeat it here. The user can now choose to submit the card.>

const res = await openai.createChatCompletion({
    model: "gpt-3.5-turbo-0613",
    messages: [
      ...messages,
      {
        role: "assistant",
        content: null,
        function_call: {
          name: "articulate_values_card",
          arguments: args
        },
      },
      {
        role: "function",
        name: "articulate_values_card",
        content: `<A card (${card.title}) was articulated and shown to the user. The preview of the card is shown in the UI, no need to repeat it here. The user can now choose to submit the card.>`
      },
    ],
    temperature: 0.7,
    functions,
    stream: true,
  })

Is there a best-practice for how to format chat responses following the output of a function call?

1 Like

That’s not what the AI actually writes to call a function. You are using the API response, not the AI language.

user: what is the time in argentina
assistant: world_time(“argentina”)
function: Argentina: 15:33:38
assistant: The time there is 3:33 pm.
user: can you calculate the time difference between there and japan on Jan 3?
assistant: “”"Sure, I can answer exactly with python, let’s see:

python(“import time\n\argentina_time = …”“”
function: Times: Argentina to Japan on 03-Jan-2023: +11
assistant: Looks like Japan is 11 hours ahead.
user: thanks for showing me how python functions look in roles and history.

The function role and assistant function calling role can be removed more quickly from chat history as they don’t have much value after all the function looping is done.