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)}】"}