火山方舟模型调用 API 与 OpenAI API 协议兼容,您可以使用兼容 OpenAI API 协议的多语言社区 SDK 调用火山方舟大模型或应用。您可以很方便地迁移模型服务至方舟平台以及 Doubao 大模型。
调用之前,您需要获取以下信息:
https://ark.cn-beijing.volces.com/api/v3/
https://ark.cn-beijing.volces.com/api/v3/bots/
说明
社区第三方 SDK 不由火山引擎团队维护,本文仅供参考。
pip install --upgrade openai>=1.0
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- standard request -----") completion = client.chat.completions.create( model="<YOUR_ENDPOINT_ID>", messages = [ {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"}, {"role": "user", "content": "常见的十字花科植物有哪些?"}, ], ) print(completion.choices[0].message.content)
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- standard request -----") completion = client.chat.completions.create( model="<YOUR_ENDPOINT_ID>", messages=[ {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"}, {"role": "user", "content": "花椰菜是什么?"}, {"role": "assistant", "content": "花椰菜又称菜花、花菜,是一种常见的蔬菜。"}, {"role": "user", "content": "再详细点"}, ], ) print(completion.choices[0].message.content)
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- streaming request -----") stream = client.chat.completions.create( model="<YOUR_ENDPOINT_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()
import asyncio import os from openai import AsyncOpenAI client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) async def main() -> None: stream = await client.chat.completions.create( model="<YOUR_ENDPOINT_ID>", messages=[ {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"}, {"role": "user", "content": "常见的十字花科植物有哪些?"}, ], stream=True, ) async for completion in stream: print(completion.choices[0].delta.content, end="") print()
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- function call request -----") completion = client.chat.completions.create( model="<YOUR_ENDPOINT_ID>", messages = [ {"role": "user", "content": "北京今天天气如何?"}, ], tools=[ { "type": "function", "function": { "name": "get_current_weather", "description": "获取给定地点的天气", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "地点的位置信息,比如北京" }, "unit": { "type": "string", "enum": [ "摄氏度", "华氏度" ] } }, "required": [ "location" ] } } } ] ) print(completion.choices[0])
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- standard request -----") completion = client.chat.completions.create( model="<YOUR_ENDPOINT_ID>", messages = [ {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"}, {"role": "user", "content": "常见的十字花科植物有哪些?"}, ], # 自定义request id extra_headers={"X-Client-Request-Id": "202406251728190000B7EA7A9648AC08D9"} ) print(completion.choices[0].message.content)
from openai import OpenAI import os client = OpenAI( # 替换为您需要调用的模型服务Base Url base_url="<BASE_URL>", # 环境变量中配置您的API Key api_key=os.environ.get("ARK_API_KEY") ) print("----- embeddings request -----") resp = client.embeddings.create( model="<YOUR_ENDPOINT_ID>", input=["花椰菜又称菜花、花菜,是一种常见的蔬菜。"] ) print(resp)
LangChain OpenAI SDK:您可以使用下面命令安装。
pip install langchain-openai
from langchain_openai import ChatOpenAI from langchain_core.prompts import PromptTemplate import os llm = ChatOpenAI( # 环境变量中配置您的API Key openai_api_key=os.environ.get("ARK_API_KEY"), # 替换为您需要调用的模型服务Base Url openai_api_base="<BASE_URL>", # 替换为您创建推理接入点 ID model_name="<YOUR_ENDPOINT_ID>" ) template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" llm_chain = prompt | llm print(llm_chain.invoke(question))