#OpenAI Functions calling in python

1 messages · Page 1 of 1 (latest)

thin bronze
#

Give me an example code for calling the functions endpoint in python

thick zephyrBOT
#

Hi @thin bronze. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, here is an example of how to call functions with the OpenAI Python API, based on the information extracted from the GitHub issues:

import openai

def run_conversation(user_input): 
    messages = [{"role": "user", "content": user_input}]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_online_data",
                "description": "Get real-time data from the web",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "input": {
                            "type": "string",
                            "description": "What is the latest stock price of Apple?",
                        },
                    },
                    "required": ["input"],
                },
            },
        }
    ]

    response = openai.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=messages,
        tools=tools,
        tool_choice="auto",
    )
    response_message = response.choices[0].message
    total_tokens = response.usage.total_tokens

    if response.choices[0].message.tool_calls is None:
        return response_message.content + " Total tokens used: " + str(total_tokens)

In this example, a function named get_online_data is defined and called within the run_conversation function. The get_online_data function is expected to retrieve real-time data from the web based on the user's input. The openai.chat.completions.create method is used to generate a response from the model, and the response is processed to extract the message content and the total number of tokens used.

Please note that you need to replace "gpt-3.5-turbo-1106" with the actual model you are using, and you need to define the get_online_data function according to your requirements.

#

@thin bronze kapa.ai is still learning and improving, please let us know how it did by reacting below

thin bronze
#

How do I make sure the model always chooses a function or tool? @thick zephyr

thick zephyrBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To ensure that the model always chooses a function or tool, you can specify the tool_choice parameter in your API call. This parameter controls which function or tool is called by the model.

Here's an example of how to do this:

response = openai.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    tools=tools,
    tool_choice={
        "type": "function",
        "function": {
            "name": "my_function"
        }
    },
)

In this example, the tool_choice parameter is set to a specific function named my_function. This forces the model to call that function.

Please note that you need to replace "gpt-3.5-turbo-1106" with the actual model you are using, and "my_function" with the actual function you want the model to call.

This information is based on the extract from this GitHub issue (https://github.com/openai/openai-node/issues/515).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: