From cf2a0cf8c253651bf0f8328014b2858da41c5c33 Mon Sep 17 00:00:00 2001 From: Pollo Date: Thu, 20 Nov 2025 16:56:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20google=E5=92=8Copenai=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=90=86=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/agent/__init__.py | 47 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/app/agent/__init__.py b/app/agent/__init__.py index a92a7392..69953ec8 100644 --- a/app/agent/__init__.py +++ b/app/agent/__init__.py @@ -73,15 +73,43 @@ class MoviePilotAgent: raise ValueError("未配置 LLM_API_KEY") if provider == "google": + import os + from contextlib import contextmanager from langchain_google_genai import ChatGoogleGenerativeAI - return ChatGoogleGenerativeAI( - model=settings.LLM_MODEL, - google_api_key=api_key, - max_retries=3, - temperature=settings.LLM_TEMPERATURE, - streaming=True, - callbacks=[self.callback_handler] - ) + + # 使用临时环境变量配置代理 + @contextmanager + def _temp_proxy_env(): + """临时设置代理环境变量的上下文管理器""" + old_http = os.environ.get("HTTP_PROXY") + old_https = os.environ.get("HTTPS_PROXY") + try: + if settings.PROXY_HOST: + os.environ["HTTP_PROXY"] = settings.PROXY_HOST + os.environ["HTTPS_PROXY"] = settings.PROXY_HOST + yield + finally: + # 恢复原始环境变量 + if old_http is not None: + os.environ["HTTP_PROXY"] = old_http + elif "HTTP_PROXY" in os.environ: + del os.environ["HTTP_PROXY"] + + if old_https is not None: + os.environ["HTTPS_PROXY"] = old_https + elif "HTTPS_PROXY" in os.environ: + del os.environ["HTTPS_PROXY"] + + # 在临时环境变量中初始化 ChatGoogleGenerativeAI + with _temp_proxy_env(): + return ChatGoogleGenerativeAI( + model=settings.LLM_MODEL, + google_api_key=api_key, + max_retries=3, + temperature=settings.LLM_TEMPERATURE, + streaming=True, + callbacks=[self.callback_handler] + ) elif provider == "deepseek": from langchain_deepseek import ChatDeepSeek return ChatDeepSeek( @@ -103,7 +131,8 @@ class MoviePilotAgent: temperature=settings.LLM_TEMPERATURE, streaming=True, callbacks=[self.callback_handler], - stream_usage=True + stream_usage=True, + openai_proxy=settings.PROXY_HOST ) def _initialize_tools(self) -> List: From 84ef5705e745a966a24ffb6eff20590652566c76 Mon Sep 17 00:00:00 2001 From: Pollo Date: Thu, 20 Nov 2025 17:05:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20google=E4=B8=B4=E6=97=B6=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/agent/__init__.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/app/agent/__init__.py b/app/agent/__init__.py index 69953ec8..96ef40f7 100644 --- a/app/agent/__init__.py +++ b/app/agent/__init__.py @@ -1,6 +1,7 @@ """MoviePilot AI智能体实现""" import asyncio +import threading from typing import Dict, List, Any from langchain.agents import AgentExecutor, create_openai_tools_agent @@ -20,6 +21,9 @@ from app.helper.message import MessageHelper from app.log import logger from app.schemas import Notification +# 用于保护环境变量修改的线程锁 +_env_lock = threading.Lock() + class AgentChain(ChainBase): pass @@ -77,28 +81,29 @@ class MoviePilotAgent: from contextlib import contextmanager from langchain_google_genai import ChatGoogleGenerativeAI - # 使用临时环境变量配置代理 + # 使用线程锁保护的临时环境变量配置 @contextmanager def _temp_proxy_env(): - """临时设置代理环境变量的上下文管理器""" - old_http = os.environ.get("HTTP_PROXY") - old_https = os.environ.get("HTTPS_PROXY") - try: - if settings.PROXY_HOST: - os.environ["HTTP_PROXY"] = settings.PROXY_HOST - os.environ["HTTPS_PROXY"] = settings.PROXY_HOST - yield - finally: - # 恢复原始环境变量 - if old_http is not None: - os.environ["HTTP_PROXY"] = old_http - elif "HTTP_PROXY" in os.environ: - del os.environ["HTTP_PROXY"] + """线程安全的临时设置代理环境变量的上下文管理器""" + with _env_lock: + old_http = os.environ.get("HTTP_PROXY") + old_https = os.environ.get("HTTPS_PROXY") + try: + if settings.PROXY_HOST: + os.environ["HTTP_PROXY"] = settings.PROXY_HOST + os.environ["HTTPS_PROXY"] = settings.PROXY_HOST + yield + finally: + # 恢复原始环境变量 + if old_http is not None: + os.environ["HTTP_PROXY"] = old_http + elif "HTTP_PROXY" in os.environ: + del os.environ["HTTP_PROXY"] - if old_https is not None: - os.environ["HTTPS_PROXY"] = old_https - elif "HTTPS_PROXY" in os.environ: - del os.environ["HTTPS_PROXY"] + if old_https is not None: + os.environ["HTTPS_PROXY"] = old_https + elif "HTTPS_PROXY" in os.environ: + del os.environ["HTTPS_PROXY"] # 在临时环境变量中初始化 ChatGoogleGenerativeAI with _temp_proxy_env():