mirror of
https://github.com/Zippland/Snap-Solver.git
synced 2026-02-16 08:16:20 +08:00
ocr
This commit is contained in:
@@ -16,6 +16,76 @@ class DeepSeekModel(BaseModel):
|
||||
def get_model_identifier(self) -> str:
|
||||
return "deepseek-reasoner"
|
||||
|
||||
def analyze_text(self, text: str, proxies: dict = None) -> Generator[dict, None, None]:
|
||||
"""Stream DeepSeek's response for text analysis"""
|
||||
try:
|
||||
# Initial status
|
||||
yield {"status": "started", "content": ""}
|
||||
|
||||
# Configure client with proxy if needed
|
||||
client_args = {
|
||||
"api_key": self.api_key,
|
||||
"base_url": "https://api.deepseek.com"
|
||||
}
|
||||
|
||||
if proxies:
|
||||
session = requests.Session()
|
||||
session.proxies = proxies
|
||||
client_args["http_client"] = session
|
||||
|
||||
client = OpenAI(**client_args)
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=self.get_model_identifier(),
|
||||
messages=[{
|
||||
'role': 'system',
|
||||
'content': self.system_prompt
|
||||
}, {
|
||||
'role': 'user',
|
||||
'content': text
|
||||
}],
|
||||
stream=True
|
||||
)
|
||||
|
||||
for chunk in response:
|
||||
try:
|
||||
if hasattr(chunk.choices[0].delta, 'reasoning_content'):
|
||||
content = chunk.choices[0].delta.reasoning_content
|
||||
if content:
|
||||
yield {
|
||||
"status": "streaming",
|
||||
"content": content
|
||||
}
|
||||
elif hasattr(chunk.choices[0].delta, 'content'):
|
||||
content = chunk.choices[0].delta.content
|
||||
if content:
|
||||
yield {
|
||||
"status": "streaming",
|
||||
"content": content
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Chunk processing error: {str(e)}")
|
||||
continue
|
||||
|
||||
# Send completion status
|
||||
yield {
|
||||
"status": "completed",
|
||||
"content": ""
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
if "invalid_api_key" in error_msg.lower():
|
||||
error_msg = "Invalid API key provided"
|
||||
elif "rate_limit" in error_msg.lower():
|
||||
error_msg = "Rate limit exceeded. Please try again later."
|
||||
|
||||
yield {
|
||||
"status": "error",
|
||||
"error": f"DeepSeek API error: {error_msg}"
|
||||
}
|
||||
|
||||
def analyze_image(self, image_data: str, proxies: dict = None) -> Generator[dict, None, None]:
|
||||
"""Stream DeepSeek's response for image analysis"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user