|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Commands that act as an interactive OpenAI API client |
| 3 | + |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import signal |
| 7 | +import sys |
| 8 | +from typing import List, Optional, Tuple |
| 9 | + |
| 10 | +from openai import OpenAI |
| 11 | +from openai.types.chat import ChatCompletionMessageParam |
| 12 | + |
| 13 | +from vllm.entrypoints.cli.types import CLISubcommand |
| 14 | +from vllm.utils import FlexibleArgumentParser |
| 15 | + |
| 16 | + |
| 17 | +def _register_signal_handlers(): |
| 18 | + |
| 19 | + def signal_handler(sig, frame): |
| 20 | + sys.exit(0) |
| 21 | + |
| 22 | + signal.signal(signal.SIGINT, signal_handler) |
| 23 | + signal.signal(signal.SIGTSTP, signal_handler) |
| 24 | + |
| 25 | + |
| 26 | +def _interactive_cli(args: argparse.Namespace) -> Tuple[str, OpenAI]: |
| 27 | + _register_signal_handlers() |
| 28 | + |
| 29 | + base_url = args.url |
| 30 | + api_key = args.api_key or os.environ.get("OPENAI_API_KEY", "EMPTY") |
| 31 | + openai_client = OpenAI(api_key=api_key, base_url=base_url) |
| 32 | + |
| 33 | + if args.model_name: |
| 34 | + model_name = args.model_name |
| 35 | + else: |
| 36 | + available_models = openai_client.models.list() |
| 37 | + model_name = available_models.data[0].id |
| 38 | + |
| 39 | + print(f"Using model: {model_name}") |
| 40 | + |
| 41 | + return model_name, openai_client |
| 42 | + |
| 43 | + |
| 44 | +def chat(system_prompt: Optional[str], model_name: str, |
| 45 | + client: OpenAI) -> None: |
| 46 | + conversation: List[ChatCompletionMessageParam] = [] |
| 47 | + if system_prompt is not None: |
| 48 | + conversation.append({"role": "system", "content": system_prompt}) |
| 49 | + |
| 50 | + print("Please enter a message for the chat model:") |
| 51 | + while True: |
| 52 | + try: |
| 53 | + input_message = input("> ") |
| 54 | + except EOFError: |
| 55 | + return |
| 56 | + conversation.append({"role": "user", "content": input_message}) |
| 57 | + |
| 58 | + chat_completion = client.chat.completions.create(model=model_name, |
| 59 | + messages=conversation) |
| 60 | + |
| 61 | + response_message = chat_completion.choices[0].message |
| 62 | + output = response_message.content |
| 63 | + |
| 64 | + conversation.append(response_message) # type: ignore |
| 65 | + print(output) |
| 66 | + |
| 67 | + |
| 68 | +def _add_query_options( |
| 69 | + parser: FlexibleArgumentParser) -> FlexibleArgumentParser: |
| 70 | + parser.add_argument( |
| 71 | + "--url", |
| 72 | + type=str, |
| 73 | + default="http://localhost:8000/v1", |
| 74 | + help="url of the running OpenAI-Compatible RESTful API server") |
| 75 | + parser.add_argument( |
| 76 | + "--model-name", |
| 77 | + type=str, |
| 78 | + default=None, |
| 79 | + help=("The model name used in prompt completion, default to " |
| 80 | + "the first model in list models API call.")) |
| 81 | + parser.add_argument( |
| 82 | + "--api-key", |
| 83 | + type=str, |
| 84 | + default=None, |
| 85 | + help=( |
| 86 | + "API key for OpenAI services. If provided, this api key " |
| 87 | + "will overwrite the api key obtained through environment variables." |
| 88 | + )) |
| 89 | + return parser |
| 90 | + |
| 91 | + |
| 92 | +class ChatCommand(CLISubcommand): |
| 93 | + """The `chat` subcommand for the vLLM CLI. """ |
| 94 | + |
| 95 | + def __init__(self): |
| 96 | + self.name = "chat" |
| 97 | + super().__init__() |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def cmd(args: argparse.Namespace) -> None: |
| 101 | + model_name, client = _interactive_cli(args) |
| 102 | + system_prompt = args.system_prompt |
| 103 | + conversation: List[ChatCompletionMessageParam] = [] |
| 104 | + if system_prompt is not None: |
| 105 | + conversation.append({"role": "system", "content": system_prompt}) |
| 106 | + |
| 107 | + print("Please enter a message for the chat model:") |
| 108 | + while True: |
| 109 | + try: |
| 110 | + input_message = input("> ") |
| 111 | + except EOFError: |
| 112 | + return |
| 113 | + conversation.append({"role": "user", "content": input_message}) |
| 114 | + |
| 115 | + chat_completion = client.chat.completions.create( |
| 116 | + model=model_name, messages=conversation) |
| 117 | + |
| 118 | + response_message = chat_completion.choices[0].message |
| 119 | + output = response_message.content |
| 120 | + |
| 121 | + conversation.append(response_message) # type: ignore |
| 122 | + print(output) |
| 123 | + |
| 124 | + def subparser_init( |
| 125 | + self, |
| 126 | + subparsers: argparse._SubParsersAction) -> FlexibleArgumentParser: |
| 127 | + chat_parser = subparsers.add_parser( |
| 128 | + "chat", |
| 129 | + help="Generate chat completions via the running API server", |
| 130 | + usage="vllm chat [options]") |
| 131 | + _add_query_options(chat_parser) |
| 132 | + chat_parser.add_argument( |
| 133 | + "--system-prompt", |
| 134 | + type=str, |
| 135 | + default=None, |
| 136 | + help=("The system prompt to be added to the chat template, " |
| 137 | + "used for models that support system prompts.")) |
| 138 | + return chat_parser |
| 139 | + |
| 140 | + |
| 141 | +class CompleteCommand(CLISubcommand): |
| 142 | + """The `complete` subcommand for the vLLM CLI. """ |
| 143 | + |
| 144 | + def __init__(self): |
| 145 | + self.name = "complete" |
| 146 | + super().__init__() |
| 147 | + |
| 148 | + @staticmethod |
| 149 | + def cmd(args: argparse.Namespace) -> None: |
| 150 | + model_name, client = _interactive_cli(args) |
| 151 | + print("Please enter prompt to complete:") |
| 152 | + while True: |
| 153 | + input_prompt = input("> ") |
| 154 | + completion = client.completions.create(model=model_name, |
| 155 | + prompt=input_prompt) |
| 156 | + output = completion.choices[0].text |
| 157 | + print(output) |
| 158 | + |
| 159 | + def subparser_init( |
| 160 | + self, |
| 161 | + subparsers: argparse._SubParsersAction) -> FlexibleArgumentParser: |
| 162 | + complete_parser = subparsers.add_parser( |
| 163 | + "complete", |
| 164 | + help=("Generate text completions based on the given prompt " |
| 165 | + "via the running API server"), |
| 166 | + usage="vllm complete [options]") |
| 167 | + _add_query_options(complete_parser) |
| 168 | + return complete_parser |
| 169 | + |
| 170 | + |
| 171 | +def cmd_init() -> List[CLISubcommand]: |
| 172 | + return [ChatCommand(), CompleteCommand()] |
0 commit comments