python实战豆包大模型-文本模型

准备

注册并登录火山引擎

官网 https://console.volcengine.com/ark/region:ark+cn-beijing/experience/chat

开通模型

创建推理接入点

调用参数API Key、model

调用

SDK调用

pip install --upgrade 'volcengine-python-sdkark'

python 复制代码
from volcenginesdkarkruntime import Ark
​
client = Ark(
    base_url="https://ark.cn-beijing.volces.com/api/v3",
    api_key="上面获取的api-key"
)
​
# Non-streaming:
print("----- standard request -----")
completion = client.chat.completions.create(
    model="上面获取的model",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
)
print(completion.choices[0].message.content)
​
# Streaming: 流式调用
print("----- streaming request -----")
stream = client.chat.completions.create(
    model="上面获取的model",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    stream=True
)
for chunk in stream:
    if not chunk.choices:
        continue
    print(chunk.choices[0].delta.content, end="")

HTTP调用

url https://ark.cn-beijing.volces.com/api/v3

json_date { model:"上面获取的model",messages = {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},{"role": "user", "content": "常见的十字花科植物有哪些?"},}

sk 上面获取的api-key

python 复制代码
async def get_common_content(url,json_data,sk):
  try:
    async with aiohttp.ClientSession() as session:
        authorization = "Bearer "+ (str(sk))
        # print(f"**** 调用接口 -{url}-{json_data}-{authorization}****")
        headers = {"Content-Type": 'application/json',"Authorization": authorization }
        async with session.post(url,data = json.dumps(json_data), headers = headers) as response:
            return await response.text()
  except Exception as e:
       print(f"**ERROR 调用模型失败....{e}**")
相关推荐
AC赳赳老秦7 小时前
OpenClaw 合规公开数据采集入门:合法边界、数据源选型与反爬规避实操指南
大数据·人工智能·python·信息可视化·php·deepseek·openclaw
麻瓜老宋8 小时前
AI开发C语言应用按步走,我的基础开发环境
c语言·开发语言
见叶之秋8 小时前
C细节补充
c语言·开发语言
她说彩礼65万8 小时前
C# yeild的使用
开发语言·c#
阿童木写作8 小时前
Python实现跨境商品详情页自动翻译
开发语言·python·机器翻译
2501_931803758 小时前
Go 反射:原理、核心机制与实践指南
开发语言·后端·golang
江屿风8 小时前
【C++笔记】List流食般投喂
开发语言·c++·笔记·list
hhzz8 小时前
CNN猫狗图像分类实战:基于Keras+TensorFlow的卷积神经网络全流程解析
图像处理·python·深度学习·计算机视觉·cnn
辞旧 lekkk18 小时前
【Redis初阶】常见数据类型
开发语言·数据库·c++·redis·学习·缓存·bootstrap
帅次18 小时前
Kotlin 与 Java 互操作:混合工程里的平台类型与 API 边界
java·开发语言·kotlin·suspend·nullable