cli.py 4.0 KB

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