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

XFEstudio/gpt4free

feat(g4f/Provider/HuggingChat.py: Enhance HuggingChat provider functionality

fde29c53
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

2 个文件 +49 -7
Modified g4f/Provider/HuggingChat.py +39 -7
@@ -19,6 +19,7 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
19 19 'CohereForAI/c4ai-command-r-plus-08-2024',
20 20 'Qwen/Qwen2.5-72B-Instruct',
21 21 'nvidia/Llama-3.1-Nemotron-70B-Instruct-HF',
22 'Qwen/Qwen2.5-Coder-32B-Instruct',
22 23 'meta-llama/Llama-3.2-11B-Vision-Instruct',
23 24 'NousResearch/Hermes-3-Llama-3.1-8B',
24 25 'mistralai/Mistral-Nemo-Instruct-2407',
@@ -30,6 +31,7 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
30 31 "command-r-plus": "CohereForAI/c4ai-command-r-plus-08-2024",
31 32 "qwen-2-72b": "Qwen/Qwen2.5-72B-Instruct",
32 33 "nemotron-70b": "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
34 "qwen-2.5-coder-32b": "Qwen/Qwen2.5-Coder-32B-Instruct",
33 35 "llama-3.2-11b": "meta-llama/Llama-3.2-11B-Vision-Instruct",
34 36 "hermes-3": "NousResearch/Hermes-3-Llama-3.1-8B",
35 37 "mistral-nemo": "mistralai/Mistral-Nemo-Instruct-2407",
@@ -83,12 +85,33 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
83 85 raise RuntimeError(f"Request failed with status code: {response.status_code}, response: {response.text}")
84 86
85 87 conversationId = response.json().get('conversationId')
86 response = session.get(f'https://huggingface.co/chat/conversation/{conversationId}/__data.json?x-sveltekit-invalidated=01')
88
89 # Get the data response and parse it properly
90 response = session.get(f'https://huggingface.co/chat/conversation/{conversationId}/__data.json?x-sveltekit-invalidated=11')
91
92 # Split the response content by newlines and parse each line as JSON
93 try:
94 json_data = None
95 for line in response.text.split('\n'):
96 if line.strip():
97 try:
98 parsed = json.loads(line)
99 if isinstance(parsed, dict) and "nodes" in parsed:
100 json_data = parsed
101 break
102 except json.JSONDecodeError:
103 continue
104
105 if not json_data:
106 raise RuntimeError("Failed to parse response data")
107
108 data: list = json_data["nodes"][1]["data"]
109 keys: list[int] = data[data[0]["messages"]]
110 message_keys: dict = data[keys[0]]
111 messageId: str = data[message_keys["id"]]
87 112
88 data: list = response.json()["nodes"][1]["data"]
89 keys: list[int] = data[data[0]["messages"]]
90 message_keys: dict = data[keys[0]]
91 messageId: str = data[message_keys["id"]]
113 except (KeyError, IndexError, TypeError) as e:
114 raise RuntimeError(f"Failed to extract message ID: {str(e)}")
92 115
93 116 settings = {
94 117 "inputs": format_prompt(messages),
@@ -120,7 +143,8 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
120 143 'data': (None, json.dumps(settings, separators=(',', ':'))),
121 144 }
122 145
123 response = requests.post(f'https://huggingface.co/chat/conversation/{conversationId}',
146 response = requests.post(
147 f'https://huggingface.co/chat/conversation/{conversationId}',
124 148 cookies=session.cookies,
125 149 headers=headers,
126 150 files=files,
@@ -142,10 +166,18 @@ class HuggingChat(AbstractProvider, ProviderModelMixin):
142 166 elif line["type"] == "stream":
143 167 token = line["token"].replace('\u0000', '')
144 168 full_response += token
169 if stream:
170 yield token
145 171
146 172 elif line["type"] == "finalAnswer":
147 173 break
148 174
149 175 full_response = full_response.replace('<|im_end|', '').replace('\u0000', '').strip()
150 176
151 yield full_response
177 if not stream:
178 yield full_response
179
180 @classmethod
181 def supports_model(cls, model: str) -> bool:
182 """Check if the model is supported by the provider."""
183 return model in cls.models or model in cls.model_aliases
Modified g4f/models.py +10 -0
@@ -361,6 +361,13 @@ qwen_2_72b = Model(
361 361 best_provider = IterListProvider([DeepInfraChat, HuggingChat, HuggingFace])
362 362 )
363 363
364 # qwen 2.5
365 qwen_2_5_coder_32b = Model(
366 name = 'qwen-2.5-coder-32b',
367 base_provider = 'Qwen',
368 best_provider = IterListProvider([HuggingChat, HuggingFace])
369 )
370
364 371 ### Upstage ###
365 372 solar_mini = Model(
366 373 name = 'solar-mini',
@@ -703,6 +710,9 @@ class ModelUtils:
703 710
704 711 # qwen 2
705 712 'qwen-2-72b': qwen_2_72b,
713
714 # qwen 2.5
715 'qwen-2.5-coder-32b': qwen_2_5_coder_32b,
706 716
707 717
708 718 ### Upstage ###