Skip to content

Commit

Permalink
feat: support fastgpt tool call response (zhayujie#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfangyuan4396 authored May 19, 2024
1 parent b30c36d commit 5d4502d
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion bot/chatgpt/chat_gpt_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5d4502d

Please sign in to comment.