From 7b1e17bf148d21db05d6c2e8bbd25b18177c7312 Mon Sep 17 00:00:00 2001 From: aileenfun Date: Sun, 16 Mar 2025 15:46:08 +0800 Subject: [PATCH] =?UTF-8?q?ollama=E5=8E=BB=E9=99=A4=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E4=B8=AD=E7=9A=84think=E5=AD=97=E6=AE=B5=20(#373)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ollama调用修复,并且去除回复中的think字段(deepseek) * ollama调用修复,并且去除回复中的think字段(deepseek) * ollama去除回复中的think字段(deepseek) deepseek 14b,qwen2.5测试通过 * ollama去除回复中的think字段(deepseek) deepseek 14b,qwen2.5测试通过 * Update ollama.py --------- Co-authored-by: aileenfun <105077614@qq.com> Co-authored-by: 欣南科技 --- .../core/providers/llm/ollama/ollama.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/main/xiaozhi-server/core/providers/llm/ollama/ollama.py b/main/xiaozhi-server/core/providers/llm/ollama/ollama.py index aa5184a4b..179a85b1e 100644 --- a/main/xiaozhi-server/core/providers/llm/ollama/ollama.py +++ b/main/xiaozhi-server/core/providers/llm/ollama/ollama.py @@ -12,10 +12,10 @@ class LLMProvider(LLMProviderBase): self.model_name = config.get("model_name") self.base_url = config.get("base_url", "http://localhost:11434") # Initialize OpenAI client with Ollama base URL - #如果没有v1,增加v1 + # 如果没有v1,增加v1 if not self.base_url.endswith("/v1"): self.base_url = f"{self.base_url}/v1" - + self.client = OpenAI( base_url=self.base_url, api_key="ollama" # Ollama doesn't need an API key but OpenAI client requires one @@ -28,13 +28,20 @@ class LLMProvider(LLMProviderBase): messages=dialogue, stream=True ) - + is_active=True for chunk in responses: try: delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None content = delta.content if hasattr(delta, 'content') else '' if content: - yield content + if '' in content: + is_active = False + content = content.split('')[0] + if '' in content: + is_active = True + content = content.split('')[-1] + if is_active: + yield content except Exception as e: logger.bind(tag=TAG).error(f"Error processing chunk: {e}") @@ -50,10 +57,10 @@ class LLMProvider(LLMProviderBase): stream=True, tools=functions, ) - + for chunk in stream: yield chunk.choices[0].delta.content, chunk.choices[0].delta.tool_calls except Exception as e: logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}") - yield {"type": "content", "content": f"【Ollama服务响应异常: {str(e)}】"} \ No newline at end of file + yield {"type": "content", "content": f"【Ollama服务响应异常: {str(e)}】"}