Files
dsProject/dsLightRag/JiMeng/T2_DoubaoShiTu.py
2025-08-20 10:17:04 +08:00

72 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
import requests
from JiMeng.Kit.VolcanoConst import VOLCANO_API_KEY
# API请求URL
url = 'https://ark.cn-beijing.volces.com/api/v3/chat/completions'
# 请求头
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {VOLCANO_API_KEY}'
}
# 请求体数据
payload = {
'model': 'doubao-1-5-vision-pro-32k-250115',
'messages': [
{
'content': [
{
'image_url': {
'url': 'https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/Text2Img.jpg'
},
'type': 'image_url'
},
{
'text': '以这张图片为首帧帮我生成一个5秒的视频提示词。提示词包括镜号、运镜、画面内容',
'type': 'text'
}
],
'role': 'user'
}
]
}
# 发送POST请求
try:
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
# 检查响应状态码
response.raise_for_status()
# 解析响应数据
result = response.json()
print('请求成功:')
# 提取choices[0].message.content的内容
if 'choices' in result and len(result['choices']) > 0:
choice = result['choices'][0]
if 'message' in choice and 'content' in choice['message']:
content = choice['message']['content']
print('提取的内容:')
print(content)
# 这里可以添加对content的进一步处理逻辑
# 例如保存到文件、解析表格内容等
else:
print('响应中未找到message或content字段')
else:
print('响应中未找到choices字段或choices为空')
except requests.exceptions.RequestException as e:
print(f'请求发生错误: {e}')
if hasattr(e, 'response') and e.response is not None:
print(f'错误响应状态码: {e.response.status_code}')
print(f'错误响应内容: {e.response.text}')
except json.JSONDecodeError:
print('响应解析失败: 返回的内容不是有效的JSON格式')
print(f'响应内容: {response.text}')