我正在尝试回答OpenAI助手的功能工具。
请参阅下面我的代码。自定义函数称为“funnyfunc”,我要求助手调用它,它确实调用了它,并询问函数结果。
当我尝试发送预期的响应(通过我在论坛上看到的方法)时,由于意外参数,我收到 400 Bad Request。
openai.BadRequestError: Error code: 400 - {'error': {'message': "1 validation error for Request\nbody -> role\n value is not a valid enumeration member; permitted: 'user' (type=type_error.enum; enum_values=[<RoleParam.USER: 'user'>])", 'type': 'invalid_request_error', 'param': None, 'code': None}}
我尝试了各种方法来发回响应,并且在互联网上查阅了很多(也询问了ChatGPT),但我不知道如何正确地做到这一点。请帮忙!
from openai import OpenAI
import time
import os
client = OpenAI()
thread = client.beta.threads.create()
assistant = client.beta.assistants.retrieve(os.environ.get("ALEXA_ASSISTANT_ID_OPENAI"))
def wait_on_run(run, thread):
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.25)
return run
def askQuestion(question):
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=question,
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
run = wait_on_run(run, thread)
while run.status == 'requires_action':
if run.required_action.type == 'submit_tool_outputs':
if run.required_action.submit_tool_outputs.tool_calls[0].function.name == 'funnyfunc':
ret = "five"
tool_call_id = run.required_action.submit_tool_outputs.tool_calls[0].id
message = client.beta.threads.messages.create( # ERROR POPS UP ON THIS LINE
thread_id=thread.id,
role="tool", # THIS DOES NOT WORK
content=ret,
)
#run = client.beta.threads.runs.create( # Maybe something like this??
# thread_id=thread.id,
# assistant_id=assistant.id,
#)
else:
raise NotImplementedError(run.required_action.submit_tool_outputs.tool_calls[0].function.name)
run = wait_on_run(run, thread)
else:
raise NotImplementedError(run.required_action.type)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
return messages.data[0].content[0].text.value
print(askQuestion("What is funnyfunc() equal to right now?"))
我尝试:
使用 OpenAI 的文档 - 没有找到我需要的东西
阅读 OpenAI 论坛 - 提供的解决方案不起作用
询问 ChatGPT - 没有相关输出
搜索其他网站 - 没有找到有用的信息