You need to enable JavaScript to run this app.
导航
BotChatCompletions
最近更新时间:2025.01.16 23:13:56首次发布时间:2024.08.12 20:37:59

前提条件

参数说明

示例代码

说明

示例代码中 <YOUR_BOT_ID> 需要替换为您在平台上创建的应用ID。

单轮

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- standard request -----")
completion = client.bot_chat.completions.create(
    model="<YOUR_BOT_ID>",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
)
print(completion.choices[0].message.content)
print(completion.references)

多轮

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- multiple rounds request -----")
completion = client.bot_chat.completions.create(
    model="<YOUR_BOT_ID>",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "花椰菜是什么?"},
        {"role": "assistant", "content": "花椰菜又称菜花、花菜,是一种常见的蔬菜。"},
        {"role": "user", "content": "再详细点"},
    ],
)
print(completion.choices[0].message.content)
print(completion.references)

流式

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- streaming request -----")
stream = client.bot_chat.completions.create(
    model="<YOUR_BOT_ID>",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    stream=True
)
for chunk in stream:
    if chunk.references:
        print(chunk.references)
    if not chunk.choices:
        continue
    print(chunk.choices[0].delta.content, end="")

群聊

import os 
from volcenginesdkarkruntime import Ark 
 
client = Ark( 
    api_key=os.environ.get("ARK_API_KEY"), 
) 
 
print("----- streaming request -----") 
stream = client.bot_chat.completions.create( 
    model="<YOUR_BOT_ID>", 
    messages = [ 
        {"role": "user", "content": "你是谁?"},
    ], 
    stream=True, 
    metadata={ 
      "target_character_name": "群聊角色", 
      "group_chat_config": { 
        "user_name": "用户角色名", 
        "description": "场景描述",
        "characters": [ 
          { 
            "name": "群聊角色", 
            "system_prompt": "群聊角色设定", 
            "model_desc": { 
              "endpoint_id": "<YOUR_ENDPOINT_ID>" 
            } 
          } 
      }, 
    ] 
  } 
} 
) 
for chunk in stream: 
    if chunk.references: 
        print(chunk.references) 
    if not chunk.choices: 
        continue 
    print(chunk.choices[0].delta.content, end="")

设置自定义 header

import os
from volcenginesdkarkruntime._constants import CLIENT_REQUEST_HEADER
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- standard request -----")
completion = client.bot_chat.completions.create(
    model="<YOUR_BOT_ID>",
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    # 自定义request id
    extra_headers={CLIENT_REQUEST_HEADER: "202406251728190000B7EA7A9648AC08D9"}
)
print(completion.choices[0].message.content)

异常处理

import os
from volcenginesdkarkruntime._exceptions import ArkAPIError
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

# Streaming:
print("----- streaming request -----")
try:
    stream = client.bot_chat.completions.create(
        model="<YOUR_BOT_ID>",
        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="")
    print()
except ArkAPIError as e:
    print(e)

视觉理解

应用已经能够支持视觉理解模型。您可以在应用中配置视觉理解模型的推理接入点,使应用具备理解图片、视频能力,处理如图片文字内容识别,根据图片绘制表格、开发代码等任务。

更详细的视觉理解模型使用教程请参见视觉理解

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- standard request -----")
completion = client.bot_chat.completions.create(
    model="<YOUR_BOT_ID>",
    messages = [
        {
            "role": "user",  # 指定消息的角色为用户
            "content": [  # 消息内容列表
                {"type": "text", "text": "对美国人来说最安全的支付APP是什么?"},  # 文本消息
                {
                    "type": "image_url",  # 图片消息
                    # 图片的URL,需要大模型进行理解的图片链接
                    "image_url": {"url":  "https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/7d2e7e810f054952aa8854fc99577260~tplv-goo7wpa0wc-image.image"}
                },
            ],
        }
    ],
)
print(completion.choices[0].message.content)
print(completion.references)

Bot SDK与Chat SDK差异对比

对比项

Bot SDK

Chat SDK

单轮

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)
print("----- standard request -----")
completion = client.bot_chat.completions.create(# 注意此处存在差异
    model="<YOUR_BOT_ID>",# 注意此处为您的应用ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
)
print(completion.choices[0].message.content)
print(completion.references)
import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)
print("----- standard request -----")
completion = client.chat.completions.create(# 注意此处存在差异
    model="<YOUR_ENDPOINT_ID>",# 注意此处为您的推理接入点ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
)
print(completion.choices[0].message.content)

多轮

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- multiple rounds request -----")
completion = client.bot_chat.completions.create(# 注意此处存在差异
    model="<YOUR_BOT_ID>",# 注意此处为您的应用ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "花椰菜是什么?"},
        {"role": "assistant", "content": "花椰菜又称菜花、花菜,是一种常见的蔬菜。"},
        {"role": "user", "content": "再详细点"},
    ],
)
print(completion.choices[0].message.content)
print(completion.references)
import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- multiple rounds request -----")
completion = client.chat.completions.create(# 注意此处存在差异
    model="<YOUR_ENDPOINT_ID>",# 注意此处为您的推理接入点ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "花椰菜是什么?"},
        {"role": "assistant", "content": "花椰菜又称菜花、花菜,是一种常见的蔬菜。"},
        {"role": "user", "content": "再详细点"},
    ],
)
print(completion.choices[0].message.content)

流式

import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- streaming request -----")
stream = client.bot_chat.completions.create(# 注意此处存在差异
    model="<YOUR_BOT_ID>",# 注意此处为您的应用ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    stream=True
)
for chunk in stream:
    if chunk.references:
        print(chunk.references)
    if not chunk.choices:
        continue
    print(chunk.choices[0].delta.content, end="")
import os
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- streaming request -----")
stream = client.chat.completions.create(# 注意此处存在差异
    model="<YOUR_ENDPOINT_ID>",# 注意此处为您的推理接入点ID
    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="")
print()

自定义header

import os
from volcenginesdkarkruntime._constants import CLIENT_REQUEST_HEADER
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- standard request -----")
completion = client.bot_chat.completions.create(# 注意此处存在差异
    model="<YOUR_BOT_ID>",# 注意此处为您的应用ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    # 自定义request id
    extra_headers={CLIENT_REQUEST_HEADER: "202406251728190000B7EA7A9648AC08D9"}
)
print(completion.choices[0].message.content)
import os
from volcenginesdkarkruntime._constants import CLIENT_REQUEST_HEADER
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

print("----- standard request -----")
completion = client.chat.completions.create(# 注意此处存在差异
    model="<YOUR_ENDPOINT_ID>",# 注意此处为您的推理接入点ID
    messages = [
        {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
        {"role": "user", "content": "常见的十字花科植物有哪些?"},
    ],
    # 自定义request id
    extra_headers={CLIENT_REQUEST_HEADER: "202406251728190000B7EA7A9648AC08D9"}
)
print(completion.choices[0].message.content)

异常处理

import os
from volcenginesdkarkruntime._exceptions import ArkAPIError
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

# Streaming:
print("----- streaming request -----")
try:
    stream = client.bot_chat.completions.create(# 注意此处存在差异
        model="<YOUR_BOT_ID>",# 注意此处为您的应用ID
        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="")
    print()
except ArkAPIError as e:
    print(e)
import os
from volcenginesdkarkruntime._exceptions import ArkAPIError
from volcenginesdkarkruntime import Ark

client = Ark(
    api_key=os.environ.get("ARK_API_KEY"),
)

# Streaming:
print("----- streaming request -----")
try:
    stream = client.chat.completions.create(# 注意此处存在差异
        model="<YOUR_ENDPOINT_ID>",# 注意此处为您的推理接入点ID
        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="")
    print()
except ArkAPIError as e:
    print(e)