How to do few shots completions with Tool Calls?

Hey, was wondering how you would add your own tool calling messages (as if they were from the model itself) to the messages now that every tool call has an id?

Here is an example of what I mean using deprecated function calling instead:

from openai import OpenAI

client = OpenAI()
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["location"],
        },
    }
]
user_msg = {
    "role": "user",
    "content": "What is the weather like in Stockholm?",
}
assistant_msg = {
    "role": "assistant",
    "function_call": {
        "arguments": """{"location": "Stockholm", "unit": "celsius"}""",
        "name": "get_current_weather",
    },
}
function_msg = {
    "role": "function",
    "name": "get_current_weather",
    "content": """{"weather": "sunny", "temperature": 21, "unit": "celsius"}""",
}
assistant_msg2 = {
    "role": "assistant",
    "content": "It is sunny and 21 degrees celsius in Stockholm.",
}
user_msg2 = {
    "role": "user",
    "content": "What is the weather like in Oslo?",
}

messages = [user_msg, assistant_msg, function_msg, assistant_msg2, user_msg2]

response = client.chat.completions.create(
    model="gpt-4-0613",
    messages=messages,
    functions=functions,
)

print(response.choices[0].message)

How would you achieve the same using tools instead? :thinking:

Figured it out. Doesn’t seem that the tool_call_id needs to follow any specific pattern. Here is the same example using tools if anyone has the same question:

from openai import OpenAI

client = OpenAI()
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        },
    },
]
user_msg = {
    "role": "user",
    "content": "What is the weather like in Stockholm?",
}
assistant_msg = {
    "role": "assistant",
    "content": None,
    "tool_calls": [
        {
            "id": "call_abc123",
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "arguments": '{"location": "Stockholm", "unit": "celsius"}',
            },
        }
    ],
}
tool_msg = {
    "role": "tool",
    "tool_call_id": "call_abc123",
    "content": """{"weather": "sunny", "temperature": 21, "unit": "celsius"}""",
}
assistant_msg2 = {
    "role": "assistant",
    "content": "It is sunny and 21 degrees celsius in Stockholm.",
}
user_msg2 = {
    "role": "user",
    "content": "What is the weather like in Oslo?",
}

messages = [user_msg, assistant_msg, tool_msg, assistant_msg2, user_msg2]

messages = [user_msg, assistant_msg, tool_msg]
response = client.chat.completions.create(
    model="gpt-4-1106-preview",
    messages=messages,
    tools=tools,
)

print(response.choices[0].message)

It appears that tool_call_id is filtered/replaced by OpenAI somewhere in their pipeline. I noticed that the prompt_tokens doesn’t change when I increased the length of tool_call_id.

Also, changing tool_call_id still gives reproducible outputs when you set a seed value. :slightly_smiling_face: