题意:“在 Python 中使用 OpenAI 助手的函数工具的用途”
问题背景:
I am trying to answer to OpenAI assistants' function tool.
“我正在尝试回答 OpenAI 助手的函数工具。”
See my code below. The custom function is called "funnyfunc", I am asking the assistant to invoke it, it does invoke it, and asks for the function result.
“请看我下面的代码。自定义函数名为 `funnyfunc`,我让助手调用它,它确实调用了,并要求提供函数结果。”
When I try to send the expected response (through a method I saw on a forum), I get 400 Bad Request because of unexpected parameter.
“当我尝试发送预期的响应(通过我在论坛上看到的一种方法)时,由于参数不符合预期,我收到 400 错误请求。”
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}}
I have tried various ways to send the response back, and looked on the internet a lot (also asked ChatGPT), but I cannot figure out how to do it correctly. Please help!
“我尝试了多种方法来发送响应,并在网上查找了很多资料(也问过 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?")) 
I tried to: 我尝试做以下事情:
-  
Use OpenAI's documentation - Didn't find what I needed
 
“使用 OpenAI 的文档——没有找到我需要的内容。”
-  
Read OpenAI forums - Solution provided did not work
 
“阅读了 OpenAI 论坛——提供的解决方案没有奏效。”
-  
Ask ChatGPT - No relevant output “询问 ChatGPT——没有相关输出。”
 -  
Search other sites - Did not find helpful info
 
“搜索其他网站——没有找到有用的信息。”
问题解决:
I found the answer in the meantime.
“我在这段时间找到了答案。”
https://platform.openai.com/docs/assistants/tools/function-calling?lang=python image You need to use the following code to submit outputs instead:
“https://platform.openai.com/docs/assistants/tools/function-calling?lang=python 你需要使用以下代码来提交输出:”
run = client.beta.threads.runs.submit_tool_outputs(
  thread_id=thread.id,
  run_id=run.id,
  tool_outputs=[
      {
        "tool_call_id": tool_call_id,
        "output": "not implemented",
      },
    ]
) 
 
 




















