返回提交历史
Modified
g4f/Provider/HuggingChat.py
+57
-70
Modified
g4f/models.py
+3
-0
XFEstudio/gpt4free
update huggingface provider
8ef5207a
代码差异
2 个文件
+60
-70
@@ -33,80 +33,78 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
33
33
model: str,
34
34
messages: Messages,
35
35
stream: bool,
36
**kwargs
37
) -> CreateResult:
36
**kwargs) -> CreateResult:
38
37
39
38
if (model in cls.models) :
40
39
41
session = requests.Session()
42
headers = {
43
'accept' : '*/*',
44
'accept-language' : 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
45
'cache-control' : 'no-cache',
46
'origin' : 'https://huggingface.co',
47
'pragma' : 'no-cache',
48
'priority' : 'u=1, i',
49
'referer' : 'https://huggingface.co/chat/',
50
'sec-ch-ua' : '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
51
'sec-ch-ua-mobile' : '?0',
40
session = cf_reqs.Session()
41
session.headers = {
42
'accept': '*/*',
43
'accept-language': 'en',
44
'cache-control': 'no-cache',
45
'origin': 'https://huggingface.co',
46
'pragma': 'no-cache',
47
'priority': 'u=1, i',
48
'referer': 'https://huggingface.co/chat/',
49
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
50
'sec-ch-ua-mobile': '?0',
52
51
'sec-ch-ua-platform': '"macOS"',
53
'sec-fetch-dest' : 'empty',
54
'sec-fetch-mode' : 'cors',
55
'sec-fetch-site' : 'same-origin',
56
'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
52
'sec-fetch-dest': 'empty',
53
'sec-fetch-mode': 'cors',
54
'sec-fetch-site': 'same-origin',
55
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
57
56
}
58
57
58
print(model)
59
59
json_data = {
60
'searchEnabled' : True,
61
'activeModel' : 'CohereForAI/c4ai-command-r-plus', # doesn't matter
62
'hideEmojiOnSidebar': False,
63
'customPrompts' : {},
64
'assistants' : [],
65
'tools' : {},
66
'disableStream' : False,
67
'recentlySaved' : False,
68
'ethicsModalAccepted' : True,
69
'ethicsModalAcceptedAt' : None,
70
'shareConversationsWithModelAuthors': False,
60
'model': model,
71
61
}
72
62
73
response = cf_reqs.post('https://huggingface.co/chat/settings', headers=headers, json=json_data)
74
session.cookies.update(response.cookies)
75
76
response = session.post('https://huggingface.co/chat/conversation',
77
headers=headers, json={'model': model})
78
63
response = session.post('https://huggingface.co/chat/conversation', json=json_data)
79
64
conversationId = response.json()['conversationId']
80
response = session.get(f'https://huggingface.co/chat/conversation/{conversationId}/__data.json?x-sveltekit-invalidated=11',
81
headers=headers,
82
)
83
65
84
messageId = extract_id(response.json())
66
response = session.get(f'https://huggingface.co/chat/conversation/{conversationId}/__data.json?x-sveltekit-invalidated=01',)
67
68
data: list = (response.json())["nodes"][1]["data"]
69
keys: list[int] = data[data[0]["messages"]]
70
message_keys: dict = data[keys[0]]
71
messageId: str = data[message_keys["id"]]
85
72
86
73
settings = {
87
"inputs" : format_prompt(messages),
88
"id" : messageId,
89
"is_retry" : False,
90
"is_continue" : False,
91
"web_search" : False,
92
93
# TODO // add feature to enable/disable tools
94
"tools": {
95
"websearch" : True,
96
"document_parser" : False,
97
"query_calculator" : False,
98
"image_generation" : False,
99
"image_editing" : False,
100
"fetch_url" : False,
101
}
74
"inputs":format_prompt(messages),
75
"id":messageId,
76
"is_retry":False,
77
"is_continue":False,
78
"web_search":False,
79
"tools":[]
102
80
}
103
81
104
payload = {
105
"data": json.dumps(settings),
82
headers = {
83
'accept': '*/*',
84
'accept-language': 'en',
85
'cache-control': 'no-cache',
86
'origin': 'https://huggingface.co',
87
'pragma': 'no-cache',
88
'priority': 'u=1, i',
89
'referer': f'https://huggingface.co/chat/conversation/{conversationId}',
90
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
91
'sec-ch-ua-mobile': '?0',
92
'sec-ch-ua-platform': '"macOS"',
93
'sec-fetch-dest': 'empty',
94
'sec-fetch-mode': 'cors',
95
'sec-fetch-site': 'same-origin',
96
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
97
}
98
99
100
files = {
101
'data': (None, json.dumps(settings, separators=(',', ':'))),
106
102
}
107
103
108
response = session.post(f"https://huggingface.co/chat/conversation/{conversationId}",
109
headers=headers, data=payload, stream=True,
104
response = requests.post(f'https://huggingface.co/chat/conversation/{conversationId}',
105
cookies=session.cookies,
106
headers=headers,
107
files=files,
110
108
)
111
109
112
110
first_token = True
@@ -125,18 +123,7 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
125
123
else:
126
124
token = token.replace('\u0000', '')
127
125
128
yield token
126
yield (token)
129
127
130
128
elif line["type"] == "finalAnswer":
131
break
132
133
def extract_id(response: dict) -> str:
134
data = response["nodes"][1]["data"]
135
uuid_pattern = re.compile(
136
r"^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$"
137
)
138
for item in data:
139
if type(item) == str and uuid_pattern.match(item):
140
return item
141
142
return None
129
break
@@ -508,6 +508,9 @@ class ModelUtils:
508
508
'llama-3-70b-instruct': llama_3_70b_instruct,
509
509
'llama-3-70b-chat': llama_3_70b_chat_hf,
510
510
'llama-3-70b-instruct': llama_3_70b_instruct,
511
512
'llama-3.1-70b': llama_3_1_70b_instruct,
513
'llama-3.1-405b': llama_3_1_405b_instruct_FP8,
511
514
'llama-3.1-70b-instruct': llama_3_1_70b_instruct,
512
515
'llama-3.1-405b-instruct': llama_3_1_405b_instruct_FP8,
513
516