byzer-llm icon indicating copy to clipboard operation
byzer-llm copied to clipboard

OpenAI-Compatible RESTful API 并不支持function_call吗?我用https://platform.openai.com/docs/guides/function-calling的例子测试了下,但发现始终返回的是function_call=None, tool_calls=None

Open zhangjiekui opened this issue 11 months ago • 1 comments

# https://platform.openai.com/docs/guides/function-calling
from openai import OpenAI
import json

client = OpenAI(
    base_url="http://127.0.0.1:8000/v1",
    api_key="simple"
)

# 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": unit})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})

def run_conversation():
    # Step 1: send the conversation and available functions to the model
    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"],
                },
            },
        }
    ]
   
    messages = [{
                "role": "system", 
                "content": f"你是一个人工作智能助手,帮助回答用户的问题,必要时可以调用工具,回答用户问题。下面是你可以调用的工具:\n{tools}\n  请明确返回是否需要调用工具,并返回调用该工具所需的参数。"
            },
            {"role": "user", 
             "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}
            ]
            
    
    response = client.chat.completions.create(
        model="chat",
        messages=messages,
        tools=tools,
        tool_choice="auto",  # auto is default, but we'll be explicit
    )
    response_message = response.choices[0].message
    print(response_message)
    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="chat",
            messages=messages,
        )  # get a new response from the model where it can see the function response
        return second_response
print(run_conversation())

打印返回结果如下: ChatCompletionMessage(content="Yes, I need to call the tool to get the current weather for each location. Here are the parameters for each city:\n\n1. San Francisco, CA\n2. Tokyo, Japan\n3. Paris, France\n\nUnit will be assumed as 'celsius' for all cities unless specified otherwise.", role='assistant', function_call=None, tool_calls=None)

zhangjiekui avatar Mar 15 '24 07:03 zhangjiekui