ollama去除回复中的think字段 (#373)

* ollama调用修复,并且去除回复中的think字段(deepseek)

* ollama调用修复,并且去除回复中的think字段(deepseek)

* ollama去除回复中的think字段(deepseek)
deepseek 14b,qwen2.5测试通过

* ollama去除回复中的think字段(deepseek)
deepseek 14b,qwen2.5测试通过

* Update ollama.py

---------

Co-authored-by: aileenfun <105077614@qq.com>
Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>
This commit is contained in:
aileenfun 2025-03-16 15:46:08 +08:00 committed by GitHub
parent 490a66d3fe
commit 7b1e17bf14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,10 +12,10 @@ class LLMProvider(LLMProviderBase):
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 # Initialize OpenAI client with Ollama base URL
#如果没有v1,增加v1 # 如果没有v1,增加v1
if not self.base_url.endswith("/v1"): if not self.base_url.endswith("/v1"):
self.base_url = f"{self.base_url}/v1" self.base_url = f"{self.base_url}/v1"
self.client = OpenAI( self.client = OpenAI(
base_url=self.base_url, base_url=self.base_url,
api_key="ollama" # Ollama doesn't need an API key but OpenAI client requires one api_key="ollama" # Ollama doesn't need an API key but OpenAI client requires one
@ -28,13 +28,20 @@ class LLMProvider(LLMProviderBase):
messages=dialogue, messages=dialogue,
stream=True stream=True
) )
is_active=True
for chunk in responses: for chunk in responses:
try: try:
delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
content = delta.content if hasattr(delta, 'content') else '' content = delta.content if hasattr(delta, 'content') else ''
if content: if content:
yield content if '<think>' in content:
is_active = False
content = content.split('<think>')[0]
if '</think>' in content:
is_active = True
content = content.split('</think>')[-1]
if is_active:
yield content
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error processing chunk: {e}") logger.bind(tag=TAG).error(f"Error processing chunk: {e}")
@ -50,10 +57,10 @@ class LLMProvider(LLMProviderBase):
stream=True, stream=True,
tools=functions, tools=functions,
) )
for chunk in stream: for chunk in stream:
yield chunk.choices[0].delta.content, chunk.choices[0].delta.tool_calls yield chunk.choices[0].delta.content, chunk.choices[0].delta.tool_calls
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}") logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}")
yield {"type": "content", "content": f"【Ollama服务响应异常: {str(e)}】"} yield {"type": "content", "content": f"【Ollama服务响应异常: {str(e)}】"}