增加本地大模型ollama支持

This commit is contained in:
玄凤科技
2025-02-14 10:24:02 +08:00
parent df0e44798a
commit 8266979e91
2 changed files with 51 additions and 1 deletions
+6 -1
View File
@@ -44,7 +44,7 @@ VAD:
min_silence_duration_ms: 700 # 如果说话停顿比较长,可以把这个值设置大一些
LLM:
# 当前支持的type为openai、dify,可自行适配
# 当前支持的type为openai、dify、ollama,可自行适配
AliLLM:
# 定义LLM API类型
type: openai
@@ -67,6 +67,11 @@ LLM:
model_name: glm-4-flash
url: https://open.bigmodel.cn/api/paas/v4/
api_key: 你的bigmodel api key
OllamaLLM:
# 定义LLM API类型
type: ollama
model_name: qwen2.5 # 使用的模型名称,需要预先使用ollama pull下载
base_url: http://localhost:11434 # Ollama服务地址
DifyLLM:
# 定义LLM API类型
type: dify
+45
View File
@@ -0,0 +1,45 @@
import logging
import requests, json
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
class LLMProvider(LLMProviderBase):
def __init__(self, config):
self.model_name = config.get("model_name")
self.base_url = config.get("base_url", "http://localhost:11434")
def response(self, session_id, dialogue):
try:
# Convert dialogue format to Ollama format
prompt = ""
for msg in dialogue:
if msg["role"] == "system":
prompt += f"System: {msg['content']}\n"
elif msg["role"] == "user":
prompt += f"User: {msg['content']}\n"
elif msg["role"] == "assistant":
prompt += f"Assistant: {msg['content']}\n"
# Make request to Ollama API
response = requests.post(
f"{self.base_url}/api/generate",
json={
"model": self.model_name,
"prompt": prompt,
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
json_response = json.loads(line)
if "response" in json_response:
yield json_response["response"]
except Exception as e:
logger.error(f"Error in Ollama response generation: {e}")
yield "【Ollama服务响应异常】"