mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 16:36:53 +08:00
Merge pull request #5163 from Pollo3470/feat/ai-proxy
This commit is contained in:
+15
-34
@@ -1,7 +1,6 @@
|
|||||||
"""MoviePilot AI智能体实现"""
|
"""MoviePilot AI智能体实现"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
|
||||||
from typing import Dict, List, Any
|
from typing import Dict, List, Any
|
||||||
|
|
||||||
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
||||||
@@ -21,9 +20,6 @@ from app.helper.message import MessageHelper
|
|||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.schemas import Notification
|
from app.schemas import Notification
|
||||||
|
|
||||||
# 用于保护环境变量修改的线程锁
|
|
||||||
_env_lock = threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
class AgentChain(ChainBase):
|
class AgentChain(ChainBase):
|
||||||
pass
|
pass
|
||||||
@@ -77,36 +73,21 @@ class MoviePilotAgent:
|
|||||||
raise ValueError("未配置 LLM_API_KEY")
|
raise ValueError("未配置 LLM_API_KEY")
|
||||||
|
|
||||||
if provider == "google":
|
if provider == "google":
|
||||||
import os
|
if settings.PROXY_HOST:
|
||||||
from contextlib import contextmanager
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_google_genai import ChatGoogleGenerativeAI
|
return ChatOpenAI(
|
||||||
|
model=settings.LLM_MODEL,
|
||||||
# 使用线程锁保护的临时环境变量配置
|
api_key=api_key,
|
||||||
@contextmanager
|
max_retries=3,
|
||||||
def _temp_proxy_env():
|
base_url="https://generativelanguage.googleapis.com/v1beta/openai",
|
||||||
"""线程安全的临时设置代理环境变量的上下文管理器"""
|
temperature=settings.LLM_TEMPERATURE,
|
||||||
with _env_lock:
|
streaming=True,
|
||||||
old_http = os.environ.get("HTTP_PROXY")
|
callbacks=[self.callback_handler],
|
||||||
old_https = os.environ.get("HTTPS_PROXY")
|
stream_usage=True,
|
||||||
try:
|
openai_proxy=settings.PROXY_HOST
|
||||||
if settings.PROXY_HOST:
|
)
|
||||||
os.environ["HTTP_PROXY"] = settings.PROXY_HOST
|
else:
|
||||||
os.environ["HTTPS_PROXY"] = settings.PROXY_HOST
|
from langchain_google_genai import ChatGoogleGenerativeAI
|
||||||
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(
|
return ChatGoogleGenerativeAI(
|
||||||
model=settings.LLM_MODEL,
|
model=settings.LLM_MODEL,
|
||||||
google_api_key=api_key,
|
google_api_key=api_key,
|
||||||
|
|||||||
Reference in New Issue
Block a user