main
py 64 lines 1.67 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Example Python client for OpenAI Chat Completion using vLLM API server
4 NOTE: start a supported chat completion model server with `vllm serve`, e.g.
5 vllm serve meta-llama/Llama-2-7b-chat-hf
6 """
7
8 import argparse
9
10 from openai import OpenAI
11
12 # Modify OpenAI's API key and API base to use vLLM's API server.
13 openai_api_key = "EMPTY"
14 openai_api_base = "http://localhost:8000/v1"
15
16 messages = [
17 {"role": "system", "content": "You are a helpful assistant."},
18 {"role": "user", "content": "Who won the world series in 2020?"},
19 {
20 "role": "assistant",
21 "content": "The Los Angeles Dodgers won the World Series in 2020.",
22 },
23 {"role": "user", "content": "Where was it played?"},
24 ]
25
26
27 def parse_args():
28 parser = argparse.ArgumentParser(description="Client for vLLM API server")
29 parser.add_argument(
30 "--stream", action="store_true", help="Enable streaming response"
31 )
32 return parser.parse_args()
33
34
35 def main(args):
36 client = OpenAI(
37 # defaults to os.environ.get("OPENAI_API_KEY")
38 api_key=openai_api_key,
39 base_url=openai_api_base,
40 )
41
42 models = client.models.list()
43 model = models.data[0].id
44
45 # Chat Completion API
46 chat_completion = client.chat.completions.create(
47 messages=messages,
48 model=model,
49 stream=args.stream,
50 )
51
52 print("-" * 50)
53 print("Chat completion results:")
54 if args.stream:
55 for c in chat_completion:
56 print(c)
57 else:
58 print(chat_completion)
59 print("-" * 50)
60
61
62 if __name__ == "__main__":
63 args = parse_args()
64 main(args)