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

XFEstudio/gpt4free

~ | new providers test

8bce9ace
abc <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

1 个文件 +30 -97
Modified etc/testing/test_providers.py +30 -97
@@ -1,99 +1,32 @@
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
34 import sys
35 from pathlib import Path
36 from colorama import Fore, Style
37
38 sys.path.append(str(Path(__file__).parent.parent))
39
40 from g4f import BaseProvider, models, Provider
41
42 logging = False
43
44
45 def main():
46 providers = get_providers()
47 failed_providers = []
48
49 for _provider in providers:
50 if _provider.needs_auth:
51 continue
52 print("Provider:", _provider.__name__)
53 result = test(_provider)
54 print("Result:", result)
55 if _provider.working and not result:
56 failed_providers.append(_provider)
57
58 print()
59
60 if failed_providers:
61 print(f"{Fore.RED + Style.BRIGHT}Failed providers:{Style.RESET_ALL}")
62 for _provider in failed_providers:
63 print(f"{Fore.RED}{_provider.__name__}")
64 else:
65 print(f"{Fore.GREEN + Style.BRIGHT}All providers are working")
66
67
68 def get_providers() -> list[type[BaseProvider]]:
69 providers = dir(Provider)
70 providers = [getattr(Provider, provider) for provider in providers if provider != "RetryProvider"]
71 providers = [provider for provider in providers if isinstance(provider, type)]
72 return [provider for provider in providers if issubclass(provider, BaseProvider)]
73
74
75 def create_response(_provider: type[BaseProvider]) -> str:
76 model = models.gpt_35_turbo.name if _provider.supports_gpt_35_turbo else models.default.name
77 response = _provider.create_completion(
78 model=model,
79 messages=[{"role": "user", "content": "Hello, who are you? Answer in detail much as possible."}],
80 stream=False,
81 )
82 return "".join(response)
83
84
85 def test(_provider: type[BaseProvider]) -> bool:
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):
86 13 try:
87 response = create_response(_provider)
88 assert type(response) is str
89 assert len(response) > 0
90 return response
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__
91 20 except Exception as e:
92 if logging:
93 print(e)
94 return False
95
96
97 if __name__ == "__main__":
98 main()
99
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]}')