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}**")
相关推荐
aqi004 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn5 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵21 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent