diff options
author | razrab <razrab@PimoLin> | 2023-10-23 10:01:08 +0200 |
---|---|---|
committer | razrab <razrab@PimoLin> | 2023-10-23 10:01:40 +0200 |
commit | 36fe869a81ad44480b2aa72bdc3fae2952c69816 (patch) | |
tree | 9eb201ed704f06e3524fe006db0016a7b4215837 | |
parent | Merge remote-tracking branch 'upstream/main' into api-ignore-providers (diff) | |
download | gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar.gz gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar.bz2 gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar.lz gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar.xz gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.tar.zst gpt4free-36fe869a81ad44480b2aa72bdc3fae2952c69816.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/api/__init__.py | 14 | ||||
-rw-r--r-- | g4f/cli.py | 15 | ||||
-rw-r--r-- | g4f_api/ApiInterface.py | 12 | ||||
-rw-r--r-- | g4f_api/__init__.py | 0 | ||||
-rw-r--r-- | g4f_api/__main__.py | 23 |
5 files changed, 24 insertions, 40 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index a682f094..4c945fbe 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -1,4 +1,9 @@ -import g4f; g4f.logging = True +import typing + +import g4f; +from .. import BaseProvider + +g4f.logging = True import time import json import random @@ -26,10 +31,12 @@ class Api: __default_ip = '127.0.0.1' __default_port = 1337 - def __init__(self, engine: g4f, debug: bool = True, sentry: bool = False) -> None: + def __init__(self, engine: g4f, debug: bool = True, sentry: bool = False, + list_ignored_providers:typing.List[typing.Union[str, BaseProvider]]=None) -> None: self.engine = engine self.debug = debug self.sentry = sentry + self.list_ignored_providers = list_ignored_providers self.log_level = logging.DEBUG if debug else logging.WARN hook_logging(level=self.log_level, format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s') @@ -102,7 +109,8 @@ class Api: logger.info(f'model: {model}, stream: {stream}, request: {messages[-1]["content"]}') response = self.engine.ChatCompletion.create(model=model, - stream=stream, messages=messages) + stream=stream, messages=messages, + ignored=self.list_ignored_providers) completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28)) completion_timestamp = int(time.time()) @@ -1,5 +1,8 @@ import argparse +from enum import Enum + import g4f +from g4f import Provider from g4f.api import Api from g4f.gui.run import gui_parser, run_gui_args @@ -10,14 +13,22 @@ def run_gui(args): def main(): + IgnoredProviders = Enum("ignore_providers", {key: key for key in Provider.__all__}) parser = argparse.ArgumentParser(description="Run gpt4free") subparsers = parser.add_subparsers(dest="mode", help="Mode to run the g4f in.") - subparsers.add_parser("api") + api_parser=subparsers.add_parser("api") + api_parser.add_argument("--bind", default="127.0.0.1:1337", help="The bind string.") + api_parser.add_argument("--debug", type=bool, default=False, help="Enable verbose logging") + api_parser.add_argument("--num-threads", type=int, default=8, help="The number of threads.") + api_parser.add_argument("--ignored-providers", nargs="+", choices=[provider.name for provider in IgnoredProviders], + default=[], help="List of providers to ignore when processing request.") subparsers.add_parser("gui", parents=[gui_parser()], add_help=False) args = parser.parse_args() if args.mode == "api": - Api(g4f).run('127.0.0.1:1337', 8) + controller=Api(g4f, debug=args.debug) + controller.list_ignored_providers=args.ignored_providers + controller.run(args.bind, args.num_threads) elif args.mode == "gui": run_gui_args(args) else: diff --git a/g4f_api/ApiInterface.py b/g4f_api/ApiInterface.py deleted file mode 100644 index f6572912..00000000 --- a/g4f_api/ApiInterface.py +++ /dev/null @@ -1,12 +0,0 @@ -import g4f -from g4f.api import Api - -create_chat_completion_original = g4f.ChatCompletion.create -list_ignored_providers=[] - -def create_chat_completion(*args, **kwargs): - kwargs['ignored']=list_ignored_providers - return create_chat_completion_original(*args, **kwargs) - -g4f.ChatCompletion.create=create_chat_completion -api=Api(g4f, debug=False) diff --git a/g4f_api/__init__.py b/g4f_api/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/g4f_api/__init__.py +++ /dev/null diff --git a/g4f_api/__main__.py b/g4f_api/__main__.py deleted file mode 100644 index 58267686..00000000 --- a/g4f_api/__main__.py +++ /dev/null @@ -1,23 +0,0 @@ -import typing -from enum import Enum -from g4f import Provider -from g4f_api import ApiInterface -import typer - -IgnoredProviders = Enum("ignore_providers", {key:key for key in Provider.__all__}) - -app = typer.Typer(help="Run the G4F API") - -@app.command() -def main( - bind_str: str = typer.Argument(..., envvar="G4F_API_BIND_STR", help="The bind string."), - i_num_threads: int = typer.Option(1, envvar="G4F_API_NUM_THREADS", help="The number of threads."), - list_ignored_providers: typing.List[IgnoredProviders] = typer.Option([], envvar="G4F_API_LIST_IGNORED_PROVIDERS", help="List of providers to ignore when processing request."), -): - list_ignored_providers=[provider.name for provider in list_ignored_providers] - ApiInterface.list_ignored_providers=list_ignored_providers - ApiInterface.api.run(bind_str, i_num_threads) - - -if __name__ == "__main__": - app()
\ No newline at end of file |