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}**")
相关推荐
ZhengEnCi14 分钟前
AI Agent工具调用防护层怎么写-一份能直接跑的Python四层拦截实现
python·llm·agent
ZhengEnCi35 分钟前
AI Agent工具误调用怎么解决-从参数校验到高危二次确认的四层工程防线
python·llm·agent
镜子AI2 小时前
镜子AI工程教程:用Python实现关键词布局检查器与语义改写查重(附完整代码)
人工智能·python·microsoft
小oo呆3 小时前
【学习心得】Python协程
python
信誓旦旦的程序猿3 小时前
【Python量化实战宝典 #22】涨跌停、连板情绪手册:情绪指标构建(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
gCode Teacher 格码致知3 小时前
Pandas教学-21:subplots的使用
python·pandas·1024程序员节
2601_967980113 小时前
Python开发的顶级开源项目全景图:2025-2026年生态概览
python·机器学习·开源项目·数据科学·生态概览
小成202303202654 小时前
C++~07补充
开发语言·c++
benchmark_cc4 小时前
量化开发中,什么时候应该停止手动下载股票数据?
python·数据分析·pandas·量化交易·1024程序员节·股票数据·quantdash
泡茶喝茶写代码4 小时前
A股量化策略回测实战:从行情数据到绩效评估(第 15 篇):新高新低策略
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口