mirror of
https://github.com/Zippland/Snap-Solver.git
synced 2026-01-19 01:21:13 +08:00
添加Claude 4模型支持
This commit is contained in:
@@ -27,6 +27,22 @@
|
||||
}
|
||||
},
|
||||
"models": {
|
||||
"claude-opus-4-20250514": {
|
||||
"name": "Claude 4 Opus",
|
||||
"provider": "anthropic",
|
||||
"supportsMultimodal": true,
|
||||
"isReasoning": true,
|
||||
"version": "20250514",
|
||||
"description": "最强大的Claude 4 Opus模型,支持图像理解和深度思考过程"
|
||||
},
|
||||
"claude-sonnet-4-20250514": {
|
||||
"name": "Claude 4 Sonnet",
|
||||
"provider": "anthropic",
|
||||
"supportsMultimodal": true,
|
||||
"isReasoning": true,
|
||||
"version": "20250514",
|
||||
"description": "高性能的Claude 4 Sonnet模型,支持图像理解和思考过程"
|
||||
},
|
||||
"claude-3-7-sonnet-20250219": {
|
||||
"name": "Claude 3.7 Sonnet",
|
||||
"provider": "anthropic",
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
import json
|
||||
import requests
|
||||
from typing import Generator
|
||||
from typing import Generator, Optional
|
||||
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)
|
||||
def __init__(self, api_key, temperature=0.7, system_prompt=None, language=None, api_base_url=None, model_identifier=None):
|
||||
super().__init__(api_key, temperature, system_prompt or self.get_default_system_prompt(), language or "en")
|
||||
# 设置API基础URL,默认为Anthropic官方API
|
||||
self.api_base_url = api_base_url or "https://api.anthropic.com/v1"
|
||||
# 设置模型标识符,支持动态选择
|
||||
self.model_identifier = model_identifier or "claude-3-7-sonnet-20250219"
|
||||
# 初始化推理配置
|
||||
self.reasoning_config = None
|
||||
# 初始化最大Token数
|
||||
self.max_tokens = None
|
||||
|
||||
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:
|
||||
@@ -18,9 +24,9 @@ class AnthropicModel(BaseModel):
|
||||
5. If there are multiple approaches, explain the most efficient one first"""
|
||||
|
||||
def get_model_identifier(self) -> str:
|
||||
return "claude-3-7-sonnet-20250219"
|
||||
return self.model_identifier
|
||||
|
||||
def analyze_text(self, text: str, proxies: dict = None) -> Generator[dict, None, None]:
|
||||
def analyze_text(self, text: str, proxies: Optional[dict] = None) -> Generator[dict, None, None]:
|
||||
"""Stream Claude's response for text analysis"""
|
||||
try:
|
||||
yield {"status": "started"}
|
||||
@@ -190,7 +196,7 @@ class AnthropicModel(BaseModel):
|
||||
"error": f"Streaming error: {str(e)}"
|
||||
}
|
||||
|
||||
def analyze_image(self, image_data, proxies=None):
|
||||
def analyze_image(self, image_data, proxies: Optional[dict] = None):
|
||||
yield {"status": "started"}
|
||||
|
||||
api_key = self.api_key
|
||||
@@ -212,7 +218,7 @@ class AnthropicModel(BaseModel):
|
||||
max_tokens = self.max_tokens
|
||||
|
||||
payload = {
|
||||
'model': 'claude-3-7-sonnet-20250219',
|
||||
'model': self.get_model_identifier(),
|
||||
'stream': True,
|
||||
'max_tokens': max_tokens,
|
||||
'temperature': 1,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Dict, Type, Any
|
||||
from typing import Dict, Type, Any, Optional
|
||||
import json
|
||||
import os
|
||||
import importlib
|
||||
@@ -81,7 +81,7 @@ class ModelFactory:
|
||||
|
||||
@classmethod
|
||||
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:
|
||||
system_prompt: Optional[str] = None, language: Optional[str] = None, api_base_url: Optional[str] = None) -> BaseModel:
|
||||
"""
|
||||
Create a model instance based on the model name.
|
||||
|
||||
@@ -129,6 +129,16 @@ class ModelFactory:
|
||||
temperature=temperature,
|
||||
system_prompt=system_prompt
|
||||
)
|
||||
# 对于Anthropic模型,需要传递model_identifier参数
|
||||
elif 'claude' in model_name.lower() or 'anthropic' in model_name.lower():
|
||||
return model_class(
|
||||
api_key=api_key,
|
||||
temperature=temperature,
|
||||
system_prompt=system_prompt,
|
||||
language=language,
|
||||
api_base_url=api_base_url,
|
||||
model_identifier=model_name
|
||||
)
|
||||
else:
|
||||
# 其他模型仅传递标准参数
|
||||
return model_class(
|
||||
@@ -181,7 +191,7 @@ class ModelFactory:
|
||||
@classmethod
|
||||
def register_model(cls, model_name: str, model_class: Type[BaseModel],
|
||||
is_multimodal: bool = False, is_reasoning: bool = False,
|
||||
display_name: str = None, description: str = None) -> None:
|
||||
display_name: Optional[str] = None, description: Optional[str] = None) -> None:
|
||||
"""
|
||||
Register a new model type with the factory.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user