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

XFEstudio/gpt4free

Improve providers with tests

98d33041
Heiner Lohaus <heiner.lohaus@netformic.com>
提交于

代码差异

7 个文件 +40 -40
Modified g4f/Provider/DfeHub.py +1 -0
@@ -50,6 +50,7 @@ class DfeHub(BaseProvider):
50 50 "https://chat.dfehub.com/api/openai/v1/chat/completions",
51 51 headers=headers,
52 52 json=json_data,
53 timeout=3
53 54 )
54 55
55 56 for chunk in response.iter_lines():
Modified g4f/Provider/FastGpt.py +1 -1
@@ -6,7 +6,7 @@ from ..typing import Any, CreateResult
6 6
7 7 class FastGpt(ABC):
8 8 url: str = 'https://chat9.fastgpt.me/'
9 working = True
9 working = False
10 10 needs_auth = False
11 11 supports_stream = True
12 12 supports_gpt_35_turbo = True
Modified g4f/Provider/H2o.py +5 -3
@@ -11,6 +11,7 @@ class H2o(BaseProvider):
11 11 url = "https://gpt-gm.h2o.ai"
12 12 working = True
13 13 supports_stream = True
14 model = "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-40b-v1"
14 15
15 16 @staticmethod
16 17 def create_completion(
@@ -47,8 +48,9 @@ class H2o(BaseProvider):
47 48 "https://gpt-gm.h2o.ai/conversation",
48 49 headers=headers,
49 50 json=data,
50 )
51 conversation_id = response.json()["conversationId"]
51 ).json()
52 if "conversationId" not in response:
53 return
52 54
53 55 data = {
54 56 "inputs": conversation,
@@ -71,7 +73,7 @@ class H2o(BaseProvider):
71 73 }
72 74
73 75 response = session.post(
74 f"https://gpt-gm.h2o.ai/conversation/{conversation_id}",
76 f"https://gpt-gm.h2o.ai/conversation/{response['conversationId']}",
75 77 headers=headers,
76 78 json=data,
77 79 )
Modified g4f/Provider/V50.py +3 -2
@@ -8,7 +8,7 @@ class V50(BaseProvider):
8 8 supports_gpt_35_turbo = True
9 9 supports_stream = False
10 10 needs_auth = False
11 working = True
11 working = False
12 12
13 13 @staticmethod
14 14 def create_completion(
@@ -46,7 +46,8 @@ class V50(BaseProvider):
46 46 }
47 47 response = requests.post("https://p5.v50.ltd/api/chat-process",
48 48 json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {})
49 yield response.text
49 if "https://fk1.v50.ltd" not in response.text:
50 yield response.text
50 51
51 52 @classmethod
52 53 @property
Modified g4f/Provider/Wewordle.py +1 -6
@@ -21,11 +21,6 @@ class Wewordle(BaseProvider):
21 21 stream: bool,
22 22 **kwargs: Any,
23 23 ) -> CreateResult:
24 base = ""
25
26 for message in messages:
27 base += "%s: %s\n" % (message["role"], message["content"])
28 base += "assistant:"
29 24 # randomize user id and app id
30 25 _user_id = "".join(
31 26 random.choices(f"{string.ascii_lowercase}{string.digits}", k=16)
@@ -45,7 +40,7 @@ class Wewordle(BaseProvider):
45 40 }
46 41 data: dict[str, Any] = {
47 42 "user": _user_id,
48 "messages": [{"role": "user", "content": base}],
43 "messages": messages,
49 44 "subscriber": {
50 45 "originalPurchaseDate": None,
51 46 "originalApplicationVersion": None,
Modified g4f/Provider/You.py +7 -8
@@ -1,5 +1,6 @@
1 1 import re
2 2 import urllib.parse
3 import json
3 4
4 5 from curl_cffi import requests
5 6
@@ -28,7 +29,11 @@ class You(BaseProvider):
28 29 impersonate="chrome107",
29 30 )
30 31 response.raise_for_status()
31 yield _parse_output(response.text)
32 start = 'data: {"youChatToken": '
33 for line in response.content.splitlines():
34 line = line.decode('utf-8')
35 if line.startswith(start):
36 yield json.loads(line[len(start): -1])
32 37
33 38
34 39 def _create_url_param(messages: list[dict[str, str]]):
@@ -50,10 +55,4 @@ def _create_header():
50 55 return {
51 56 "accept": "text/event-stream",
52 57 "referer": "https://you.com/search?fromSearchBar=true&tbm=youchat",
53 }
54
55
56 def _parse_output(output: str) -> str:
57 regex = r"^data:\s{\"youChatToken\": \"(.*)\"}$"
58 tokens = [token for token in re.findall(regex, output, re.MULTILINE)]
59 return "".join(tokens)
58 }
Modified testing/test_providers.py +22 -20
@@ -3,50 +3,51 @@ from pathlib import Path
3 3
4 4 sys.path.append(str(Path(__file__).parent.parent))
5 5
6 from g4f import BaseProvider, models, provider
6 from g4f import BaseProvider, models, Provider
7 7
8 logging = False
8 9
9 10 def main():
10 11 providers = get_providers()
11 results: list[list[str | bool]] = []
12 failed_providers = []
12 13
13 14 for _provider in providers:
14 print("start", _provider.__name__)
15 actual_working = judge(_provider)
16 expected_working = _provider.working
17 match = actual_working == expected_working
15 if _provider.needs_auth:
16 continue
17 print("Provider:", _provider.__name__)
18 result = judge(_provider)
19 print("Result:", result)
20 if _provider.working and not result:
21 failed_providers.append([_provider, result])
18 22
19 results.append([_provider.__name__, expected_working, actual_working, match])
20
21 print("failed provider list")
22 for result in results:
23 if not result[3]:
24 print(result)
23 print("Failed providers:")
24 for _provider, result in failed_providers:
25 print(f"{_provider.__name__}: {result}")
25 26
26 27
27 28 def get_providers() -> list[type[BaseProvider]]:
28 provider_names = dir(provider)
29 provider_names = dir(Provider)
29 30 ignore_names = [
30 31 "base_provider",
31 "BaseProvider",
32 "BaseProvider"
32 33 ]
33 34 provider_names = [
34 35 provider_name
35 36 for provider_name in provider_names
36 37 if not provider_name.startswith("__") and provider_name not in ignore_names
37 38 ]
38 return [getattr(provider, provider_name) for provider_name in provider_names]
39 return [getattr(Provider, provider_name) for provider_name in provider_names]
39 40
40 41
41 42 def create_response(_provider: type[BaseProvider]) -> str:
42 43 model = (
43 44 models.gpt_35_turbo.name
44 if _provider is not provider.H2o
45 else models.falcon_7b.name
45 if _provider.supports_gpt_35_turbo
46 else _provider.model
46 47 )
47 48 response = _provider.create_completion(
48 49 model=model,
49 messages=[{"role": "user", "content": "Hello world!, plz yourself"}],
50 messages=[{"role": "user", "content": "Hello world!"}],
50 51 stream=False,
51 52 )
52 53 return "".join(response)
@@ -59,9 +60,10 @@ def judge(_provider: type[BaseProvider]) -> bool:
59 60 try:
60 61 response = create_response(_provider)
61 62 assert type(response) is str
62 return len(response) > 1
63 return response
63 64 except Exception as e:
64 print(e)
65 if logging:
66 print(e)
65 67 return False
66 68
67 69