diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index b053db6a3..4995d27a3 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -337,6 +337,11 @@ LLM: model_name: glm-4-flash url: https://open.bigmodel.cn/api/paas/v4/ api_key: 你的chat-glm web key + OllamaLLMClient: + # 定义LLM API类型 + type: ollama_client + model_name: qwen2.5:7b # 使用的模型名称,需要预先使用ollama pull下载 + base_url: http://localhost:11434 # Ollama服务地址 OllamaLLM: # 定义LLM API类型 type: ollama diff --git a/main/xiaozhi-server/core/providers/llm/ollama_client/ollama_client.py b/main/xiaozhi-server/core/providers/llm/ollama_client/ollama_client.py new file mode 100644 index 000000000..4c992fe8e --- /dev/null +++ b/main/xiaozhi-server/core/providers/llm/ollama_client/ollama_client.py @@ -0,0 +1,54 @@ +from config.logger import setup_logging +from ollama import Client +from core.providers.llm.base import LLMProviderBase + +TAG = __name__ +logger = setup_logging() + + +class LLMProvider(LLMProviderBase): + + def __init__(self, config): + self.model_name = config.get("model_name") + self.base_url = config.get("base_url", "http://localhost:11434") + self.client = Client(self.base_url) + def response(self, session_id, dialogue): + try: + responses = self.client.chat( + model=self.model_name, + messages=dialogue, + stream=True + ) + for chunk in responses: + try: + if chunk.message.content: + content = chunk.message.content + yield content + except Exception as e: + logger.bind(tag=TAG).error(f"Error processing chunk: {e}") + + except Exception as e: + logger.bind(tag=TAG).error(f"Error in Ollama response generation: {e}") + yield "【Ollama服务响应异常】" + def response_with_functions(self, session_id, dialogue, functions=None): + try: + stream = self.client.chat( + model=self.model_name, + messages=dialogue, + stream=True, + tools=functions, + ) + + logger.bind(tag=TAG).info(f"msg: {dialogue}") + + response_text = "" + for chunk in stream: + if chunk.message.content: + content = chunk.message.content + response_text += content + yield response_text, None + + except Exception as e: + logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}") + yield f"【Ollama服务响应异常: {str(e)}】", None +