Files
PastpaperMaster/backend/app/services/llm_clients.py
Zhao 7a09167261 Initial commit: PastPaper Master full stack
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-21 12:27:47 +07:00

75 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import httpx
from openai import OpenAI
from app.config import get_settings
_TIMEOUT = httpx.Timeout(connect=10, read=300, write=60, pool=10)
_gpt_client: OpenAI | None = None
_qwen_client: OpenAI | None = None
_gemini_flash_client: OpenAI | None = None
_gemini_lite_client: OpenAI | None = None
_deepseek_client: OpenAI | None = None
def get_gpt_client() -> OpenAI:
"""laozhang API — gpt-4o / gpt-4o-mini"""
global _gpt_client
if _gpt_client is None:
s = get_settings()
_gpt_client = OpenAI(
base_url=s.laozhang_base_url,
api_key=s.laozhang_api_key,
)
return _gpt_client
def get_qwen_client() -> OpenAI:
"""DashScope — qwen-plus"""
global _qwen_client
if _qwen_client is None:
s = get_settings()
_qwen_client = OpenAI(
base_url=s.dashscope_base_url,
api_key=s.dashscope_api_key,
)
return _qwen_client
def get_vision_client() -> OpenAI:
"""Google Gemini 官方 API视觉用于拆题+OCR— 部署在新加坡可用"""
global _gemini_flash_client
if _gemini_flash_client is None:
s = get_settings()
_gemini_flash_client = OpenAI(
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
api_key=s.google_gemini_api_key,
timeout=_TIMEOUT,
)
return _gemini_flash_client
def get_gemini_lite_client() -> OpenAI:
"""laozhang — gemini-3.1-flash-lite-preview轻量用于 AI trio"""
global _gemini_lite_client
if _gemini_lite_client is None:
s = get_settings()
_gemini_lite_client = OpenAI(
base_url=s.laozhang_base_url,
api_key=s.laozhang_api_key,
timeout=_TIMEOUT,
)
return _gemini_lite_client
def get_deepseek_client() -> OpenAI:
"""DeepSeek — deepseek-chat用于 AI trio"""
global _deepseek_client
if _deepseek_client is None:
s = get_settings()
_deepseek_client = OpenAI(
base_url=s.deepseek_base_url,
api_key=s.deepseek_api_key,
timeout=_TIMEOUT,
)
return _deepseek_client