豆包模型调用 http方式
【代码】豆包模型调用 http方式。
·
"""
response = ask("写一篇高中生的文章")
print("豆包回答:", response)
"""
import requests
import time
from requests.exceptions import RequestException
# 配置API信息
ARK_API_KEY = "you_key" # 替换为实际API密钥
API_URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
def ask(quest):
messages = [{"role": "user", "content": quest}]
"""
调用豆包API的通用函数
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {ARK_API_KEY}"
}
data = {
"model": "doubao-1-5-pro-32k-250115",
"messages": messages
}
max_retries=1
for attempt in range(max_retries):
try:
response = requests.post(API_URL, headers=headers, json=data)
response.raise_for_status() # 检查状态码
return response.json()["choices"][0]["message"]["content"]
except RequestException as e:
print(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
if attempt < max_retries - 1:
time.sleep(2 ** attempt) # 指数退避重试
raise Exception("Max retries exceeded")
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)