Files
ailine/backend/app/agent/llm_factory.py
root 8b354b7ccc
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 47m14s
重构代码,统一config配置
2026-04-21 11:02:16 +08:00

57 lines
1.6 KiB
Python

# app/llm_factory.py
import os
from ..config import ZHIPUAI_API_KEY, DEEPSEEK_API_KEY, VLLM_BASE_URL, LLAMACPP_API_KEY
from langchain_community.chat_models import ChatZhipuAI
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
class LLMFactory:
@staticmethod
def create_zhipu():
api_key = ZHIPUAI_API_KEY
if not api_key:
raise ValueError("ZHIPUAI_API_KEY not set")
return ChatZhipuAI(
model="glm-4.7-flash",
api_key=api_key,
temperature=0.1,
max_tokens=4096,
timeout=120.0,
max_retries=3,
streaming=True,
)
@staticmethod
def create_deepseek():
api_key = DEEPSEEK_API_KEY
if not api_key:
raise ValueError("DEEPSEEK_API_KEY not set")
return ChatOpenAI(
base_url="https://api.deepseek.com",
api_key=SecretStr(api_key),
model="deepseek-reasoner",
temperature=0.1,
max_tokens=4096,
timeout=60.0,
max_retries=2,
streaming=True,
)
@staticmethod
def create_local():
base_url = VLLM_BASE_URL
return ChatOpenAI(
base_url=base_url,
api_key=SecretStr(LLAMACPP_API_KEY),
model="gemma-4-E4B-it",
timeout=60.0,
max_retries=2,
streaming=True,
)
# 模型创建器映射
CREATORS = {
"local": create_local,
"deepseek": create_deepseek,
"zhipu": create_zhipu,
}