Skip to main content
Version: Next

OpenAI 完成客户端

*在线运行 vLLM 入门教程:零基础分步指南

源码 examples/online_serving/openai_completion_client.py

# SPDX-License-Identifier: Apache-2.0

from openai import OpenAI

# 修改 OpenAI 的 API key 和 API base, 使用 vLLM 的 API 服务器。
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"

client = OpenAI(
# 默认为 os.environ.get("OPENAI_API_KEY")
api_key=openai_api_key,
base_url=openai_api_base,
)

models = client.models.list()
model = models.data[0].id

# 补全 API
stream = False
completion = client.completions.create(
model=model,
prompt="A robot may not injure a human being",
echo=False,
n=2,
stream=stream,
logprobs=3)

print("Completion results:")
if stream:
for c in completion:
print(c)
else:
print(completion)