cli.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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="Add gui to the api.")
  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. return api_parser
  29. def main():
  30. parser = argparse.ArgumentParser(description="Run gpt4free")
  31. subparsers = parser.add_subparsers(dest="mode", help="Mode to run the g4f in.")
  32. subparsers.add_parser("api", parents=[get_api_parser()], add_help=False)
  33. subparsers.add_parser("gui", parents=[gui_parser()], add_help=False)
  34. args = parser.parse_args()
  35. if args.mode == "api":
  36. run_api_args(args)
  37. elif args.mode == "gui":
  38. run_gui_args(args)
  39. else:
  40. parser.print_help()
  41. exit(1)
  42. def run_api_args(args):
  43. from g4f.api import AppConfig, run_api
  44. AppConfig.set_config(
  45. ignore_cookie_files=args.ignore_cookie_files,
  46. ignored_providers=args.ignored_providers,
  47. g4f_api_key=args.g4f_api_key,
  48. provider=args.provider,
  49. image_provider=args.image_provider,
  50. proxy=args.proxy,
  51. model=args.model,
  52. gui=args.gui,
  53. )
  54. if args.cookie_browsers:
  55. g4f.cookies.browsers = [g4f.cookies[browser] for browser in args.cookie_browsers]
  56. run_api(
  57. bind=args.bind,
  58. port=args.port,
  59. debug=args.debug,
  60. workers=args.workers,
  61. use_colors=not args.disable_colors,
  62. reload=args.reload
  63. )
  64. if __name__ == "__main__":
  65. main()