为方便您集成和使用,LAS的算子 API 会尽可能兼容 OpenAI API。修改 base url、model 、api_key等少量配置,轻松将方舟模型服务集成到您已有的系统中。
说明
社区第三方 SDK 不由火山引擎团队维护,本文仅供参考。
调用算子前,您需要先生成算子调用的API Key,并建议将API Key配置为环境变量,便于更安全地调用算子,详情请参见API Key 管理。
pip install --upgrade "openai>=1.0"
from openai import OpenAI import os client = OpenAI( # The base URL for model invocation base_url="https://operator.las.cn-beijing.volces.com/api/v1", # 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-1-6-251015", 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://operator.las.cn-beijing.volces.com/api/v1", # 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-1-6-251015", messages = [ {"role": "user", "content": "Hello"}, ], extra_body={ "thinking": { "type": "disabled", # 不使用深度思考能力 # "type": "enabled", # 使用深度思考能力 } } ) print(completion.choices[0].message.content)
可以用于传递额外信息,如配置 ID来串联日志,使能数据加密能力。
from openai import OpenAI import os client = OpenAI( # The base URL for model invocation base_url="https://operator.las.cn-beijing.volces.com/api/v1", # 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-1-6-251015", messages = [ {"role": "user", "content": "Hello"}, ], # 自定义request id extra_headers={"X-Client-Request-Id": "202406251728190000B7EA7A9648AC08D9"} ) print(completion.choices[0].message.content)
安装 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://operator.las.cn-beijing.volces.com/api/v1", # Replace with Model ID model="doubao-seed-1-6-251015", ) 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))