2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-12-03 10:11:22 +01:00
|
|
|
from openai import OpenAI
|
2023-07-25 23:45:48 -07:00
|
|
|
|
|
|
|
# Modify OpenAI's API key and API base to use vLLM's API server.
|
2023-12-03 10:11:22 +01:00
|
|
|
openai_api_key = "EMPTY"
|
|
|
|
openai_api_base = "http://localhost:8000/v1"
|
2023-07-25 23:45:48 -07:00
|
|
|
|
2023-12-03 10:11:22 +01:00
|
|
|
client = OpenAI(
|
|
|
|
# defaults to os.environ.get("OPENAI_API_KEY")
|
|
|
|
api_key=openai_api_key,
|
|
|
|
base_url=openai_api_base,
|
|
|
|
)
|
2023-07-25 23:45:48 -07:00
|
|
|
|
2023-12-03 10:11:22 +01:00
|
|
|
models = client.models.list()
|
|
|
|
model = models.data[0].id
|
2023-07-25 23:45:48 -07:00
|
|
|
|
2023-12-03 10:11:22 +01:00
|
|
|
chat_completion = client.chat.completions.create(
|
2023-07-25 23:45:48 -07:00
|
|
|
messages=[{
|
|
|
|
"role": "system",
|
|
|
|
"content": "You are a helpful assistant."
|
|
|
|
}, {
|
|
|
|
"role": "user",
|
|
|
|
"content": "Who won the world series in 2020?"
|
|
|
|
}, {
|
|
|
|
"role":
|
|
|
|
"assistant",
|
|
|
|
"content":
|
|
|
|
"The Los Angeles Dodgers won the World Series in 2020."
|
|
|
|
}, {
|
|
|
|
"role": "user",
|
|
|
|
"content": "Where was it played?"
|
2023-12-03 10:11:22 +01:00
|
|
|
}],
|
|
|
|
model=model,
|
|
|
|
)
|
|
|
|
|
2023-07-25 23:45:48 -07:00
|
|
|
print("Chat completion results:")
|
|
|
|
print(chat_completion)
|