From 2f79f9dfb21a2eadd8a89560dd6c3bdc14186de1 Mon Sep 17 00:00:00 2001 From: z30057876 Date: Fri, 14 Feb 2025 14:43:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Chart=E9=83=A8=E7=BD=B2bu?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/chart/authhub/templates/secrets.yaml | 2 +- deploy/chart/databases/templates/secrets.yaml | 8 ++++---- .../euler_copilot/configs/framework/.env | 3 --- .../euler_copilot/templates/secrets.yaml | 20 +++++++++++-------- deploy/chart/euler_copilot/values.yaml | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/deploy/chart/authhub/templates/secrets.yaml b/deploy/chart/authhub/templates/secrets.yaml index 62a3e4331..a274f0973 100644 --- a/deploy/chart/authhub/templates/secrets.yaml +++ b/deploy/chart/authhub/templates/secrets.yaml @@ -9,7 +9,7 @@ metadata: helm.sh/resource-policy: keep type: Opaque stringData: - mysql-password: {{ $authhubSecret.data.mysql-password | b64dec }} + mysql-password: {{ index $authhubSecret.data "mysql-password" | b64dec }} {{- else -}} apiVersion: v1 kind: Secret diff --git a/deploy/chart/databases/templates/secrets.yaml b/deploy/chart/databases/templates/secrets.yaml index 36f993783..e2a4a071a 100644 --- a/deploy/chart/databases/templates/secrets.yaml +++ b/deploy/chart/databases/templates/secrets.yaml @@ -9,10 +9,10 @@ metadata: helm.sh/resource-policy: keep type: Opaque stringData: - redis-password: {{ $databaseSecret.data.redis-password | b64dec }} - mongo-password: {{ $databaseSecret.data.mongo-password | b64dec }} - minio-password: {{ $databaseSecret.data.minio-password | b64dec }} - pgsql-password: {{ $databaseSecret.data.pgsql-password | b64dec }} + redis-password: {{ index $databaseSecret.data "redis-password" | b64dec }} + mongo-password: {{ index $databaseSecret.data "mongo-password" | b64dec }} + minio-password: {{ index $databaseSecret.data "minio-password" | b64dec }} + pgsql-password: {{ index $databaseSecret.data "pgsql-password" | b64dec }} {{- else -}} apiVersion: v1 kind: Secret diff --git a/deploy/chart/euler_copilot/configs/framework/.env b/deploy/chart/euler_copilot/configs/framework/.env index ac40de311..99e48bca0 100644 --- a/deploy/chart/euler_copilot/configs/framework/.env +++ b/deploy/chart/euler_copilot/configs/framework/.env @@ -2,9 +2,6 @@ DEPLOY_MODE=local COOKIE_MODE=domain -# Web -WEB_FRONT_URL={{ .Values.euler_copilot.framework.web_url }} - # Redis REDIS_HOST=redis-db.{{ .Release.Namespace }}.svc.cluster.local REDIS_PORT=6379 diff --git a/deploy/chart/euler_copilot/templates/secrets.yaml b/deploy/chart/euler_copilot/templates/secrets.yaml index db56b8074..c92948cfa 100644 --- a/deploy/chart/euler_copilot/templates/secrets.yaml +++ b/deploy/chart/euler_copilot/templates/secrets.yaml @@ -5,28 +5,32 @@ kind: Secret metadata: name: euler-copilot-system namespace: {{ .Release.Namespace }} + annotations: + helm.sh/resource-policy: keep type: Opaque stringData: - jwtKey: {{ $systemSecret.data.jwtKey | b64dec }} - halfKey1: {{ $systemSecret.data.halfKey1 | b64dec }} - halfKey2: {{ $systemSecret.data.halfKey2 | b64dec }} - halfKey3: {{ $systemSecret.data.halfKey3 | b64dec }} - csrfKey: {{ $systemSecret.data.csrfKey | b64dec }} - clientId: {{ $systemSecret.data.clientId | b64dec }} - clientSecret: {{ $systemSecret.data.clientSecret | b64dec }} + jwtKey: {{ index $systemSecret.data "jwtKey" | b64dec }} + halfKey1: {{ index $systemSecret.data "halfKey1" | b64dec }} + halfKey2: {{ index $systemSecret.data "halfKey2" | b64dec }} + halfKey3: {{ index $systemSecret.data "halfKey3" | b64dec }} + csrfKey: {{ index $systemSecret.data "csrfKey" | b64dec }} + clientId: {{ index $systemSecret.data "clientId" | b64dec }} + clientSecret: {{ index $systemSecret.data "clientSecret" | b64dec }} {{- else -}} apiVersion: v1 kind: Secret metadata: name: euler-copilot-system namespace: {{ .Release.Namespace }} + annotations: + helm.sh/resource-policy: keep type: Opaque stringData: jwtKey: {{ randAlphaNum 32 | b64enc }} halfKey1: {{ randAlphaNum 32 }} halfKey2: {{ randAlphaNum 32 }} halfKey3: {{ randAlphaNum 32 }} - csrfKey: {{ randAlphaNum 32 }} + csrfKey: {{ randAlphaNum 32 | b64enc}} clientId: {{ .Values.login.client.id }} clientSecret: {{ .Values.login.client.secret }} {{- end -}} diff --git a/deploy/chart/euler_copilot/values.yaml b/deploy/chart/euler_copilot/values.yaml index a8d2c038b..7392263c5 100644 --- a/deploy/chart/euler_copilot/values.yaml +++ b/deploy/chart/euler_copilot/values.yaml @@ -29,7 +29,7 @@ models: # API Key;不填则与问答模型一致 key: # 模型名称;不填则与问答模型一致 - model: + name: # 模型最大上下文数;不填则与问答模型一致 ctx_length: # 模型最大输出长度;不填则与问答模型一致 -- Gitee From 7bcc68cd7d361b73cdc46b36a574e7946be866cc Mon Sep 17 00:00:00 2001 From: z30057876 Date: Fri, 14 Feb 2025 14:44:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E5=8F=98=E9=87=8F=EF=BC=8C=E4=BF=AE=E6=AD=A3think=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/config.py | 2 +- apps/llm/patterns/recommend.py | 16 ++---- apps/llm/reasoning.py | 98 ++++++++++++++++++++++++---------- apps/main.py | 2 +- apps/service/suggestion.py | 7 --- 5 files changed, 76 insertions(+), 49 deletions(-) diff --git a/apps/common/config.py b/apps/common/config.py index a3f3b7428..d7f3c23da 100644 --- a/apps/common/config.py +++ b/apps/common/config.py @@ -17,7 +17,7 @@ class ConfigModel(BaseModel): DEPLOY_MODE: str = Field(description="oidc 部署方式", default="online") COOKIE_MODE: str = Field(description="COOKIE SET 方式", default="domain") # WEB - WEB_FRONT_URL: str = Field(description="web前端地址") + WEB_FRONT_URL: str = Field(description="web前端跳转地址", default="/") # Redis REDIS_HOST: str = Field(description="Redis主机名") REDIS_PORT: int = Field(description="Redis端口号", default=6379) diff --git a/apps/llm/patterns/recommend.py b/apps/llm/patterns/recommend.py index fd1e2d1d6..79978a505 100644 --- a/apps/llm/patterns/recommend.py +++ b/apps/llm/patterns/recommend.py @@ -25,10 +25,10 @@ class Recommend(CorePattern): 1. 从用户角度生成预测问题。 2. 预测问题应为疑问句或祈使句,必须少于30字。 3. 预测问题应优先贴合工具描述,特别是工具描述与背景或倾向无关时。 - 5. 预测问题必须精简,不得输出非必要信息。 + 5. 预测问题必须精简,不得输出非必要信息,不得输出除问题以外的文字。 6. 预测问题不得与“用户历史提问”重复或相似。 - ==示例== + ==以下是一个例子== 工具描述:调用API,查询天气数据 用户历史提问: @@ -40,9 +40,9 @@ class Recommend(CorePattern): 生成的预测问题: 杭州西湖景区的门票价格是多少? - ==结束示例== - + ==例子结束== + 现在,进行问题生成: 工具描述:{action_description} 用户历史提问: @@ -76,16 +76,10 @@ class Recommend(CorePattern): else: history_questions = kwargs["history_questions"] - if "shown_questions" not in kwargs or not kwargs["shown_questions"]: - shown_questions = "[Empty]" - else: - shown_questions = kwargs["shown_questions"] - user_input = self.user_prompt.format( action_description=action_description, history_questions=history_questions, recent_question=kwargs["recent_question"], - shown_questions=shown_questions, user_preference=user_preference, ) @@ -95,7 +89,7 @@ class Recommend(CorePattern): ] result = "" - async for chunk in ReasoningLLM().call(task_id, messages, streaming=False, temperature=1.0): + async for chunk in ReasoningLLM().call(task_id, messages, streaming=False, temperature=0.7, result_only=True): result += chunk return result diff --git a/apps/llm/reasoning.py b/apps/llm/reasoning.py index 568735e49..bc32b345d 100644 --- a/apps/llm/reasoning.py +++ b/apps/llm/reasoning.py @@ -57,7 +57,7 @@ class ReasoningLLM(metaclass=Singleton): async def call(self, task_id: str, messages: list[dict[str, str]], # noqa: C901, PLR0912 max_tokens: Optional[int] = None, temperature: Optional[float] = None, - *, streaming: bool = True) -> AsyncGenerator[str, None]: + *, streaming: bool = True, result_only: bool = True) -> AsyncGenerator[str, None]: """调用大模型,分为流式和非流式两种""" input_tokens = self._calculate_token_length(messages) try: @@ -80,40 +80,80 @@ class ReasoningLLM(metaclass=Singleton): stream=True, ) # type: ignore[] + reasoning_content = "" result = "" + is_first_chunk = True + is_reasoning = True + reasoning_type = "" + async for chunk in stream: - content = chunk.choices[0].delta.content or "" + # 当前Chunk内的信息 reason = "" - if hasattr(chunk.choices[0].delta, "reasoning_content"): - reason = chunk.choices[0].delta.reasoning_content or "" - - if reason: - if "" not in result: - reason = "" + reason - result += reason - if streaming: - yield reason - continue - else: - if "" in result and "" not in result: - result += "" - if streaming: - yield "" - - for token in REASONING_BEGIN_TOKEN: - if token in content: - content = content.replace(token, "") - - for token in REASONING_END_TOKEN: - if token in content: - content = content.replace(token, "") - - result += content - if streaming: - yield content + text = "" + + if is_first_chunk: + if hasattr(chunk.choices[0].delta, "reasoning_content"): + reason = "" + chunk.choices[0].delta.reasoning_content or "" + reasoning_type = "args" + is_reasoning = True + else: + for token in REASONING_BEGIN_TOKEN: + if token in chunk.choices[0].delta.content or "": + reason = "" + chunk.choices[0].delta.content or "" + reasoning_type = "tokens" + is_reasoning = True + break + + # 当前已经不是第一个Chunk了 + is_first_chunk = False + + # 当前处于推理状态 + if not is_first_chunk and is_reasoning: + # 如果推理内容用特殊参数传递 + if reasoning_type == "args": + # 还在推理 + if hasattr(chunk.choices[0].delta, "reasoning_content"): + reason = chunk.choices[0].delta.reasoning_content or "" + # 推理结束 + else: + is_reasoning = False + reason = "" + text = chunk.choices[0].delta.content or "" + + # 如果推理内容用特殊token传递 + elif reasoning_type == "tokens": + # 结束推理 + for token in REASONING_END_TOKEN: + if token in chunk.choices[0].delta.content or "": + is_reasoning = False + reason, text = (chunk.choices[0].delta.content or "").split(token) + reason += "" + break + # 还在推理 + if is_reasoning: + reason = chunk.choices[0].delta.content or "" + + # 当前是正常问答 + if not is_reasoning: + text = chunk.choices[0].delta.content or "" + + # 推送消息 + if streaming: + # 如果需要推送推理内容 + if reason and not result_only: + yield reason + + # 推送text + yield text + + # 整理结果 + reasoning_content += reason + result += text if not streaming: + if not result_only: + yield reasoning_content yield result output_tokens = self._calculate_token_length([{"role": "assistant", "content": result}], pure_text=True) diff --git a/apps/main.py b/apps/main.py index fe4bab93e..704fb1719 100644 --- a/apps/main.py +++ b/apps/main.py @@ -31,7 +31,7 @@ app = FastAPI(docs_url=None, redoc_url=None) # 定义FastAPI全局中间件 app.add_middleware( CORSMiddleware, - allow_origins=[config["WEB_FRONT_URL"]], + allow_origins=[config["DOMAIN"]], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/apps/service/suggestion.py b/apps/service/suggestion.py index 53293e11c..3f86252a9 100644 --- a/apps/service/suggestion.py +++ b/apps/service/suggestion.py @@ -48,7 +48,6 @@ async def plan_next_flow(user_sub: str, task_id: str, queue: MessageQueue, user_ Question: {task.record.content.question} Answer: {task.record.content.answer} """) - generated_questions = "" records = await RecordManager.query_record_by_conversation_id(user_sub, task.record.conversation_id, HISTORY_QUESTIONS_NUM) last_n_questions = "" @@ -64,9 +63,7 @@ async def plan_next_flow(user_sub: str, task_id: str, queue: MessageQueue, user_ history_questions=last_n_questions, recent_question=current_record, user_preference=user_domain, - shown_questions=generated_questions, ) - generated_questions += f"{question}\n" content = SuggestContent( question=question, plugin_id="", @@ -101,9 +98,7 @@ async def plan_next_flow(user_sub: str, task_id: str, queue: MessageQueue, user_ history_questions=last_n_questions, recent_question=current_record, user_preference=str(user_domain), - shown_questions=generated_questions, ) - generated_questions += f"{rewrite_question}\n" content = SuggestContent( plugin_id=plugin_id, @@ -153,9 +148,7 @@ async def plan_next_flow(user_sub: str, task_id: str, queue: MessageQueue, user_ history_questions=last_n_questions, recent_question=current_record, user_preference=str(user_domain), - shown_questions=generated_questions, ) - generated_questions += f"{rewrite_question}\n" content = SuggestContent( plugin_id=next_flow_plugin_id, flow_id=next_flow.id, -- Gitee