From c07a4428545c7708626ab04bb0033284e71d4ed7 Mon Sep 17 00:00:00 2001 From: Massimiliano Pronesti Date: Sun, 3 Dec 2023 10:11:22 +0100 Subject: [PATCH] chore(examples-docs): upgrade to OpenAI V1 (#1785) --- docs/source/getting_started/quickstart.rst | 27 +++++++++++++++------- examples/openai_chatcompletion_client.py | 26 ++++++++++++--------- examples/openai_completion_client.py | 22 ++++++++++-------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/docs/source/getting_started/quickstart.rst b/docs/source/getting_started/quickstart.rst index bd3940f650e0..c9aacc20da37 100644 --- a/docs/source/getting_started/quickstart.rst +++ b/docs/source/getting_started/quickstart.rst @@ -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 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."}, diff --git a/examples/openai_chatcompletion_client.py b/examples/openai_chatcompletion_client.py index af2a690ce5c1..0b8e4b86ef5e 100644 --- a/examples/openai_chatcompletion_client.py +++ b/examples/openai_chatcompletion_client.py @@ -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." @@ -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) diff --git a/examples/openai_completion_client.py b/examples/openai_completion_client.py index 310caf52793f..7a80c4ac49ab 100644 --- a/examples/openai_completion_client.py +++ b/examples/openai_completion_client.py @@ -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: