为方便您集成和使用,火山方舟模型 API 会尽可能兼容 OpenAI API。修改 base url、model 、api_key等少量配置,轻松将方舟模型服务集成到您已有的系统中。
前提条件#
OpenAI SDK#
-
Python版本:3.7及以上。
-
OpenAI SDK:1.0版本及以上,安装命令:
pip install --upgrade "openai>=1.0"
快速开始示例#
from openai import OpenAI
import os
client = OpenAI(
# The base URL for model invocation
base_url="https://ark.cn-beijing.volces.com/api/v3",
# Replace with your API Key
api_key=os.environ.get("ARK_API_KEY"),
)
completion = client.chat.completions.create(
# Replace with Model ID
model="doubao-seed-2-1-pro-260915",
messages = [
{"role": "user", "content": "Hello"},
],
)
print(completion.choices[0].message.content)
设置额外字段#
传入OpenAI SDK中不支持的字段,可以通过 extra_body 字典传入,如开关模型是否深度思考的 thinking 字段。
from openai import OpenAI
import os
client = OpenAI(
# The base URL for model invocation
base_url="https://ark.cn-beijing.volces.com/api/v3",
# Replace with your API Key
api_key=os.environ.get("ARK_API_KEY"),
)
completion = client.chat.completions.create(
# Replace with Model ID
model="doubao-seed-2-1-pro-260915",
messages = [
{"role": "user", "content": "Hello"},
],
extra_body={
"thinking": {
"type": "disabled", # 不使用深度思考能力
# "type": "enabled", # 使用深度思考能力
}
}
)
print(completion.choices[0].message.content)
设置自定义header#
可以用于传递额外信息,如配置 ID来串联日志,使能数据加密能力。
from openai import OpenAI
import os
client = OpenAI(
# The base URL for model invocation
base_url="https://ark.cn-beijing.volces.com/api/v3",
# Replace with your API Key
api_key=os.environ.get("ARK_API_KEY"),
)
completion = client.chat.completions.create(
# Replace with Model ID
model="doubao-seed-2-1-pro-260915",
messages = [
{"role": "user", "content": "Hello"},
],
# 自定义request id
extra_headers={"X-Client-Request-Id": "202406251728190000B7EA7A9648AC08D9"}
)
print(completion.choices[0].message.content)
向量化 Embedding#
LangChain OpenAI SDK#
安装 LangChain OpenAI SDK:
pip install langchain-openai
示例代码#
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
import os
llm = ChatOpenAI(
# Replace with your API Key
openai_api_key=os.environ.get("ARK_API_KEY"),
# The base URL for model invocation
openai_api_base="https://ark.cn-beijing.volces.com/api/v3",
# Replace with Model ID
model="doubao-seed-2-1-pro-260915",
)
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))