mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 13:23:55 +08:00
同步coze llm
This commit is contained in:
@@ -7,6 +7,7 @@ import os
|
|||||||
# official coze sdk for Python [cozepy](https://github.com/coze-dev/coze-py)
|
# official coze sdk for Python [cozepy](https://github.com/coze-dev/coze-py)
|
||||||
from cozepy import COZE_CN_BASE_URL
|
from cozepy import COZE_CN_BASE_URL
|
||||||
from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageContentType, ChatEventType # noqa
|
from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageContentType, ChatEventType # noqa
|
||||||
|
from core.providers.llm.system_prompt import get_system_prompt_for_function
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
@@ -47,4 +48,24 @@ class LLMProvider(LLMProviderBase):
|
|||||||
):
|
):
|
||||||
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
|
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
|
||||||
print(event.message.content, end="", flush=True)
|
print(event.message.content, end="", flush=True)
|
||||||
yield event.message.content
|
yield event.message.content
|
||||||
|
|
||||||
|
def response_with_functions(self, session_id, dialogue, functions=None):
|
||||||
|
if len(dialogue) == 2 and functions is not None and len(functions) > 0:
|
||||||
|
# 第一次调用llm, 取最后一条用户消息,附加tool提示词
|
||||||
|
last_msg = dialogue[-1]["content"]
|
||||||
|
function_str = json.dumps(functions, ensure_ascii=False)
|
||||||
|
modify_msg = get_system_prompt_for_function(function_str) + last_msg
|
||||||
|
dialogue[-1]["content"] = modify_msg
|
||||||
|
|
||||||
|
# 如果最后一个是 role="tool",附加到user上
|
||||||
|
if len(dialogue) > 1 and dialogue[-1]["role"] == "tool":
|
||||||
|
assistant_msg = "tool call result: " + dialogue[-1]["content"]
|
||||||
|
while len(dialogue) > 1 :
|
||||||
|
if dialogue[-1]["role"] == "user":
|
||||||
|
dialogue[-1]["content"] += assistant_msg
|
||||||
|
break
|
||||||
|
dialogue.pop()
|
||||||
|
|
||||||
|
for token in self.response(session_id, dialogue):
|
||||||
|
yield token, None
|
||||||
Reference in New Issue
Block a user