mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
ollama去除回复中的think字段(deepseek)
deepseek 14b,qwen2.5测试通过
This commit is contained in:
parent
57aea04c19
commit
45a1fa1b74
1 changed files with 40 additions and 28 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
import requests, json
|
from openai import OpenAI
|
||||||
|
import json
|
||||||
from core.providers.llm.base import LLMProviderBase
|
from core.providers.llm.base import LLMProviderBase
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
@ -8,39 +9,33 @@ logger = setup_logging()
|
||||||
|
|
||||||
class LLMProvider(LLMProviderBase):
|
class LLMProvider(LLMProviderBase):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
|
|
||||||
self.model_name = config.get("model_name")
|
self.model_name = config.get("model_name")
|
||||||
self.base_url = config.get("base_url", "http://localhost:11434")
|
self.base_url = config.get("base_url", "http://localhost:11434")
|
||||||
|
# Initialize OpenAI client with Ollama base URL
|
||||||
|
# 如果没有v1,增加v1
|
||||||
|
|
||||||
def response(self, session_id, dialogue):
|
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
|
||||||
|
)
|
||||||
|
print("ollama init finished")
|
||||||
|
|
||||||
|
def response(self, session_id, dialogue):
|
||||||
try:
|
try:
|
||||||
# Convert dialogue format to Ollama format
|
responses = self.client.chat.completions.create(
|
||||||
prompt = ""
|
model=self.model_name,
|
||||||
for msg in dialogue:
|
messages=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
|
stream=True
|
||||||
)
|
)
|
||||||
is_active=True
|
is_active=True
|
||||||
for line in response.iter_lines():
|
for chunk in responses:
|
||||||
content=''
|
try:
|
||||||
if line:
|
delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
|
||||||
json_response = json.loads(line)
|
content = delta.content if hasattr(delta, 'content') else ''
|
||||||
if "response" in json_response:
|
if content:
|
||||||
content=json_response["response"]
|
|
||||||
if '<think>' in content:
|
if '<think>' in content:
|
||||||
is_active = False
|
is_active = False
|
||||||
content = content.split('<think>')[0]
|
content = content.split('<think>')[0]
|
||||||
|
|
@ -49,8 +44,25 @@ class LLMProvider(LLMProviderBase):
|
||||||
content = content.split('</think>')[-1]
|
content = content.split('</think>')[-1]
|
||||||
if is_active:
|
if is_active:
|
||||||
yield content
|
yield content
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Error processing chunk: {e}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"Error in Ollama response generation: {e}")
|
logger.bind(tag=TAG).error(f"Error in Ollama response generation: {e}")
|
||||||
yield "【Ollama服务响应异常】"
|
yield "【Ollama服务响应异常】"
|
||||||
|
|
||||||
|
def response_with_functions(self, session_id, dialogue, functions=None):
|
||||||
|
try:
|
||||||
|
stream = self.client.chat.completions.create(
|
||||||
|
model=self.model_name,
|
||||||
|
messages=dialogue,
|
||||||
|
stream=False,
|
||||||
|
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)}】"}
|
||||||
Loading…
Reference in a new issue