mirror of
https://github.com/Zippland/Snap-Solver.git
synced 2026-02-26 16:16:58 +08:00
支持自定义api接口地址
This commit is contained in:
@@ -4,6 +4,11 @@ from typing import Generator
|
||||
from .base import BaseModel
|
||||
|
||||
class AnthropicModel(BaseModel):
|
||||
def __init__(self, api_key, temperature=0.7, system_prompt=None, language=None, api_base_url=None):
|
||||
super().__init__(api_key, temperature, system_prompt, language)
|
||||
# 设置API基础URL,默认为Anthropic官方API
|
||||
self.api_base_url = api_base_url or "https://api.anthropic.com/v1"
|
||||
|
||||
def get_default_system_prompt(self) -> str:
|
||||
return """You are an expert at analyzing questions and providing detailed solutions. When presented with an image of a question:
|
||||
1. First read and understand the question carefully
|
||||
@@ -82,8 +87,11 @@ class AnthropicModel(BaseModel):
|
||||
|
||||
print(f"Debug - 推理配置: max_tokens={max_tokens}, thinking={payload.get('thinking', payload.get('speed_mode', 'default'))}")
|
||||
|
||||
# 使用配置的API基础URL
|
||||
api_endpoint = f"{self.api_base_url}/messages"
|
||||
|
||||
response = requests.post(
|
||||
'https://api.anthropic.com/v1/messages',
|
||||
api_endpoint,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
stream=True,
|
||||
@@ -257,8 +265,11 @@ class AnthropicModel(BaseModel):
|
||||
|
||||
print(f"Debug - 图像分析推理配置: max_tokens={max_tokens}, thinking={payload.get('thinking', payload.get('speed_mode', 'default'))}")
|
||||
|
||||
# 使用配置的API基础URL
|
||||
api_endpoint = f"{self.api_base_url}/messages"
|
||||
|
||||
response = requests.post(
|
||||
'https://api.anthropic.com/v1/messages',
|
||||
api_endpoint,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
stream=True,
|
||||
|
||||
@@ -80,7 +80,8 @@ class ModelFactory:
|
||||
print(f"无法加载基础Mathpix工具: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
def create_model(cls, model_name: str, api_key: str, temperature: float = 0.7, system_prompt: str = None, language: str = None) -> BaseModel:
|
||||
def create_model(cls, model_name: str, api_key: str, temperature: float = 0.7,
|
||||
system_prompt: str = None, language: str = None, api_base_url: str = None) -> BaseModel:
|
||||
"""
|
||||
Create a model instance based on the model name.
|
||||
|
||||
@@ -90,6 +91,7 @@ class ModelFactory:
|
||||
temperature: The temperature to use for generation
|
||||
system_prompt: The system prompt to use
|
||||
language: The preferred language for responses
|
||||
api_base_url: The base URL for API requests
|
||||
|
||||
Returns:
|
||||
A model instance
|
||||
@@ -107,7 +109,8 @@ class ModelFactory:
|
||||
temperature=temperature,
|
||||
system_prompt=system_prompt,
|
||||
language=language,
|
||||
model_name=model_name
|
||||
model_name=model_name,
|
||||
api_base_url=api_base_url
|
||||
)
|
||||
# 对于阿里巴巴模型,也需要传递正确的模型名称
|
||||
elif 'qwen' in model_name.lower() or 'qvq' in model_name.lower() or 'alibaba' in model_name.lower():
|
||||
@@ -116,7 +119,8 @@ class ModelFactory:
|
||||
temperature=temperature,
|
||||
system_prompt=system_prompt,
|
||||
language=language,
|
||||
model_name=model_name
|
||||
model_name=model_name,
|
||||
api_base_url=api_base_url
|
||||
)
|
||||
# 对于Mathpix模型,不传递language参数
|
||||
elif model_name == 'mathpix':
|
||||
@@ -131,7 +135,8 @@ class ModelFactory:
|
||||
api_key=api_key,
|
||||
temperature=temperature,
|
||||
system_prompt=system_prompt,
|
||||
language=language
|
||||
language=language,
|
||||
api_base_url=api_base_url
|
||||
)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -11,7 +11,7 @@ class GoogleModel(BaseModel):
|
||||
支持Gemini 2.5 Pro等模型,可处理文本和图像输入
|
||||
"""
|
||||
|
||||
def __init__(self, api_key: str, temperature: float = 0.7, system_prompt: str = None, language: str = None, model_name: str = None):
|
||||
def __init__(self, api_key: str, temperature: float = 0.7, system_prompt: str = None, language: str = None, model_name: str = None, api_base_url: str = None):
|
||||
"""
|
||||
初始化Google模型
|
||||
|
||||
@@ -21,13 +21,20 @@ class GoogleModel(BaseModel):
|
||||
system_prompt: 系统提示词
|
||||
language: 首选语言
|
||||
model_name: 指定具体模型名称,如不指定则使用默认值
|
||||
api_base_url: API基础URL,用于设置自定义API端点
|
||||
"""
|
||||
super().__init__(api_key, temperature, system_prompt, language)
|
||||
self.model_name = model_name or self.get_model_identifier()
|
||||
self.max_tokens = 8192 # 默认最大输出token数
|
||||
self.api_base_url = api_base_url
|
||||
|
||||
# 配置Google API
|
||||
genai.configure(api_key=api_key)
|
||||
if api_base_url:
|
||||
# 如果提供了自定义API基础URL,设置genai的api_url
|
||||
genai.configure(api_key=api_key, transport="rest", client_options={"api_endpoint": api_base_url})
|
||||
else:
|
||||
# 使用默认API端点
|
||||
genai.configure(api_key=api_key)
|
||||
|
||||
def get_default_system_prompt(self) -> str:
|
||||
return """You are an expert at analyzing questions and providing detailed solutions. When presented with an image of a question:
|
||||
|
||||
@@ -4,6 +4,11 @@ from openai import OpenAI
|
||||
from .base import BaseModel
|
||||
|
||||
class OpenAIModel(BaseModel):
|
||||
def __init__(self, api_key, temperature=0.7, system_prompt=None, language=None, api_base_url=None):
|
||||
super().__init__(api_key, temperature, system_prompt, language)
|
||||
# 设置API基础URL,默认为OpenAI官方API
|
||||
self.api_base_url = api_base_url
|
||||
|
||||
def get_default_system_prompt(self) -> str:
|
||||
return """You are an expert at analyzing questions and providing detailed solutions. When presented with an image of a question:
|
||||
1. First read and understand the question carefully
|
||||
@@ -35,8 +40,11 @@ class OpenAIModel(BaseModel):
|
||||
if 'https' in proxies:
|
||||
os.environ['https_proxy'] = proxies['https']
|
||||
|
||||
# Initialize OpenAI client
|
||||
client = OpenAI(api_key=self.api_key)
|
||||
# Initialize OpenAI client with base_url if provided
|
||||
if self.api_base_url:
|
||||
client = OpenAI(api_key=self.api_key, base_url=self.api_base_url)
|
||||
else:
|
||||
client = OpenAI(api_key=self.api_key)
|
||||
|
||||
# Prepare messages
|
||||
messages = [
|
||||
@@ -123,8 +131,11 @@ class OpenAIModel(BaseModel):
|
||||
if 'https' in proxies:
|
||||
os.environ['https_proxy'] = proxies['https']
|
||||
|
||||
# Initialize OpenAI client
|
||||
client = OpenAI(api_key=self.api_key)
|
||||
# Initialize OpenAI client with base_url if provided
|
||||
if self.api_base_url:
|
||||
client = OpenAI(api_key=self.api_key, base_url=self.api_base_url)
|
||||
else:
|
||||
client = OpenAI(api_key=self.api_key)
|
||||
|
||||
# 使用系统提供的系统提示词,不再自动添加语言指令
|
||||
system_prompt = self.system_prompt
|
||||
|
||||
Reference in New Issue
Block a user