说明
示例代码中 <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._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)