#function calls

1 messages · Page 1 of 1 (latest)

sick surge
#

Im trying to get functions working with the API. This is just a test and rough prototyping currently. It will do one function but not the other one, which is a simple requesting the current time. I get the below responses.

Generated response: As an AI, I’'‘m unable to provide real-time information such as the current time. Please check the device you’''re using for the most accurate time.

I know it dose not know the time but i thought the function would go above that. Any help would be good.
First Prompt

{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "give me the current time"}
],
"temperature": 0.2,
"tools": [{
"type": "function",
"function": {
"name":"getCurrentTime",
"description": "this will get the time"
}
},{
"type": "function",
"function": {
"name":"getRandomWords",
"description": "this will get a list of predefined random words"
}
}
]
}

still cipherBOT
#

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

#

Based on the information provided in the extracts, it seems like you're trying to use the Function Calling feature of OpenAI's API. This feature allows the model to call predefined functions during a conversation. However, the function needs to be defined and passed to the API call.

In your case, you're trying to call two functions: getCurrentTime and getRandomWords. However, the code you provided doesn't seem to include the actual definitions of these functions.

Here's an example of how you might define a function and pass it to the API, based on the information provided in the extracts:

available_functions = {
    "get_current_weather": get_current_weather,
    # Add your functions here
}

messages = [
    {
        "role": "system",
        "content": "You are a helpful assistant."
    },
    {
        "role": "user",
        "content": "give me the current time"
    }
]

if isinstance(functions_calls, list):
    pass
else:
    functions_calls = [functions_calls]

for function_call in functions_calls:
    function_name = function_call.name
    function_to_call = available_functions[function_name]
    function_args = json.loads(function_call.arguments)
    function_response = function_to_call(
        location=function_args.get("location"),
        unit=function_args.get("unit"),
    )
    messages.append(
        {
            "role": "function",
            "name": function_name,
            "content": function_response,
        }
    )

second_response = client.chat.completions.create(
    model=os.environ["model"],
    messages=messages
)

(Source: Github Issue (https://github.com/openai/openai-python/issues/926))

Please note that the actual implementation of the getCurrentTime and getRandomWords functions is not provided in the extracts, so you'll need to implement these yourself.

#

Also, please ensure that you're using a model and API version that supports function calling. As mentioned in one of the extracts, function calling is available in the 2023-07-01-preview API version and works with version 0613 of gpt-35-turbo, gpt-35-turbo-16k, gpt-4, and gpt-4-32k.
(Source: Github Issue (https://github.com/openai/openai-python/issues/517))

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

#

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

sick surge
#

@still cipher ChatGPT first reponds

{
"id": "chatcmpl-8aZ5ED4XAvDa7lYXq4OSBgz7oEa6f",
"object": "chat.completion",
"created": 1703725360,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_PQJTIICE5PhM5NHMchhYiY68",
"type": "function",
"function": {
"name": "getCurrentTime",
"arguments": "{}"
}
}
]
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 68,
"completion_tokens": 7,
"total_tokens": 75
},
"system_fingerprint": null
}

Responds to command

{
"model": "gpt-4",
"temperature": 0.2,
"messages": [
{"role": "user", "content": "give me the current time"},
{"role": "function", "name": "getCurrentTime","content":"Current time: Thu 28 Dec 2023 12:02:41 AEDT" }
]
}

Final gpt reponds

{
"id": "chatcmpl-8aZ5FX1kpBFldviU3CvTTN2t7zV6V",
"object": "chat.completion",
"created": 1703725361,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "As an AI, I'''m unable to provide real-time information such as the current time. Please check the device you'''re using for the most accurate time."
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 36,
"completion_tokens": 31,
"total_tokens": 67
},
"system_fingerprint": null
}

still cipherBOT
#

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

#

Based on the information provided in the extracts, it seems like you're trying to use the Function Calling feature of OpenAI's API. This feature allows the model to call predefined functions during a conversation. However, the function needs to be defined and passed to the API call.

In your case, you're trying to call a function: getCurrentTime. However, the final response from the model seems to be ignoring the function call and providing a default response.

Here's an example of how you might define a function and pass it to the API, based on the information provided in the extracts:

available_functions = {{
    "get_current_weather": get_current_weather,
    # Add your functions here
}}

messages = [
    {{
        "role": "system",
        "content": "You are a helpful assistant."
    }},
    {{
        "role": "user",
        "content": "give me the current time"
    }}
]

if isinstance(functions_calls, list):
    pass
else:
    functions_calls = [functions_calls]

for function_call in functions_calls:
    function_name = function_call.name
    function_to_call = available_functions[function_name]
    function_args = json.loads(function_call.arguments)
    function_response = function_to_call(
        location=function_args.get("location"),
        unit=function_args.get("unit"),
    )
    messages.append(
        {{
            "role": "function",
            "name": function_name,
            "content": function_response,
        }}
    )

second_response = client.chat.completions.create(
    model=os.environ["model"],
    messages=messages
)

(Source: Github Issue (https://github.com/openai/openai-python/issues/926))

Please note that the actual implementation of the getCurrentTime function is not provided in the extracts, so you'll need to implement this yourself.

#

Also, please ensure that you're using a model and API version that supports function calling. As mentioned in one of the extracts, function calling is available in the 2023-07-01-preview API version and works with version 0613 of gpt-35-turbo, gpt-35-turbo-16k, gpt-4, and gpt-4-32k.
(Source: Github Issue (https://github.com/openai/openai-python/issues/517))

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