Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
norm
vllm
Commits
c07a4428
Unverified
Commit
c07a4428
authored
Dec 03, 2023
by
Massimiliano Pronesti
Committed by
GitHub
Dec 03, 2023
Browse files
chore(examples-docs): upgrade to OpenAI V1 (#1785)
parent
cd3aa153
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
28 deletions
+47
-28
docs/source/getting_started/quickstart.rst
docs/source/getting_started/quickstart.rst
+19
-8
examples/openai_chatcompletion_client.py
examples/openai_chatcompletion_client.py
+15
-11
examples/openai_completion_client.py
examples/openai_completion_client.py
+13
-9
No files found.
docs/source/getting_started/quickstart.rst
View file @
c07a4428
...
...
@@ -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)
...
...
@@ -194,11 +199,17 @@ Using the `openai` python package, you can also communicate with the model in a
.. code-block:: python
import
o
pen
ai
from openai
import
O
pen
AI
# 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."},
...
...
examples/openai_chatcompletion_client.py
View file @
c07a4428
import
o
pen
ai
from
openai
import
O
pen
AI
# 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."
...
...
@@ -27,7 +28,10 @@ chat_completion = openai.ChatCompletion.create(
},
{
"role"
:
"user"
,
"content"
:
"Where was it played?"
}])
}],
model
=
model
,
)
print
(
"Chat completion results:"
)
print
(
chat_completion
)
examples/openai_completion_client.py
View file @
c07a4428
import
o
pen
ai
from
openai
import
O
pen
AI
# 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
.
C
ompletion
.
create
(
completion
=
client
.
c
ompletion
s
.
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
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment