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

XFEstudio/gpt4free

Fix Phind Provider / add generate_challenge

6aae1891
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +49 -3
Modified g4f/Provider/Phind.py +48 -2
@@ -1,5 +1,6 @@
1 1 from __future__ import annotations
2 2
3 from urllib import parse
3 4 from datetime import datetime
4 5
5 6 from ..typing import AsyncResult, Messages
@@ -55,9 +56,9 @@ class Phind(AsyncGeneratorProvider):
55 56 "customLinks": []
56 57 },
57 58 "context": "\n".join([message["content"] for message in messages if message["role"] == "system"]),
58 "rewrittenQuestion": prompt,
59 "challenge": 0.21132115912208504
60 59 }
60 data["challenge"] = generate_challenge(data)
61
61 62 async with session.post(f"https://https.api.phind.com/infer/", headers=headers, json=data) as response:
62 63 new_line = False
63 64 async for line in response.iter_lines():
@@ -65,6 +66,8 @@ class Phind(AsyncGeneratorProvider):
65 66 chunk = line[6:]
66 67 if chunk.startswith(b'<PHIND_DONE/>'):
67 68 break
69 if chunk.startswith(b'<PHIND_BACKEND_ERROR>'):
70 raise RuntimeError(f"Response: {chunk}")
68 71 if chunk.startswith(b'<PHIND_WEBRESULTS>') or chunk.startswith(b'<PHIND_FOLLOWUP>'):
69 72 pass
70 73 elif chunk.startswith(b"<PHIND_METADATA>") or chunk.startswith(b"<PHIND_INDICATOR>"):
@@ -78,3 +81,46 @@ class Phind(AsyncGeneratorProvider):
78 81 new_line = False
79 82 else:
80 83 new_line = True
84
85 def deterministic_stringify(obj):
86 def handle_value(value):
87 if isinstance(value, (dict, list)):
88 if isinstance(value, list):
89 return '[' + ','.join(sorted(map(handle_value, value))) + ']'
90 else: # It's a dict
91 return '{' + deterministic_stringify(value) + '}'
92 elif isinstance(value, bool):
93 return 'true' if value else 'false'
94 elif isinstance(value, (int, float)):
95 return format(value, '.8f').rstrip('0').rstrip('.')
96 elif isinstance(value, str):
97 return f'"{value}"'
98 else:
99 return 'null'
100
101 items = sorted(obj.items(), key=lambda x: x[0])
102 return ','.join([f'{k}:{handle_value(v)}' for k, v in items if handle_value(v) is not None])
103
104 def simple_hash(s):
105 d = 0
106 for char in s:
107 if len(char) > 1 or ord(char) >= 256:
108 continue
109 d = ((d << 5) - d + ord(char[0])) & 0xFFFFFFFF
110 if d > 0x7FFFFFFF: # 2147483647
111 d -= 0x100000000 # Subtract 2**32
112 return d
113
114 def generate_challenge(obj):
115 deterministic_str = deterministic_stringify(obj)
116 encoded_str = parse.quote(deterministic_str, safe='')
117
118 c = simple_hash(encoded_str)
119 a = (9301 * c + 49297)
120 b = 233280
121
122 # If negativ, we need a special logic
123 if a < 0:
124 return ((a%b)-b)/b
125 else:
126 return a%b/b
Modified g4f/models.py +1 -1
@@ -89,7 +89,7 @@ gpt_4 = Model(
89 89 name = 'gpt-4',
90 90 base_provider = 'openai',
91 91 best_provider = RetryProvider([
92 Bing, Phind, Liaobots,
92 Bing, Liaobots,
93 93 ])
94 94 )
95 95