mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
add ollama client
This commit is contained in:
parent
d9149ff572
commit
c1e4218d8d
2 changed files with 59 additions and 0 deletions
|
|
@ -337,6 +337,11 @@ LLM:
|
||||||
model_name: glm-4-flash
|
model_name: glm-4-flash
|
||||||
url: https://open.bigmodel.cn/api/paas/v4/
|
url: https://open.bigmodel.cn/api/paas/v4/
|
||||||
api_key: 你的chat-glm web key
|
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:
|
OllamaLLM:
|
||||||
# 定义LLM API类型
|
# 定义LLM API类型
|
||||||
type: ollama
|
type: ollama
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
Loading…
Reference in a new issue