Skip to content

Why, my msg can be used, but the tools case cannot be passed #863

@wellcasa

Description

@wellcasa

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

(openai) /mnt/workspace/project/Eagent> python 1.py
Traceback (most recent call last):
File "/mnt/workspace/project/Eagent/1.py", line 94, in
print(run_conversation())
File "/mnt/workspace/project/Eagent/1.py", line 55, in run_conversation
response = client.chat.completions.create(
File "/home/pai/lib/python3.9/site-packages/openai/_utils/_utils.py", line 299, in wrapper
return func(*args, **kwargs)
File "/home/pai/lib/python3.9/site-packages/openai/resources/chat/completions.py", line 598, in create
return self._post(
File "/home/pai/lib/python3.9/site-packages/openai/_base_client.py", line 1063, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "/home/pai/lib/python3.9/site-packages/openai/_base_client.py", line 842, in request
return self._request(
File "/home/pai/lib/python3.9/site-packages/openai/_base_client.py", line 885, in _request
raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'message': 'Unrecognized request argument supplied: tools (request id: 202311220623247350366364AkoGmo5)', 'type': 'invalid_request_error', 'param': '', 'code': None}}

To Reproduce

Fill in ak and run the code

Code snippets

from openai import OpenAI
from openai import api_version
import json
import os 
api_base = "https://vp01.glyph.cyou:8443/v1"
api_key = "xxxx"
model_name = "gpt-3.5-turbo-1106"
# model_name = "gpt-4"
# gpt-3.5-turbo、 gpt-3.5-turbo-16k、gpt-4、gpt-4-32k、gpt-3.5-turbo-1106、gpt-4-1106-preview

os.environ["OPENAI_API_BASE"] = api_base
os.environ["OPENAI_API_KEY"] = api_key
client = OpenAI(
    api_key=api_key,
    base_url=api_base
)
# client = OpenAI()

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"})
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})

def run_conversation():
    # Step 1: send the conversation and available functions to the model
    messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
    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"],
                },
            },
        }
    ]
    response = client.chat.completions.create(
        model=model_name,
        messages=messages,
        tools=tools,
        # tool_choice="auto",  # auto is default, but we'll be explicit
    )
    response_message = response.choices[0].message
    print(response_message.model_dump_json(indent=4))
    tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
    if tool_calls:
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_current_weather": get_current_weather,
        }  # only one function in this example, but you can have multiple
        messages.append(response_message)  # extend conversation with assistant's reply
        # Step 4: send the info for each function call and function response to the model
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(
                location=function_args.get("location"),
                unit=function_args.get("unit"),
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        second_response = client.chat.completions.create(
            model="gpt-3.5-turbo-1106",
            messages=messages,
        )  # get a new response from the model where it can see the function response
        return second_response
print(run_conversation())

OS

macos

Python version

v3.10

Library version

openai v1.34.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions