Skip to content

Commit

Permalink
chore(examples-docs): upgrade to OpenAI V1 (vllm-project#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
mspronesti authored Dec 3, 2023
1 parent cd3aa15 commit c07a442
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
27 changes: 19 additions & 8 deletions docs/source/getting_started/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@ Since this server is compatible with OpenAI API, you can use it as a drop-in rep

.. code-block:: python
import openai
from openai import OpenAI
# Modify OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
completion = openai.Completion.create(model="facebook/opt-125m",
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
completion = client.completions.create(model="facebook/opt-125m",
prompt="San Francisco is a")
print("Completion result:", completion)
Expand Down Expand Up @@ -194,11 +199,17 @@ Using the `openai` python package, you can also communicate with the model in a

.. code-block:: python
import openai
from openai import OpenAI
# Set OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
chat_response = openai.ChatCompletion.create(
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
chat_response = client.chat.completions.create(
model="facebook/opt-125m",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
Expand Down
26 changes: 15 additions & 11 deletions examples/openai_chatcompletion_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import openai
from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"

# List models API
models = openai.Model.list()
print("Models:", models)
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key=openai_api_key,
base_url=openai_api_base,
)

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

# Chat completion API
chat_completion = openai.ChatCompletion.create(
model=model,
chat_completion = client.chat.completions.create(
messages=[{
"role": "system",
"content": "You are a helpful assistant."
Expand All @@ -27,7 +28,10 @@
}, {
"role": "user",
"content": "Where was it played?"
}])
}],
model=model,
)


print("Chat completion results:")
print(chat_completion)
22 changes: 13 additions & 9 deletions examples/openai_completion_client.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import openai
from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"

# List models API
models = openai.Model.list()
print("Models:", models)
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key=openai_api_key,
base_url=openai_api_base,
)

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

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

print("Completion results:")
if stream:
Expand Down

0 comments on commit c07a442

Please sign in to comment.