Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few-shot and function calling - API - OpenAI Developer Forum #129

Open
1 task
irthomasthomas opened this issue Nov 28, 2023 · 0 comments
Open
1 task

Few-shot and function calling - API - OpenAI Developer Forum #129

irthomasthomas opened this issue Nov 28, 2023 · 0 comments
Labels
AI-Agents Autonomous AI agents using LLMs in-context-learning Examples of few-shot prompts for in-context learning. llm Large Language Models llm-function-calling Function Calling with Large Language Models openai OpenAI APIs, LLMs, Recipes and Evals prompt Collection of llm prompts and notes

Comments

@irthomasthomas
Copy link
Owner

irthomasthomas commented Nov 28, 2023

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.

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)

{
  "content": null,
  "function_call": {
    "arguments": "{\"object_type\": \"polygon\", \"geometry\": [[0, 0], [0, 5], [5, 5], [5, 0]]}",
    "name": "example_func"
  },
  "role": "assistant"
}
@irthomasthomas irthomasthomas added inbox-url unclassified Choose this if none of the other labels (bar New Label) fit the content. llm-function-calling Function Calling with Large Language Models llm Large Language Models prompt Collection of llm prompts and notes AI-Agents Autonomous AI agents using LLMs openai OpenAI APIs, LLMs, Recipes and Evals in-context-learning Examples of few-shot prompts for in-context learning. labels Nov 28, 2023
@irthomasthomas irthomasthomas removed the unclassified Choose this if none of the other labels (bar New Label) fit the content. label Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AI-Agents Autonomous AI agents using LLMs in-context-learning Examples of few-shot prompts for in-context learning. llm Large Language Models llm-function-calling Function Calling with Large Language Models openai OpenAI APIs, LLMs, Recipes and Evals prompt Collection of llm prompts and notes
Projects
None yet
Development

No branches or pull requests

1 participant