cli.py 3.4 KB

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