gpt4 book ai didi

python - OpenAI 聊天完成 API : How do I use a function to store conversation memory?

转载 作者:行者123 更新时间:2023-12-02 22:48:22 26 4
gpt4 key购买 nike

我正在尝试使用 OpenAI 函数调用创建一个聊天机器人。我采用了获取当前天气状况的基本示例,该示例在文档中给出。

我想要实现的是拥有一个内存。

我尝试附加到消息中,但我想要的是当我有一条新消息时,所以不是调用该函数,如果已经询问过,它如何从内存中获取响应?

我的代码是这样的:

def get_current_weather(location, unit="fahrenheit"):
print("IT RAN>>>>>>>>>>")
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)


messages = []


def run_conversation(input_message):
messages.append({"role": "user", "content": f"{input_message}"})
functions = [
{
"name": "get_current_weather",
"description": "Get the details about a drug/medication",
"parameters": {
"type": "object",
"properties": {
"definition": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]
print("MESSAGE 1", messages)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=messages,
functions=functions,
# function_call="auto",
)
response_message = response["choices"][0]["message"]
print("RESPONSE MSG", response_message)

if response_message.get("function_call"):
available_functions = {"get_current_weather": get_current_weather}
function_name = response_message["function_call"]["name"]
function_to_call = available_functions[function_name]
function_args = json.loads(response_message["function_call"]["arguments"])
function_response = function_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)

# messages.append(response_message)
messages.append(
{"role": "function", "name": function_name, "content": function_response}
)
print("MESSAGE 2", messages)
second_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=messages,
)
print("SECOND RESPONSE", second_response['choices'][0]['message'].to_dict())
messages.append(second_response['choices'][0]['message'].to_dict())
print("MESSAGE 3", messages)
return second_response

即使我问同样的问题,它也总是运行该函数

输出:

RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\n \"definition\": \"Boston, MA\",\n \"unit\": \"celsius\"\n}"
}
}
IT RAN>>>>>>>>>>
MESSAGE 2 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}]
SECOND RESPONSE {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Celsius.'}
MESSAGE 3 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}]
MESSAGE 1 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}]
RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\"location\": \"New York\"}"
}
}
IT RAN>>>>>>>>>>
MESSAGE 2 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"locati
on": "New York", "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}]
SECOND RESPONSE {'role': 'assistant', 'content': 'The temperature in New York is 72 degrees. Please note that I did not specify the temperature unit, as i
t is missing in the response.'}
MESSAGE 3 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"locati
on": "New York", "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in New York is 72
degrees. Please note that I did not specify the temperature unit, as it is missing in the response.'}]
MESSAGE 1 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"locati
on": "New York", "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in New York is 72
degrees. Please note that I did not specify the temperature unit, as it is missing in the response.'}, {'role': 'user', 'content': 'what is the temperatu
re in Boston?'}]
RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\"definition\": \"Boston\"}"
}
}
IT RAN>>>>>>>>>>
MESSAGE 2 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"locati
MESSAGE 3 [{'role': 'user', 'content': 'what is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degree
s Celsius.'}, {'role': 'user', 'content': 'what is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"locati
on": "New York", "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in New York is 72
degrees. Please note that I did not specify the temperature unit, as it is missing in the response.'}, {'role': 'user', 'content': 'what is the temperatu
re in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location": null, "temperature": "72", "unit": null, "forecast": ["sunny
", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees. Please note that the unit of temperature is missing in the resp
onse.'}]
MESSAGE 1 [{'role': 'user', 'content': 'What is the temperature in Boston?'}]
RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\n \"definition\": \"Boston, MA\"\n}"
}
}
IT RAN>>>>>>>>>>
MESSAGE 2 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}]
SECOND RESPONSE {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fahrenheit.'}
MESSAGE 3 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fah
renheit.'}]
MESSAGE 1 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fah
renheit.'}, {'role': 'user', 'content': 'What is the temperature in NewYork?'}]
RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\"location\": \"New York\", \"unit\": \"celsius\"}"
}
}
IT RAN>>>>>>>>>>
MESSAGE 2 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fah
renheit.'}, {'role': 'user', 'content': 'What is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location
": "New York", "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}]
SECOND RESPONSE {'role': 'assistant', 'content': 'The temperature in New York is 72 degrees Celsius.'}
MESSAGE 3 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fah
renheit.'}, {'role': 'user', 'content': 'What is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location
": "New York", "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in New York is
72 degrees Celsius.'}]
MESSAGE 1 [{'role': 'user', 'content': 'What is the temperature in Boston?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location":
null, "temperature": "72", "unit": null, "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in Boston is 72 degrees Fah
renheit.'}, {'role': 'user', 'content': 'What is the temperature in NewYork?'}, {'role': 'function', 'name': 'get_current_weather', 'content': '{"location
": "New York", "temperature": "72", "unit": "celsius", "forecast": ["sunny", "windy"]}'}, {'role': 'assistant', 'content': 'The temperature in New York is
72 degrees Celsius.'}, {'role': 'user', 'content': 'What is the temperature in Boston?'}]
RESPONSE MSG {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\"location\": \"Boston\"}"
}
is 72 degrees.'}]

最佳答案

使用 OpenAI API 函数存储对话内存并不是正确的方法,因为模型可能会生成无效的 JSON 或幻觉参数。

人们已经 having problems就这样。

如果你看一下官方OpenAI documentation ,您可以看到 OpenAI 透明地声明:

The basic sequence of steps for function calling is as follows:

  1. Call the model with the user query and a set of functions defined in the functions parameter.
  2. The model can choose to call a function; if so, the content will be a stringified JSON object adhering to your custom schema (note: themodel may generate invalid JSON or hallucinate parameters).
  3. Parse the string into JSON in your code, and call your function with the provided arguments if they exist.
  4. Call the model again by appending the function response as a new message, and let the model summarize the results back to the user.

关于python - OpenAI 聊天完成 API : How do I use a function to store conversation memory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76734099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com