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

XFEstudio/gpt4free

Add challenge_seeds in Phind

5071cd95
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +30 -17
Modified g4f/Provider/Phind.py +30 -17
@@ -1,5 +1,7 @@
1 1 from __future__ import annotations
2 2
3 import re
4 import json
3 5 from urllib import parse
4 6 from datetime import datetime
5 7
@@ -32,10 +34,18 @@ class Phind(AsyncGeneratorProvider):
32 34 "Sec-Fetch-Site": "same-origin",
33 35 }
34 36 async with StreamSession(
35 impersonate="chrome110",
37 headers=headers,
38 impersonate="chrome",
36 39 proxies={"https": proxy},
37 40 timeout=timeout
38 41 ) as session:
42 url = "https://www.phind.com/search?home=true"
43 async with session.get(url) as response:
44 text = await response.text()
45 match = re.search(r'<script id="__NEXT_DATA__" type="application/json">(?P<json>[\S\s]+?)</script>', text)
46 data = json.loads(match.group("json"))
47 challenge_seeds = data["props"]["pageProps"]["challengeSeeds"]
48
39 49 prompt = messages[-1]["content"]
40 50 data = {
41 51 "question": prompt,
@@ -51,14 +61,13 @@ class Phind(AsyncGeneratorProvider):
51 61 "language": "en-US",
52 62 "detailed": True,
53 63 "anonUserId": "",
54 "answerModel": "GPT-4" if model.startswith("gpt-4") else "Phind Model",
64 "answerModel": "GPT-4" if model.startswith("gpt-4") else "Phind-34B",
55 65 "creativeMode": creative_mode,
56 66 "customLinks": []
57 67 },
58 68 "context": "\n".join([message["content"] for message in messages if message["role"] == "system"]),
59 69 }
60 data["challenge"] = generate_challenge(data)
61
70 data["challenge"] = generate_challenge(data, **challenge_seeds)
62 71 async with session.post(f"https://https.api.phind.com/infer/", headers=headers, json=data) as response:
63 72 new_line = False
64 73 async for line in response.iter_lines():
@@ -101,6 +110,18 @@ def deterministic_stringify(obj):
101 110 items = sorted(obj.items(), key=lambda x: x[0])
102 111 return ','.join([f'{k}:{handle_value(v)}' for k, v in items if handle_value(v) is not None])
103 112
113 def prng_general(seed, multiplier, addend, modulus):
114 a = seed * multiplier + addend
115 if a < 0:
116 return ((a%modulus)-modulus)/modulus
117 else:
118 return a%modulus/modulus
119
120 def generate_challenge_seed(l):
121 I = deterministic_stringify(l)
122 d = parse.quote(I, safe='')
123 return simple_hash(d)
124
104 125 def simple_hash(s):
105 126 d = 0
106 127 for char in s:
@@ -111,16 +132,8 @@ def simple_hash(s):
111 132 d -= 0x100000000 # Subtract 2**32
112 133 return d
113 134
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
135 def generate_challenge(obj, **kwargs):
136 return prng_general(
137 seed=generate_challenge_seed(obj),
138 **kwargs
139 )