我正在尝试使用openai的函数调用。但是,它会引发错误。代码如下:
import openai
def fetch_weather_with_openai(location, unit='celsius'):
"""
Fetch weather using OpenAI's ChatCompletion.create with a simulated function call.
Args:
location (str): The location for which weather information is requested.
unit (str): The unit of temperature (celsius or fahrenheit).
Returns:
str: The generated response simulating weather information.
"""
function_payload = {
"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"]
}
}
}
}
# Define the input message
messages = [
{"role": "user", "content": f"What's the weather like in {location}?"}
]
response = openai.ChatCompletion.create(
model=llm_config['model_name'],
deployment_id=llm_config['deployment_name'],
messages=messages,
tools=[function_payload]
)
# Extract and return the response
return response.choices[0].message['content']
# Example usage
response = fetch_weather_with_openai("Paris, France")
print(response)
错误如下:
File "c:\metatool-tool-desc-gen_reasoning_func_call.py", line 74, in <module>
response = fetch_weather_with_openai("Paris, France")
File "c\:metatool-tool-desc-gen_reasoning_func_call.py", line 63, in fetch_weather_with_openai
response = openai.ChatCompletion.create(
File "C:\project\lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create
return super().create(*args, **kwargs)
File "C:\project\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
File "C:\project\lib\site-packages\openai\api_requestor.py", line 298, in request
resp, got_stream = self._interpret_response(result, stream)
File "C:\project\lib\site-packages\openai\api_requestor.py", line 700, in _interpret_response
self._interpret_response_line(
File "C:\project\lib\site-packages\openai\api_requestor.py", line 763, in _interpret_response_line
raise self.handle_error_response(
openai.error.InvalidRequestError: 'type' is a required property - 'tools.0'
我也很困惑,有些函数调用使用“工具”作为参数,而在某些源中参数是“函数”。
你能帮我解决这个代码吗?谢谢!
你的做法不正确。下面是修改后的代码。同样使用openai v0版本(openai==0.28.0)
import os
import openai
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ.get('OPENAI_API_KEY')
def fetch_weather_with_openai(location, unit='celsius'):
"""
Fetch weather using OpenAI's ChatCompletion.create with a simulated function call.
Args:
location (str): The location for which weather information is requested.
unit (str): The unit of temperature (celsius or fahrenheit).
Returns:
str: The generated response simulating weather information.
"""
function_payload = [{"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"]
}
}
}
}}]
# Define the input message
messages = [
{"role": "user", "content": f"What's the weather like in {location}?"}
]
response = openai.ChatCompletion.create(
model='gpt-4',
# deployment_id=llm_config['deployment_name'],
messages=messages,
tools=function_payload
)
# Extract and return the response
# print(response)
return response.choices[0].message['tool_calls'][0]["function"]
# Example usage
response = fetch_weather_with_openai("Paris, France")
print(response)