one-api 使用部署服务,但是在访问OPENAI过程中遇到如下问题,

62o28rlo  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(107)

例行检查

问题描述

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20231206054444375592893PDWPRFyf)", 'type': 'invalid_request_error', 'param': 'messages.[1].content', 'code': None}}

代码,这个码是官方案例代码。

from openai import OpenAI
import json
api_base = "xx"
api_key = "xx"
model_name = "gpt-3.5-turbo-1106"
# gpt-3.5-turbo、 gpt-3.5-turbo-16k、gpt-4、gpt-4-32k、gpt-3.5-turbo-1106、gpt-4-1106-preview
client = OpenAI(
    api_key=api_key,
    base_url=api_base
)

# 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?"}]
    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

        print(messages)
        second_response = client.chat.completions.create(
            model=model_name,
            messages=messages,
        )
        return second_response
print(run_conversation())

复现步骤

上面代码。

预期结果
相关截图

如果没有的话,请删除此节。

kupeojn6

kupeojn61#

我也遇到了这个问题。OpenAI 在 文档 中指出,contenttool_callsfunction_call 存在时是可选的,然而 one-api 似乎要求这一字段始终存在。当然考虑到我使用了 Azure OpenAI 作为后端,这也可能是 Azure 的问题。期待作者的解答。

相关问题