Python基础(②豆包API)
·
请求代码doubao-1.5-vision-pro
import requests
# API 地址
api_url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
# API 密钥
api_key = ""
# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# 请求数据
data = {
"model": "doubao-1.5-vision-pro-32k-250115",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "图片主要讲了什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://ark-project.tos-cn-beijing.volces.com/doc_image/ark_demo_img_1.png"
}
}
]
}
]
}
# Clash 代理设置
proxies = {
"http": "http://127.0.0.1:7890",
"https": "http://127.0.0.1:7890"
}
try:
# 发送 POST 请求
response = requests.post(api_url, headers=headers, json=data, proxies=proxies)
# 检查响应状态码
response.raise_for_status()
# 解析响应的 JSON 数据
result = response.json()
print(result)
except requests.exceptions.HTTPError as http_err:
print(f"HTTP 错误发生: {http_err}")
print("响应文本:", response.text)
except requests.exceptions.RequestException as req_err:
print(f"请求错误发生: {req_err}")
except ValueError as json_err:
print(f"JSON 解析错误发生: {json_err}")
图片识别
import requests
# API 地址
api_url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
# API 密钥
api_key = "xxx"
# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# 代理设置 - Clash 7890
proxies = {
'http': 'http://127.0.0.1:7890',
'https': 'http://127.0.0.1:7890'
}
# 请求数据 - 图片识别
data = {
"model": "doubao-vision-pro-32k-241028",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "图片主要讲了什么?"
},
{
"type": "image_url",
"image_url": {
"url": "http://106.53.22.33:8080/image/9f9259430e014577aa80d0665b9b2c92.jpg"
}
}
]
}
]
}
try:
# 发送 POST 请求(使用代理)
response = requests.post(api_url, headers=headers, json=data, proxies=proxies)
# 检查响应状态码
response.raise_for_status()
# 解析响应的 JSON 数据
result = response.json()
# 提取并打印AI的回复
if 'choices' in result and len(result['choices']) > 0:
ai_response = result['choices'][0]['message']['content']
print("AI回复:")
print(ai_response)
else:
print("完整响应:")
print(result)
except requests.exceptions.HTTPError as http_err:
print(f"HTTP 错误发生: {http_err}")
print("响应文本:", response.text)
except requests.exceptions.RequestException as req_err:
print(f"请求错误发生: {req_err}")
except ValueError as json_err:
print(f"JSON 解析错误发生: {json_err}")
123
更多推荐
所有评论(0)