一、获取API Key
-
通过官方网址注册账号并且认证: 零一万物大模型开放平台
-
创建API Key
二、安装及调用
安装OpenAI SDK
零一万物API 接口兼容 OpenAI 的 Python SDK,只需要简单配置即可使用。
安装 OpenAI SDK。请确保使用的 Python 版本至少为 3.7.1,OpenAI SDK 版本不低于 1.0.0。
pip install openai
调用示例一: 同步调用
import openai
from openai import OpenAI
API_BASE = "https://api.lingyiwanwu.com/v1"
API_KEY = "your key"
client = OpenAI(
api_key=API_KEY,
base_url=API_BASE
)
completion = client.chat.completions.create(
model="yi-large-turbo",
messages=[{"role": "user", "content": "Hi, who are you?"}]
)
print(completion)
调用示例二: 流式调用
import openai
from openai import OpenAI
API_BASE = "https://api.lingyiwanwu.com/v1"
API_KEY = "your key"
client = OpenAI(
api_key=API_KEY,
base_url=API_BASE
)
completion = client.chat.completions.create(
model="yi-large",
messages=[{"role": "user", "content": "Hi, who are you?"}],
stream=True
)
for chunk in completion:
print(chunk.choices[0].delta.content or "", end="", flush=True)
更多调用详情参考官方文档: 零一万物大模型接口文档