Update app.py
Browse files
app.py
CHANGED
|
@@ -550,11 +550,14 @@ def upload_image_to_wechat(image_data):
|
|
| 550 |
# logging.error(f"API调用错误: {str(e)}")
|
| 551 |
# raise
|
| 552 |
def process_long_running_task(messages, message_type='text', image_data=None):
|
|
|
|
|
|
|
|
|
|
| 553 |
try:
|
| 554 |
-
logging.info(f"
|
| 555 |
|
| 556 |
if message_type == 'image':
|
| 557 |
-
#
|
| 558 |
try:
|
| 559 |
image_content = get_image_content(image_data['media_id'])
|
| 560 |
image_base64 = base64.b64encode(image_content).decode('utf-8')
|
|
@@ -574,26 +577,31 @@ def process_long_running_task(messages, message_type='text', image_data=None):
|
|
| 574 |
}
|
| 575 |
]
|
| 576 |
|
| 577 |
-
logging.info("
|
| 578 |
image_response = client.chat.completions.create(
|
| 579 |
model="gpt-4o-mini",
|
| 580 |
messages=image_messages,
|
| 581 |
max_tokens=300,
|
| 582 |
timeout=60
|
| 583 |
)
|
| 584 |
-
logging.info("
|
| 585 |
|
|
|
|
|
|
|
|
|
|
| 586 |
return {
|
| 587 |
"type": "text",
|
| 588 |
"content": image_response.choices[0].message.content
|
| 589 |
}
|
| 590 |
|
| 591 |
except Exception as e:
|
| 592 |
-
logging.error(f"
|
| 593 |
raise
|
| 594 |
|
| 595 |
else:
|
|
|
|
| 596 |
try:
|
|
|
|
| 597 |
response = client.chat.completions.create(
|
| 598 |
model="o3-mini",
|
| 599 |
messages=messages,
|
|
@@ -602,69 +610,74 @@ def process_long_running_task(messages, message_type='text', image_data=None):
|
|
| 602 |
timeout=60
|
| 603 |
)
|
| 604 |
|
| 605 |
-
#
|
| 606 |
-
if
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
message = response.choices[0].message
|
| 610 |
-
if not message:
|
| 611 |
-
raise Exception("No message in response")
|
| 612 |
-
|
| 613 |
-
# Then check for tool calls
|
| 614 |
-
if message.tool_calls:
|
| 615 |
-
tool_call = message.tool_calls[0]
|
| 616 |
if tool_call.function.name == "generate_image":
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
"
|
| 631 |
-
"
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
}
|
| 634 |
-
)
|
| 635 |
-
|
| 636 |
-
response.raise_for_status()
|
| 637 |
-
|
| 638 |
-
# Process the response
|
| 639 |
-
for line in response.iter_lines():
|
| 640 |
-
if not line:
|
| 641 |
-
continue
|
| 642 |
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
"type": "image",
|
| 652 |
-
"media_id": media_id
|
| 653 |
-
}
|
| 654 |
-
|
| 655 |
-
# If no image generation, return the text response
|
| 656 |
return {
|
| 657 |
"type": "text",
|
| 658 |
-
"content":
|
| 659 |
}
|
| 660 |
|
|
|
|
|
|
|
|
|
|
| 661 |
except Exception as e:
|
| 662 |
-
logging.error(f"
|
| 663 |
raise
|
| 664 |
-
|
| 665 |
except Exception as e:
|
| 666 |
-
logging.error(f"API
|
| 667 |
raise
|
|
|
|
| 668 |
def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
|
| 669 |
"""
|
| 670 |
处理异步任务,支持文本对话和图片识别
|
|
|
|
| 550 |
# logging.error(f"API调用错误: {str(e)}")
|
| 551 |
# raise
|
| 552 |
def process_long_running_task(messages, message_type='text', image_data=None):
|
| 553 |
+
"""
|
| 554 |
+
处理长时间运行的任务,支持文本对话和图片识别
|
| 555 |
+
"""
|
| 556 |
try:
|
| 557 |
+
logging.info(f"开始调用AI服务,消息类型: {message_type}")
|
| 558 |
|
| 559 |
if message_type == 'image':
|
| 560 |
+
# 图片识别逻辑保持不变
|
| 561 |
try:
|
| 562 |
image_content = get_image_content(image_data['media_id'])
|
| 563 |
image_base64 = base64.b64encode(image_content).decode('utf-8')
|
|
|
|
| 577 |
}
|
| 578 |
]
|
| 579 |
|
| 580 |
+
logging.info("开始调用图像识别模型")
|
| 581 |
image_response = client.chat.completions.create(
|
| 582 |
model="gpt-4o-mini",
|
| 583 |
messages=image_messages,
|
| 584 |
max_tokens=300,
|
| 585 |
timeout=60
|
| 586 |
)
|
| 587 |
+
logging.info("图像识别完成")
|
| 588 |
|
| 589 |
+
if not image_response.choices:
|
| 590 |
+
raise Exception("图像识别服务未返回有效结果")
|
| 591 |
+
|
| 592 |
return {
|
| 593 |
"type": "text",
|
| 594 |
"content": image_response.choices[0].message.content
|
| 595 |
}
|
| 596 |
|
| 597 |
except Exception as e:
|
| 598 |
+
logging.error(f"图像识别过程中发生错误: {str(e)}")
|
| 599 |
raise
|
| 600 |
|
| 601 |
else:
|
| 602 |
+
# 处理文本消息
|
| 603 |
try:
|
| 604 |
+
logging.info("开始处理文本消息")
|
| 605 |
response = client.chat.completions.create(
|
| 606 |
model="o3-mini",
|
| 607 |
messages=messages,
|
|
|
|
| 610 |
timeout=60
|
| 611 |
)
|
| 612 |
|
| 613 |
+
# 检查是否需要生成图片
|
| 614 |
+
if response.choices[0].message.tool_calls:
|
| 615 |
+
tool_call = response.choices[0].message.tool_calls[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
if tool_call.function.name == "generate_image":
|
| 617 |
+
try:
|
| 618 |
+
logging.info("检测到图片生成请求")
|
| 619 |
+
args = json.loads(tool_call.function.arguments)
|
| 620 |
+
|
| 621 |
+
# 使用新的DALL-E 3 API进行图片生成
|
| 622 |
+
image_generation_response = requests.post(
|
| 623 |
+
"https://api1.oaipro.com/v1/images/generations",
|
| 624 |
+
headers={
|
| 625 |
+
'Content-Type': 'application/json',
|
| 626 |
+
'Authorization': f'Bearer {IMAGE_MODEL_KEY}'
|
| 627 |
+
},
|
| 628 |
+
json={
|
| 629 |
+
"model": "dall-e-3",
|
| 630 |
+
"prompt": args['prompt'],
|
| 631 |
+
"n": 1,
|
| 632 |
+
"size": "1024x1024"
|
| 633 |
+
},
|
| 634 |
+
timeout=60
|
| 635 |
+
)
|
| 636 |
+
image_generation_response.raise_for_status()
|
| 637 |
+
generation_result = image_generation_response.json()
|
| 638 |
+
|
| 639 |
+
if 'data' not in generation_result or not generation_result['data']:
|
| 640 |
+
raise Exception("图片生成服务未返回有效结果")
|
| 641 |
+
|
| 642 |
+
# 获取生成的图片URL
|
| 643 |
+
image_url = generation_result['data'][0]['url']
|
| 644 |
+
|
| 645 |
+
# 下载生成的图片
|
| 646 |
+
img_response = requests.get(image_url, timeout=30)
|
| 647 |
+
img_response.raise_for_status()
|
| 648 |
+
|
| 649 |
+
# 上传图片到微信服务器
|
| 650 |
+
media_id = upload_image_to_wechat(img_response.content)
|
| 651 |
+
|
| 652 |
+
return {
|
| 653 |
+
"type": "image",
|
| 654 |
+
"media_id": media_id
|
| 655 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
| 657 |
+
except requests.exceptions.RequestException as e:
|
| 658 |
+
logging.error(f"图片生成过程中发生网络错误: {str(e)}")
|
| 659 |
+
raise
|
| 660 |
+
except Exception as e:
|
| 661 |
+
logging.error(f"图片生成过程中发生错误: {str(e)}")
|
| 662 |
+
raise
|
| 663 |
+
|
| 664 |
+
# 返回文本响应
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
return {
|
| 666 |
"type": "text",
|
| 667 |
+
"content": response.choices[0].message.content
|
| 668 |
}
|
| 669 |
|
| 670 |
+
except requests.exceptions.RequestException as e:
|
| 671 |
+
logging.error(f"处理文本消息时发生网络错误: {str(e)}")
|
| 672 |
+
raise
|
| 673 |
except Exception as e:
|
| 674 |
+
logging.error(f"处理文本消息时发生错误: {str(e)}")
|
| 675 |
raise
|
| 676 |
+
|
| 677 |
except Exception as e:
|
| 678 |
+
logging.error(f"API调用错误: {str(e)}")
|
| 679 |
raise
|
| 680 |
+
|
| 681 |
def handle_async_task(session, task_id, messages=None, message_type='text', message_data=None):
|
| 682 |
"""
|
| 683 |
处理异步任务,支持文本对话和图片识别
|