About the Assistant's Tools - Functions (Fetching Web Pages)

我现在有个需求就是(I have a need right now)

如果用户在消息内明确需要访问url获取其内容
If the user explicitly needs to access the url in the message to get its content

我的解决方案(My solution)

是用助手函数判断是否需要解析url,然后返回函数结果给gpt返回这个url对应的内容
Is to use the helper function to determine whether to parse the url, and then return the function result to gpt to return the url corresponding content

以下是我的代码实现,有几个问题(Here is my code implementation, with a few problems)

创建助手(Create assistant)

{
    "model": "gpt-4-turbo-2024-04-09",
    "name": "测试助手(Test assistant)",
    "description": "你是一个获取聊天助手,你可以使用我提供给你的函数来获取用户消息中的url(You are a fetching chat assistant and you can use the function I have provided you to fetch the url in the user's message)",
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_msg_url",
                "description": "获取消息内容中的url(Gets the url in the message content)",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "如果用户需要获取网站或者url的内容、信息,就返回url。例如‘https://overseas.queenou.cn/negativeReviewRate’格式,如果用户的url没有指定协议类型,默认使用http协议,如果有指定使用指定的协议,如果不是https和http协议返回url空(If the user needs to obtain the content or information of the website or url, it returns the url. 'https://overseas.queenou.cn/negativeReviewRate' format, for example, if the user do not specify a url protocol type, default using the HTTP protocol, if there are specified using the specified protocol, if not the HTTPS and HTTP protocol returns empty url)"
                        }
                    },
                    "required": [
                        "url"
                    ]
                }
            }
        }
    ]
}

这是请求创建消息(This is the request creation message)

{
    "thread_id": "thread_l44iBJtHSzxAL01y60y7rpza",
    "role": "user",
    "content": "这个网站讲了什么(What does this website say)https://blog.csdn.net/liuxiao723846/article/details/123728803",
}

然后运行这个线程(Then run the thread)

检索线程状态的信息(Retrieves information about thread state)

{
    "id": "run_BG2BdO4JpRlNxCTPEXwXG8UE",
    "object": "thread.run",
    "created_at": 1713944981,
    "metadata": {},
    "assistant_id": "asst_9T0VXrtLO0N54XBRUd27srQN",
    "thread_id": "thread_l44iBJtHSzxAL01y60y7rpza",
    "status": "requires_action",
    "started_at": 1713944981,
    "expires_at": 1.713945581E9,
    "cancelled_at": null,
    "failed_at": null,
    "completed_at": null,
    "last_error": null,
    "model": "gpt-4-turbo-2024-04-09",
    "instructions": null,
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_msg_url",
                "description": "获取消息内容中的url(Gets the url in the message content)",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "如果用户需要获取网站或者url的内容、信息,就返回url。例如‘https://overseas.queenou.cn/negativeReviewRate’格式,如果用户的url没有指定协议类型,默认使用http协议,如果有指定使用指定的协议,如果不是https和http协议返回url空(If the user needs to obtain the content or information of the website or url, it returns the url. 'https://overseas.queenou.cn/negativeReviewRate' format, for example, if the user do not specify a url protocol type, default using the HTTP protocol, if there are specified using the specified protocol, if not the HTTPS and HTTP protocol returns empty url)"
                        }
                    },
                    "required": [
                        "url"
                    ]
                }
            }
        }
    ],
    "max_prompt_tokens": null,
    "max_completion_tokens": null,
    "truncation_strategy": {
        "last_messages": null,
        "type": "auto"
    },
    "usage": null
}

这是我提交工具响应(This is my submitted tool response)

{
    "thread_id": "thread_l44iBJtHSzxAL01y60y7rpza",
    "run_id": "run_BG2BdO4JpRlNxCTPEXwXG8UE",
    "tool_outputs": [
        {
            "tool_call_id": "call_dnsFGbrCBzM4CQ5UlgTCHA0k",
            "output": "一个很长的html(A very long html)"
        }
    ]
}

ChineseVersion:
这是我的处理逻辑,请指教!
关于这个处理逻辑我有几个问题:

  1. 关于我创建的这个助手这个函数,我怎么在运行线程的时候知道要调用哪个函数呢?(假设我添加了几个函数)是因为我没有解析到还是哪儿漏了返参?
  2. 线程的这几个状态有什么区别 status: "requires_action" and required_action.type is submit_tool_outputs
  3. 使用Submit tool outputs to run附加信息到线程为什么我获取不到这个附加的工具消息?也是我没有解析到这个返参吗?
  4. 关于使用Submit tool outputs to run附加到线程是不是也会一样计算token,那这样附加一个超长的html岂不是会导致费用大增!

EnglishVersion:
This is my processing logic, please advise!
I have a few questions about this processing logic:

  1. Regarding the function I created for this assistant, how do I know which function to call when running the thread? (Assuming I added several functions) Is it because I didn’t parse it or is there a missed return parameter?
  2. What’s the difference between these thread statuses status: "requires_action" and required_action.type is submit_tool_outputs
  3. Why can’t I get the added tool message when using Submit tool outputs to run to attach information to the thread? Is it also because I didn’t parse this return parameter?
  4. Regarding the use of Submit tool outputs to run to attach to the thread, will it also calculate the token in the same way, then attaching an overly long html will not lead to a significant increase in costs!

I am very sorry, my English is mediocre, I used machine translation.
Thank you in advance for your viewing and response. :slightly_smiling_face: