From 5d4502df22c5ddbb61c69cddd4b892b46eadf265 Mon Sep 17 00:00:00 2001 From: Han Fangyuan Date: Sun, 19 May 2024 12:48:53 +0800 Subject: [PATCH] feat: support fastgpt tool call response (#39) --- bot/chatgpt/chat_gpt_bot.py | 44 ++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 68c520e6d..b747f83d6 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -129,10 +129,52 @@ def reply_text(self, session_id: str, session: ChatGPTSession, api_key=None, arg response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args) # logger.debug("[CHATGPT] response={}".format(response)) # logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"])) + content = response.choices[0]["message"]["content"] + # fastgpt工具调用格式处理 + if isinstance(content, list): + # { + # "id": "", + # "model": "", + # "usage": {}, + # "choices": [ + # { + # "message": { + # "role": "assistant", + # "content": [ + # { + # "type": "tool", + # "tools": [ + # { + # "id": "xx", + # "toolName": "HTTP请求", + # "toolAvatar": "xx", + # "functionName": "xx", + # "params": "{\"key1\":\"xx\",\"key2\":\"xxx"}", + # "response": "xxx" + # } + # ] + # }, + # { + # "type": "text", + # "text": { + # "content": "xxx" + # } + # } + # ] + # }, + # "finish_reason": "stop", + # "index": 0 + # } + # ] + # } + for item in content: + if item["type"] == "text": + content = item["text"]["content"] + break return { "total_tokens": response["usage"]["total_tokens"], "completion_tokens": response["usage"]["completion_tokens"], - "content": response.choices[0]["message"]["content"], + "content": content, } except Exception as e: need_retry = retry_count < 2