From cb540736abd6cf7d0c7be91fd74eaf1ba483ef2b Mon Sep 17 00:00:00 2001 From: kalicyh <34980061+kalicyh@users.noreply.github.com> Date: Mon, 24 Feb 2025 16:16:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=B3=E8=BF=87=E4=BD=BF=E7=94=A8Open=20ai?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E6=97=B6=EF=BC=8CDeepSeek-R1=20=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E6=B7=B1=E5=BA=A6=E6=80=9D=E8=80=83=E5=86=85?= =?UTF-8?q?=E5=AE=B9=20(#54)=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 跳过使用Open ai 接口时,DeepSeek-R1 模型的深度思考内容 (#54) * 跳过 DeepSeek-R1 模型的深度思考内容 * 跳过 DeepSeek-R1 模型的深度思考内容 * 新增LM Studio本地大模型API接口 * 优化代码,遇到Bad Case安全处理 * update:优化 --------- Co-authored-by: Sinyo <38577585+SinyoWong@users.noreply.github.com> Co-authored-by: hrz <1710360675@qq.com> --- config.yaml | 6 ++++++ core/providers/llm/openai/openai.py | 23 ++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/config.yaml b/config.yaml index 6763ee54..9fbb78d5 100644 --- a/config.yaml +++ b/config.yaml @@ -147,6 +147,12 @@ LLM: user_id: 你的user_id base_url: "https://api.coze.cn/open_api/v2/chat" # 服务地址 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 HomeAssistant: # 定义LLM API类型 type: homeassistant diff --git a/core/providers/llm/openai/openai.py b/core/providers/llm/openai/openai.py index e574d50d..e060248f 100644 --- a/core/providers/llm/openai/openai.py +++ b/core/providers/llm/openai/openai.py @@ -25,12 +25,25 @@ class LLMProvider(LLMProviderBase): messages=dialogue, stream=True ) + + is_active = True for chunk in responses: - # 检查是否存在有效的choice且content不为空 - if chunk.choices and len(chunk.choices) > 0: - delta = chunk.choices[0].delta - content = getattr(delta, 'content', '') - if content: # 仅在content非空时生成 + try: + # 检查是否存在有效的choice且content不为空 + delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None + content = delta.content if hasattr(delta, 'content') else '' + except IndexError: + content = '' + if content: + # 处理标签跨多个chunk的情况 + if '' in content: + is_active = False + content = content.split('')[0] + if '' in content: + is_active = True + content = content.split('')[-1] + if is_active: yield content + except Exception as e: logger.bind(tag=TAG).error(f"Error in response generation: {e}")