diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 21f37c5b..89a6f7f9 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -186,6 +186,10 @@ LLM: # 如果使用DifyLLM,配置文件里prompt(提示词)是无效的,需要在dify控制台设置提示词 base_url: https://api.dify.cn/v1 api_key: 你的DifyLLM web key + # 使用的对话模式 可以选择工作流 workflows/run 对话模式 chat-messages 文本生成 completion-messages + # 使用workflows进行返回的时候输入参数为 query 返回参数的名字要设置为 answer + # 文本生成的默认输入参数也是query + mode: workflows/run GeminiLLM: type: gemini # 谷歌Gemini API,需要先在Google Cloud控制台创建API密钥并获取api_key diff --git a/main/xiaozhi-server/core/providers/llm/dify/dify.py b/main/xiaozhi-server/core/providers/llm/dify/dify.py index f3c62639..02ef5588 100644 --- a/main/xiaozhi-server/core/providers/llm/dify/dify.py +++ b/main/xiaozhi-server/core/providers/llm/dify/dify.py @@ -9,6 +9,7 @@ logger = setup_logging() class LLMProvider(LLMProviderBase): def __init__(self, config): self.api_key = config["api_key"] + self.mode = config.get("mode", "workflows/run") self.base_url = config.get("base_url", "https://api.dify.ai/v1").rstrip('/') self.session_conversation_map = {} # 存储session_id和conversation_id的映射 @@ -19,27 +20,59 @@ class LLMProvider(LLMProviderBase): conversation_id = self.session_conversation_map.get(session_id) # 发起流式请求 - with requests.post( - f"{self.base_url}/chat-messages", - headers={"Authorization": f"Bearer {self.api_key}"}, - json={ + if self.mode == "chat-messages": + request_json = { "query": last_msg["content"], "response_mode": "streaming", "user": session_id, "inputs": {}, "conversation_id": conversation_id - }, + } + elif self.mode == "workflows/run": + request_json = { + "inputs": {"query": last_msg["content"]}, + "response_mode": "streaming", + "user": session_id + } + elif self.mode == "completion-messages": + request_json = { + "inputs": {"query": last_msg["content"]}, + "response_mode": "streaming", + "user": session_id + } + + with requests.post( + f"{self.base_url}/{self.mode}", + headers={"Authorization": f"Bearer {self.api_key}"}, + json=request_json, stream=True ) as r: - for line in r.iter_lines(): - if line.startswith(b'data: '): - event = json.loads(line[6:]) - # 如果没有找到conversation_id,则获取此次conversation_id - if not conversation_id: - conversation_id = event.get('conversation_id') - self.session_conversation_map[session_id] = conversation_id # 更新映射 - if event.get('answer'): - yield event['answer'] + if self.mode == "chat-messages": + for line in r.iter_lines(): + if line.startswith(b'data: '): + event = json.loads(line[6:]) + # 如果没有找到conversation_id,则获取此次conversation_id + if not conversation_id: + conversation_id = event.get('conversation_id') + self.session_conversation_map[session_id] = conversation_id # 更新映射 + if event.get('answer'): + yield event['answer'] + elif self.mode == "workflows/run": + for line in r.iter_lines(): + # logger.bind(tag=TAG).info(f"chat message response: {line}") + if line.startswith(b'data: '): + event = json.loads(line[6:]) + if event.get('event') == "workflow_finished": + if event['data']['status'] == "succeeded": + yield event['data']['outputs']['answer'] + else: + yield "【服务响应异常】" + elif self.mode == "completion-messages": + for line in r.iter_lines(): + if line.startswith(b'data: '): + event = json.loads(line[6:]) + if event.get('answer'): + yield event['answer'] except Exception as e: logger.bind(tag=TAG).error(f"Error in response generation: {e}")