完善截图和分析流程

This commit is contained in:
Zylan
2025-03-06 14:45:52 +08:00
parent 33cf23d462
commit 80d79375de
8 changed files with 1352 additions and 685 deletions

View File

@@ -18,13 +18,12 @@ class ClaudeModel(BaseModel):
def analyze_text(self, text: str, proxies: dict = None) -> Generator[dict, None, None]:
"""Stream Claude's response for text analysis"""
try:
# Initial status
yield {"status": "started", "content": ""}
api_key = self.api_key.strip()
yield {"status": "started"}
api_key = self.api_key
if api_key.startswith('Bearer '):
api_key = api_key[7:]
headers = {
'x-api-key': api_key,
'anthropic-version': '2023-06-01',
@@ -105,6 +104,16 @@ class ClaudeModel(BaseModel):
"status": "thinking",
"content": thinking_content
}
# 处理新的extended_thinking格式
elif data.get('type') == 'extended_thinking_delta':
if 'delta' in data and 'text' in data['delta']:
thinking_chunk = data['delta']['text']
thinking_content += thinking_chunk
yield {
"status": "thinking",
"content": thinking_content
}
elif data.get('type') == 'message_stop':
if thinking_content:
@@ -135,130 +144,132 @@ class ClaudeModel(BaseModel):
"error": f"Streaming error: {str(e)}"
}
def analyze_image(self, image_data: str, proxies: dict = None) -> Generator[dict, None, None]:
"""Stream Claude's response for image analysis"""
try:
# Initial status
yield {"status": "started", "content": ""}
api_key = self.api_key.strip()
if api_key.startswith('Bearer '):
api_key = api_key[7:]
headers = {
'x-api-key': api_key,
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
'accept': 'application/json',
}
payload = {
'model': self.get_model_identifier(),
'stream': True,
'max_tokens': 8192,
'temperature': 1,
'system': self.system_prompt,
'thinking': {
'type': 'enabled',
'budget_tokens': 4096
},
'messages': [{
'role': 'user',
'content': [
{
'type': 'image',
'source': {
'type': 'base64',
'media_type': 'image/png',
'data': image_data
}
},
{
'type': 'text',
'text': "Please analyze this question and provide a detailed solution. If you see multiple questions, focus on solving them one at a time."
}
]
}]
}
response = requests.post(
'https://api.anthropic.com/v1/messages',
headers=headers,
json=payload,
stream=True,
proxies=proxies,
timeout=60
)
if response.status_code != 200:
error_msg = f'API error: {response.status_code}'
try:
error_data = response.json()
if 'error' in error_data:
error_msg += f" - {error_data['error']['message']}"
except:
error_msg += f" - {response.text}"
yield {"status": "error", "error": error_msg}
return
thinking_content = ""
response_buffer = ""
def analyze_image(self, image_data, prompt, socket=None, proxies=None):
yield {"status": "started"}
api_key = self.api_key
if api_key.startswith('Bearer '):
api_key = api_key[7:]
for chunk in response.iter_lines():
if not chunk:
headers = {
'x-api-key': api_key,
'anthropic-version': '2023-06-01',
'content-type': 'application/json'
}
payload = {
'model': 'claude-3-7-sonnet-20250219',
'stream': True,
'max_tokens': 8192,
'temperature': 1,
'thinking': {
'type': 'enabled',
'budget_tokens': 4096
},
'system': "You are a helpful AI assistant that specializes in solving math problems. You should provide step-by-step solutions and explanations for any math problem presented to you. If you're given an image, analyze any mathematical content in it and provide a detailed solution.",
'messages': [{
'role': 'user',
'content': [
{
'type': 'image',
'source': {
'type': 'base64',
'media_type': 'image/png',
'data': image_data
}
},
{
'type': 'text',
'text': "Please analyze this question and provide a detailed solution. If you see multiple questions, focus on solving them one at a time."
}
]
}]
}
response = requests.post(
'https://api.anthropic.com/v1/messages',
headers=headers,
json=payload,
stream=True,
proxies=proxies,
timeout=60
)
if response.status_code != 200:
error_msg = f'API error: {response.status_code}'
try:
error_data = response.json()
if 'error' in error_data:
error_msg += f" - {error_data['error']['message']}"
except:
error_msg += f" - {response.text}"
yield {"status": "error", "error": error_msg}
return
thinking_content = ""
response_buffer = ""
for chunk in response.iter_lines():
if not chunk:
continue
try:
chunk_str = chunk.decode('utf-8')
if not chunk_str.startswith('data: '):
continue
try:
chunk_str = chunk.decode('utf-8')
if not chunk_str.startswith('data: '):
continue
chunk_str = chunk_str[6:]
data = json.loads(chunk_str)
chunk_str = chunk_str[6:]
data = json.loads(chunk_str)
if data.get('type') == 'content_block_delta':
if 'delta' in data:
if 'text' in data['delta']:
text_chunk = data['delta']['text']
yield {
"status": "streaming",
"content": text_chunk
}
response_buffer += text_chunk
elif 'thinking' in data['delta']:
thinking_chunk = data['delta']['thinking']
thinking_content += thinking_chunk
yield {
"status": "thinking",
"content": thinking_content
}
elif data.get('type') == 'message_stop':
if thinking_content:
if data.get('type') == 'content_block_delta':
if 'delta' in data:
if 'text' in data['delta']:
text_chunk = data['delta']['text']
yield {
"status": "thinking_complete",
"status": "streaming",
"content": text_chunk
}
response_buffer += text_chunk
elif 'thinking' in data['delta']:
thinking_chunk = data['delta']['thinking']
thinking_content += thinking_chunk
yield {
"status": "thinking",
"content": thinking_content
}
# 处理新的extended_thinking格式
elif data.get('type') == 'extended_thinking_delta':
if 'delta' in data and 'text' in data['delta']:
thinking_chunk = data['delta']['text']
thinking_content += thinking_chunk
yield {
"status": "completed",
"content": ""
"status": "thinking",
"content": thinking_content
}
elif data.get('type') == 'error':
error_msg = data.get('error', {}).get('message', 'Unknown error')
elif data.get('type') == 'message_stop':
if thinking_content:
yield {
"status": "error",
"error": error_msg
"status": "thinking_complete",
"content": thinking_content
}
break
except json.JSONDecodeError as e:
print(f"JSON decode error: {str(e)}")
continue
except Exception as e:
yield {
"status": "error",
"error": f"Streaming error: {str(e)}"
}
yield {
"status": "completed",
"content": response_buffer
}
elif data.get('type') == 'error':
error_message = data.get('error', {}).get('message', 'Unknown error')
yield {
"status": "error",
"error": error_message
}
except Exception as e:
yield {
"status": "error",
"error": f"Error processing response: {str(e)}"
}
break

View File

@@ -132,7 +132,7 @@ class GPT4oModel(BaseModel):
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{image_data}",
"url": image_data if image_data.startswith('data:') else f"data:image/png;base64,{image_data}",
"detail": "high"
}
},