cli.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # The CLI entrypoint to Aphrodite.
  2. import argparse
  3. import os
  4. import signal
  5. import sys
  6. from typing import Optional
  7. from openai import OpenAI
  8. from aphrodite.common.utils import FlexibleArgumentParser
  9. from aphrodite.endpoints.openai.api_server import run_server
  10. from aphrodite.endpoints.openai.args import make_arg_parser
  11. def registrer_signal_handlers():
  12. def signal_handler(sig, frame):
  13. sys.exit(0)
  14. signal.signal(signal.SIGINT, signal_handler)
  15. signal.signal(signal.SIGTSTP, signal_handler)
  16. def serve(args: argparse.Namespace) -> None:
  17. # EngineArgs expects the model name to be passed as --model.
  18. args.model = args.model_tag
  19. run_server(args)
  20. def interactive_cli(args: argparse.Namespace) -> None:
  21. registrer_signal_handlers()
  22. base_url = args.url
  23. api_key = args.api_key or os.environ.get("OPENAI_API_KEY", "EMPTY")
  24. openai_client = OpenAI(api_key=api_key, base_url=base_url)
  25. if args.model_name:
  26. model_name = args.model_name
  27. else:
  28. available_models = openai_client.models.list()
  29. model_name = available_models.data[0].id
  30. print(f"Using model: {model_name}")
  31. if args.command == "complete":
  32. complete(model_name, openai_client)
  33. elif args.command == "chat":
  34. chat(args.system_prompt, model_name, openai_client)
  35. def complete(model_name: str, client: OpenAI) -> None:
  36. print("Please enter prompt to complete:")
  37. while True:
  38. input_prompt = input("> ")
  39. completion = client.completions.create(model=model_name,
  40. prompt=input_prompt)
  41. output = completion.choices[0].text
  42. print(output)
  43. def chat(system_prompt: Optional[str], model_name: str,
  44. client: OpenAI) -> None:
  45. conversation = []
  46. if system_prompt is not None:
  47. conversation.append({"role": "system", "content": system_prompt})
  48. print("Please enter a message for the chat model:")
  49. while True:
  50. input_message = input("> ")
  51. message = {"role": "user", "content": input_message}
  52. conversation.append(message)
  53. chat_completion = client.chat.completions.create(model=model_name,
  54. messages=conversation)
  55. response_message = chat_completion.choices[0].message
  56. output = response_message.content
  57. conversation.append(response_message)
  58. print(output)
  59. def _add_query_options(
  60. parser: FlexibleArgumentParser) -> FlexibleArgumentParser:
  61. parser.add_argument(
  62. "--url",
  63. type=str,
  64. default="http://localhost:2242/v1",
  65. help="url of the running OpenAI-Compatible RESTful API server")
  66. parser.add_argument(
  67. "--model-name",
  68. type=str,
  69. default=None,
  70. help=("The model name used in prompt completion, default to "
  71. "the first model in list models API call."))
  72. parser.add_argument(
  73. "--api-key",
  74. type=str,
  75. default=None,
  76. help=(
  77. "API key for OpenAI services. If provided, this api key "
  78. "will overwrite the api key obtained through environment variables."
  79. ))
  80. return parser
  81. def main():
  82. parser = FlexibleArgumentParser(description="Aphrodite CLI")
  83. subparsers = parser.add_subparsers(required=True)
  84. serve_parser = subparsers.add_parser(
  85. "run",
  86. help="Start the Aphrodite OpenAI Compatible API server",
  87. usage="aphrodite run <model_tag> [options]")
  88. serve_parser.add_argument("model_tag",
  89. type=str,
  90. help="The model tag to serve")
  91. serve_parser = make_arg_parser(serve_parser)
  92. serve_parser.set_defaults(dispatch_function=serve)
  93. complete_parser = subparsers.add_parser(
  94. "complete",
  95. help=("Generate text completions based on the given prompt "
  96. "via the running API server"),
  97. usage="aphrodite complete [options]")
  98. _add_query_options(complete_parser)
  99. complete_parser.set_defaults(dispatch_function=interactive_cli,
  100. command="complete")
  101. chat_parser = subparsers.add_parser(
  102. "chat",
  103. help="Generate chat completions via the running API server",
  104. usage="aphrodite chat [options]")
  105. _add_query_options(chat_parser)
  106. chat_parser.add_argument(
  107. "--system-prompt",
  108. type=str,
  109. default=None,
  110. help=("The system prompt to be added to the chat template, "
  111. "used for models that support system prompts."))
  112. chat_parser.set_defaults(dispatch_function=interactive_cli, command="chat")
  113. args = parser.parse_args()
  114. # One of the sub commands should be executed.
  115. if hasattr(args, "dispatch_function"):
  116. args.dispatch_function(args)
  117. else:
  118. parser.print_help()
  119. if __name__ == "__main__":
  120. main()