# Few-shot and function calling

**URL:** <https://community.openai.com/t/few-shot-and-function-calling/265908>\
**Category:** API\
**Created:** [June 15, 2023, 3:37pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908 "2023-06-15T15:37:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 15, 2023, 3:37pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/1 "2023-06-15T15:37:00Z")

</div>

Hi,  
Is there any examples or guides on how should I do few-shot when using function calling?

I’m using function calling to extract structured data from the prompt but for some examples the 3.5 don’t give very good answers. I would like to try to do a bit of few-shot but not sure what examples I should give it because I’m expecting a function\_call.

What content should I give as an example? An example of the full function structure that we give as parameter to `openai.ChatCompletion.create`?

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [June 15, 2023, 6:13pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/2 "2023-06-15T18:13:02Z")

</div>

> **[ChatGPT Prompt Engineering for Developers](https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/)**
>
> What you’ll learn in this course In ChatGPT Prompt Engineering for Developers, you will learn how to use a large language model (LLM) to quickly build new and powerful applications.  Using the OpenAI API, you’ll...

Great free courses on prompting for Developers.

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 15, 2023, 6:16pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/3 "2023-06-15T18:16:40Z")

</div>

May you provide an example?  
This could help better understand your use case.

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 15, 2023, 11:15pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/4 "2023-06-15T23:15:28Z")

</div>

This [example](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb) from OpenAI.  
How would you do one-shot with the first example?

Like this?

Role: User  
Content: what is the weather going to be like in Glasgow, Scotland over the next 5 days

Role: Assistant  
Content: None  
function\_call:{‘name’: ‘get\_n\_day\_weather\_forecast’,  
‘arguments’: ‘{\n “location”: “Glasgow, Scotland”,\n “format”: “celsius”,\n “num\_days”: 5\n}’}},  
‘finish\_reason’: ‘function\_call’}

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 16, 2023, 1:02am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/5 "2023-06-16T01:02:23Z")

</div>

The example in the notebook is a one-shot example.  
Or do you mean to omit the ‘system’ role message at all?

To use the function call, you need only a good description of the function and the optional parameters.

In this example, the user asks a question “What is the weather in location XYZ for the next n-th days”. This question should be associable/related to the description of the function. If the user requested message fits with the description of the function call(s), then GPT formulates the function request and send it as an response to your side (backend).  
You are then responsible to read out the name, arguments and pass back the result as a function-role to GPT. GPT formulates a message back to the user (frontend).  
This is a bidirectional dialogue between the user and you. GPT is the middleman which acts as an API wrapper from NLP → “Your API” → NLP.

Or you can say, GPT translate natural language into a function and the result is translated back into natural language.

You can also force GPT to use a specified function when using the “function\_call” key in the request body. It will use the function call even if the user requested content will not fit.  
You mostly get a fabricated answer.  
It is important to set the temperature for a function call very low to work properly.

But you do not need to use this feature as a function call. You can use it also to extract content from the user message into a well-defined Json blob. Important is, that the descriptions fits with, what the user wants or what you want to have from the user. To use few-shots (in the sense of what I understand from you) is to give one or two examples in the description of the function (what its use-case is for) and the possible parameters. This can be anything, from strings, numbers, enums etc (except arrays).

Is this, what you mean?

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 16, 2023, 8:18am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/6 "2023-06-16T08:18:30Z")

</div>

Ok, I see. So, let me show a very simple example.

Thanks for the explanation. What I mean is this small example, slightly modified from the OpenAI first example.

```auto
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use.",
                },
            },
            "required": ["location", "format"],
        },
    }
]

messages = []
messages.append({"role": "system", "content":"Don't make assumptions about what values to plug into functions. "})
messages.append({"role": "user", "content": "What's the weather like today in Lisbon"})
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=messages,
    temperature=0.0,
    functions = functions,
    function_call="auto",
    stream = False
)
print(response)

```

What would I need to do to write a one or two examples? From what your answer, I should give write explicitly in the description? Like this?

```auto
functions = [
    {
        "name": "get_current_weather",
        "description": """Get the current weather.

Role: User
Content: How hot is today in London?        

Role: Assistant
Content: {'city':'London','format':'fahrenheit'}
""",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. ",
                },
            },
            "required": ["location", "format"],
        },
    }
]

messages = []
messages.append({"role": "system", "content":"Don't make assumptions about what values to plug into functions. "})
messages.append({"role": "user", "content": "What's the weather like today in Lisbon"})
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=messages,
    temperature=0.0,
    functions = functions,
    function_call="auto",
    stream = False
)
print(response)

```

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 16, 2023, 8:31am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/7 "2023-06-16T08:31:56Z")

</div>

Oh, no. You don’t need give examples in the description itself. You can use the description like it is used in the OpenAI example. This is enough to infer the function to be called.  
So like in the example to ask the weather in Lisbon should be enough for GPT to use the `get_current_weather` function.

Btw. I like Portugal. Stayed there about 1 1/2 years in the Algarve as a Digital Nomad during the lockdowns. Very friendly and open hearted people. Nice culture too.

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 16, 2023, 8:48am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/8 "2023-06-16T08:48:29Z")

</div>

Thanks! Yes, always nice weather in Algarve ;).  
Ok, but my point is what if I want to do few-shot for function callings?

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 16, 2023, 9:05am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/9 "2023-06-16T09:05:19Z")

</div>

So you want to call several functions at once in only one request?

I think this is not possible with the chat completion.

---

<div class="post-metadata">

**Author:** ![cbenerrazam2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/cbenerrazam2/32/34172_2.png) [@cbenerrazam2](https://community.openai.com/u/cbenerrazam2)\
**Post date:** [June 16, 2023, 9:22am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/10 "2023-06-16T09:22:29Z")

</div>

Have it understand the way it should answer with throughout multiple examples.

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 16, 2023, 11:04am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/11 "2023-06-16T11:04:57Z")

</div>

I don’t want to call several functions. I want to do a simple in-context few-shot learning when using function calling feature.

The example I gave. Imagine you want it to do some few-shots so that it return by default “fahrenheit” unit.

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 16, 2023, 11:20am UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/12 "2023-06-16T11:20:17Z")

</div>

Ah, I think, I understand it now. So GPT should decides based on the location what to choose “fahrenheit” or “celcius” without the user needs to explicit tell it?

You can do this in the description of the format property.  
In this example the description can be changed into “The temperature unit to be used based on the location, e.g San Francisco (fahrenheit), Lisbon (celsius)”

This is a few-shot example the GPT might ensure the current format based on the location of the request. If it fails, you can add more examples in the description.  
This should work.

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [June 16, 2023, 2:23pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/13 "2023-06-16T14:23:06Z")

</div>

I’m working in a more complex problem that I think is not adaptable for the simple example. Do you know if I can find somewhere how they fine-tuned gpt-3.5 for the function\_call feature? By knowing the sintaxe they gave the LLM, it would be easier to implement the few-shot for any case.

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 16, 2023, 4:23pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/14 "2023-06-16T16:23:50Z")

</div>

How they fine-tuned their model to recognize function calling is nowhere documented. Maybe you chunk you task in smaller pieces?

---

<div class="post-metadata">

**Author:** ![lucas.godfrey1000](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/lucas.godfrey1000/32/17292_2.png) [@lucas.godfrey1000](https://community.openai.com/u/lucas.godfrey1000)\
**Post date:** [July 10, 2023, 1:13pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/15 "2023-07-10T13:13:16Z")

</div>

The thing to understand here is that function calling introduced a new role for the chat prompt messages (“role”: “function”). To use few-shot examples with chat model prompts you provide a series of alternating (possibly ‘fake’) messages that show how the `assistant` did / should respond to a given `user` input. With function calling the principle is the same but rather than providing a series of alternating `user`-`assistant` example messages, you provide alternating `user`-`function` messages.

e.g.

```auto
schema = {
    "type": "object",
    "properties": {
        "object_type": {"type": "string"},
        "geometry": {
            "type": "array",
            "items": {
                "type": "number"
            }
        }
    },
    "required": ["object_type", "geometry"]
}

example_response_1 = "{\"object_type\": \"point\", \"geometry\": [2.3, 1.0]}\}"
example_response_2 = "{\"object_type\": \"line\", \"geometry\": [[1.0, 2.0], [3.0, 4.0]]\}"

few_shot_function_calling_example = openai.ChatCompletion.create(
    model = "gpt-3.5-turbo-0613",
        messages = [
            {"role": "system", "content": "You are a system for returning geometric objects in JSON."},
            {"role": "user", "content": "give me a point"},
            {"role": "function", "name": "example_func", "content": example_response_1,},
            {"role": "user", "content": "give me a line"},
            {"role": "function", "name": "example_func", "content": example_response_2,},
            {"role": "user", "content": "give me a polygon"}
        ],
    functions=[{"name": "example_func", "parameters": schema}],
    function_call={"name": "example_func"},
    temperature=0
)

print(few_shot_function_calling_example.choices[0].message)

```

```auto
{
  "content": null,
  "function_call": {
    "arguments": "{\"object_type\": \"polygon\", \"geometry\": [[0, 0], [0, 5], [5, 5], [5, 0]]}",
    "name": "example_func"
  },
  "role": "assistant"
}

```

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [July 10, 2023, 2:08pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/16 "2023-07-10T14:08:12Z")

</div>

Very nice! This is exactly what I was looking for. Didn’t realised they added a new role.

---

<div class="post-metadata">

**Author:** ![miguelwon](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/miguelwon/32/448293_2.png) [@miguelwon](https://community.openai.com/u/miguelwon)\
**Post date:** [July 10, 2023, 2:09pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/17 "2023-07-10T14:09:45Z")

</div>

Just noticed that it predicted correctly the function arguments but with “assistant” role. Shouldn’t be “function” role?

---

<div class="post-metadata">

**Author:** ![lucas.godfrey1000](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/lucas.godfrey1000/32/17292_2.png) [@lucas.godfrey1000](https://community.openai.com/u/lucas.godfrey1000)\
**Post date:** [July 10, 2023, 3:12pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/18 "2023-07-10T15:12:53Z")

</div>

I think it is because it is the `assistant` that responds, even if the response is a `function_call` response as opposed to a `content` response - i.e. it just follows the chat completion response format explained here: [OpenAI Platform](https://platform.openai.com/docs/guides/gpt/chat-completions-api).

---

<div class="post-metadata">

**Author:** ![brettpthomas](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/brettpthomas/32/166509_2.png) [@brettpthomas](https://community.openai.com/u/brettpthomas)\
**Post date:** [July 10, 2023, 6:12pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/19 "2023-07-10T18:12:05Z")

</div>

Is there any difference between adding the examples as messages (as you did here) and putting them inline in the system message?

Naively, feels like having 10 fake user messages could bias later responses in the chat.

---

<div class="post-metadata">

**Author:** ![lucas.godfrey1000](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/lucas.godfrey1000/32/17292_2.png) [@lucas.godfrey1000](https://community.openai.com/u/lucas.godfrey1000)\
**Post date:** [July 10, 2023, 7:26pm UTC](https://community.openai.com/t/few-shot-and-function-calling/265908/20 "2023-07-10T19:26:16Z")

</div>

I don’t know. Something to experiment with maybe.

[Next page](https://community.openai.com/t/few-shot-and-function-calling/265908.md?page=2)
