cli.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import annotations
  2. import argparse
  3. from g4f import Provider
  4. from g4f.gui.run import gui_parser, run_gui_args
  5. def main():
  6. parser = argparse.ArgumentParser(description="Run gpt4free")
  7. subparsers = parser.add_subparsers(dest="mode", help="Mode to run the g4f in.")
  8. api_parser = subparsers.add_parser("api")
  9. api_parser.add_argument("--bind", default="0.0.0.0:1337", help="The bind string.")
  10. api_parser.add_argument("--debug", action="store_true", help="Enable verbose logging.")
  11. api_parser.add_argument("--model", default=None, help="Default model for chat completion. (incompatible with --debug and --workers)")
  12. api_parser.add_argument("--provider", choices=[provider.__name__ for provider in Provider.__providers__ if provider.working],
  13. default=None, help="Default provider for chat completion. (incompatible with --debug and --workers)")
  14. api_parser.add_argument("--proxy", default=None, help="Default used proxy.")
  15. api_parser.add_argument("--workers", type=int, default=None, help="Number of workers.")
  16. api_parser.add_argument("--disable-colors", action="store_true", help="Don't use colors.")
  17. api_parser.add_argument("--ignore-cookie-files", action="store_true", help="Don't read .har and cookie files.")
  18. api_parser.add_argument("--g4f-api-key", type=str, default=None, help="Sets an authentication key for your API. (incompatible with --debug and --workers)")
  19. api_parser.add_argument("--ignored-providers", nargs="+", choices=[provider.__name__ for provider in Provider.__providers__ if provider.working],
  20. default=[], help="List of providers to ignore when processing request. (incompatible with --debug and --workers)")
  21. subparsers.add_parser("gui", parents=[gui_parser()], add_help=False)
  22. args = parser.parse_args()
  23. if args.mode == "api":
  24. run_api_args(args)
  25. elif args.mode == "gui":
  26. run_gui_args(args)
  27. else:
  28. parser.print_help()
  29. exit(1)
  30. def run_api_args(args):
  31. from g4f.api import AppConfig, run_api
  32. AppConfig.set_config(
  33. ignore_cookie_files=args.ignore_cookie_files,
  34. ignored_providers=args.ignored_providers,
  35. g4f_api_key=args.g4f_api_key,
  36. defaults={
  37. "model": args.model,
  38. "provider": args.provider,
  39. "proxy": args.proxy
  40. }
  41. )
  42. run_api(
  43. bind=args.bind,
  44. debug=args.debug,
  45. workers=args.workers,
  46. use_colors=not args.disable_colors
  47. )
  48. if __name__ == "__main__":
  49. main()