| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import argparse |
| 5 | |
| 6 | from openai import OpenAI |
| 7 | |
| 8 | # Modify OpenAI's API key and API base to use vLLM's API server. |
| 9 | openai_api_key = "EMPTY" |
| 10 | openai_api_base = "http://localhost:8000/v1" |
| 11 | |
| 12 | |
| 13 | def parse_args(): |
| 14 | parser = argparse.ArgumentParser(description="Client for vLLM API server") |
| 15 | parser.add_argument( |
| 16 | "--stream", action="store_true", help="Enable streaming response" |
| 17 | ) |
| 18 | return parser.parse_args() |
| 19 | |
| 20 | |
| 21 | def main(args): |
| 22 | client = OpenAI( |
| 23 | # defaults to os.environ.get("OPENAI_API_KEY") |
| 24 | api_key=openai_api_key, |
| 25 | base_url=openai_api_base, |
| 26 | ) |
| 27 | |
| 28 | models = client.models.list() |
| 29 | model = models.data[0].id |
| 30 | |
| 31 | # Completion API |
| 32 | completion = client.completions.create( |
| 33 | model=model, |
| 34 | prompt="A robot may not injure a human being", |
| 35 | echo=False, |
| 36 | n=2, |
| 37 | stream=args.stream, |
| 38 | logprobs=3, |
| 39 | ) |
| 40 | |
| 41 | print("-" * 50) |
| 42 | print("Completion results:") |
| 43 | if args.stream: |
| 44 | for c in completion: |
| 45 | print(c) |
| 46 | else: |
| 47 | print(completion) |
| 48 | print("-" * 50) |
| 49 | |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | args = parse_args() |
| 53 | main(args) |