跳过使用Open ai 接口时,DeepSeek-R1 模型的深度思考内容 (#54)

* 跳过 DeepSeek-R1 模型的深度思考内容

* 跳过 DeepSeek-R1 模型的深度思考内容

* 新增LM Studio本地大模型API接口

* 优化代码,遇到Bad Case安全处理
This commit is contained in:
Sinyo 2025-02-19 14:26:29 +08:00 committed by kalicyh
parent 6124799cad
commit 52a21766b3
2 changed files with 24 additions and 5 deletions

View file

@ -128,6 +128,12 @@ LLM:
user_id: 你的user_id user_id: 你的user_id
base_url: "https://api.coze.cn/open_api/v2/chat" # 服务地址 base_url: "https://api.coze.cn/open_api/v2/chat" # 服务地址
personal_access_token: 你的coze个人令牌 personal_access_token: 你的coze个人令牌
LMStudioLLM:
# 定义LLM API类型
type: openai
model_name: deepseek-r1-distill-llama-8b@q4_k_m # 使用的模型名称,需要预先在社区下载
url: http://localhost:1234/v1 # LM Studio服务地址
api_key: lm-studio # LM Studio服务的固定API Key
TTS: TTS:
# 当前支持的type为edge、doubao,可自行适配 # 当前支持的type为edge、doubao,可自行适配
EdgeTTS: EdgeTTS:

View file

@ -25,12 +25,25 @@ class LLMProvider(LLMProviderBase):
messages=dialogue, messages=dialogue,
stream=True stream=True
) )
is_active = True
for chunk in responses: for chunk in responses:
try:
# 检查是否存在有效的choice且content不为空 # 检查是否存在有效的choice且content不为空
if chunk.choices and len(chunk.choices) > 0: delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
delta = chunk.choices[0].delta content = delta.content if hasattr(delta, 'content') else ''
content = getattr(delta, 'content', '') except IndexError:
if content: # 仅在content非空时生成 content = ''
if content:
# 处理标签跨多个chunk的情况
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 yield content
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error in response generation: {e}") logger.bind(tag=TAG).error(f"Error in response generation: {e}")