XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

~ | new `test_providers.py`

46398e8a
abc <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

2 个文件 +99 -0
Modified etc/testing/test_providers.py +33 -0
@@ -1,3 +1,36 @@
1 # from g4f.Provider import __all__, ProviderUtils
2 # from g4f import ChatCompletion
3 # import concurrent.futures
4
5 # _ = [
6 # 'BaseProvider',
7 # 'AsyncProvider',
8 # 'AsyncGeneratorProvider',
9 # 'RetryProvider'
10 # ]
11
12 # def test_provider(provider):
13 # try:
14 # provider = (ProviderUtils.convert[provider])
15 # if provider.working and not provider.needs_auth:
16 # print('testing', provider.__name__)
17 # completion = ChatCompletion.create(model='gpt-3.5-turbo',
18 # messages=[{"role": "user", "content": "hello"}], provider=provider)
19 # return completion, provider.__name__
20 # except Exception as e:
21 # #print(f'Failed to test provider: {provider} | {e}')
22 # return None
23
24 # with concurrent.futures.ThreadPoolExecutor() as executor:
25 # futures = []
26 # for provider in __all__:
27 # if provider not in _:
28 # futures.append(executor.submit(test_provider, provider))
29 # for future in concurrent.futures.as_completed(futures):
30 # result = future.result()
31 # if result:
32 # print(f'{result[1]} | {result[0]}')
33
1 34 import sys
2 35 from pathlib import Path
3 36 from colorama import Fore, Style
Added etc/testing/test_providers.v1.py +66 -0
@@ -0,0 +1,66 @@
1 import sys
2 from pathlib import Path
3 from colorama import Fore, Style
4
5 sys.path.append(str(Path(__file__).parent.parent))
6
7 from g4f import BaseProvider, models, Provider
8
9 logging = False
10
11
12 def main():
13 providers = get_providers()
14 failed_providers = []
15
16 for _provider in providers:
17 if _provider.needs_auth:
18 continue
19 print("Provider:", _provider.__name__)
20 result = test(_provider)
21 print("Result:", result)
22 if _provider.working and not result:
23 failed_providers.append(_provider)
24
25 print()
26
27 if failed_providers:
28 print(f"{Fore.RED + Style.BRIGHT}Failed providers:{Style.RESET_ALL}")
29 for _provider in failed_providers:
30 print(f"{Fore.RED}{_provider.__name__}")
31 else:
32 print(f"{Fore.GREEN + Style.BRIGHT}All providers are working")
33
34
35 def get_providers() -> list[type[BaseProvider]]:
36 providers = dir(Provider)
37 providers = [getattr(Provider, provider) for provider in providers if provider != "RetryProvider"]
38 providers = [provider for provider in providers if isinstance(provider, type)]
39 return [provider for provider in providers if issubclass(provider, BaseProvider)]
40
41
42 def create_response(_provider: type[BaseProvider]) -> str:
43 model = models.gpt_35_turbo.name if _provider.supports_gpt_35_turbo else models.default.name
44 response = _provider.create_completion(
45 model=model,
46 messages=[{"role": "user", "content": "Hello, who are you? Answer in detail much as possible."}],
47 stream=False,
48 )
49 return "".join(response)
50
51
52 def test(_provider: type[BaseProvider]) -> bool:
53 try:
54 response = create_response(_provider)
55 assert type(response) is str
56 assert len(response) > 0
57 return response
58 except Exception as e:
59 if logging:
60 print(e)
61 return False
62
63
64 if __name__ == "__main__":
65 main()
66