Husnain commited on
Commit
dbc5434
1 Parent(s): 61ebb7f

⚡ [Enhance] HuggingchatStreamer: logger auth messages

Browse files
Files changed (1) hide show
  1. networks/huggingchat_streamer.py +122 -52
networks/huggingchat_streamer.py CHANGED
@@ -1,43 +1,27 @@
1
  import copy
2
  import json
3
  import re
 
4
  import requests
5
- import uuid
6
 
7
- # from curl_cffi import requests
8
  from tclogger import logger
9
- from transformers import AutoTokenizer
10
-
11
- from constants.models import (
12
- MODEL_MAP,
13
- STOP_SEQUENCES_MAP,
14
- TOKEN_LIMIT_MAP,
15
- TOKEN_RESERVED,
16
- )
17
  from constants.envs import PROXIES
18
- from constants.headers import (
19
- REQUESTS_HEADERS,
20
- HUGGINGCHAT_POST_HEADERS,
21
- HUGGINGCHAT_SETTINGS_POST_DATA,
22
- )
23
  from messagers.message_outputer import OpenaiStreamOutputer
 
 
24
 
25
 
26
- class HuggingchatStreamer:
27
  def __init__(self, model: str):
28
  if model in MODEL_MAP.keys():
29
  self.model = model
30
  else:
31
- self.model = "mixtral-8x7b"
32
  self.model_fullname = MODEL_MAP[self.model]
33
- self.message_outputer = OpenaiStreamOutputer(model=self.model)
34
- # self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)
35
-
36
- # def count_tokens(self, text):
37
- # tokens = self.tokenizer.encode(text)
38
- # token_count = len(tokens)
39
- # logger.note(f"Prompt Token Count: {token_count}")
40
- # return token_count
41
 
42
  def get_hf_chat_id(self):
43
  request_url = "https://huggingface.co/chat/settings"
@@ -48,12 +32,13 @@ class HuggingchatStreamer:
48
  request_body.update(extra_body)
49
  logger.note(f"> hf-chat ID:", end=" ")
50
 
51
- res = requests.post(
52
  request_url,
53
  headers=HUGGINGCHAT_POST_HEADERS,
54
  json=request_body,
55
  proxies=PROXIES,
56
  timeout=10,
 
57
  )
58
  self.hf_chat_id = res.cookies.get("hf-chat")
59
  if self.hf_chat_id:
@@ -61,9 +46,9 @@ class HuggingchatStreamer:
61
  else:
62
  logger.warn(f"[{res.status_code}]")
63
  logger.warn(res.text)
64
- raise ValueError("Failed to get hf-chat ID!")
65
 
66
- def get_conversation_id(self, preprompt: str = ""):
67
  request_url = "https://huggingface.co/chat/conversation"
68
  request_headers = HUGGINGCHAT_POST_HEADERS
69
  extra_headers = {
@@ -72,7 +57,7 @@ class HuggingchatStreamer:
72
  request_headers.update(extra_headers)
73
  request_body = {
74
  "model": self.model_fullname,
75
- "preprompt": preprompt,
76
  }
77
  logger.note(f"> Conversation ID:", end=" ")
78
 
@@ -120,7 +105,7 @@ class HuggingchatStreamer:
120
  logger.success(f"[{message_id}]")
121
  else:
122
  logger.warn(f"[{res.status_code}]")
123
- raise ValueError("Failed to get conversation ID!")
124
 
125
  return message_id
126
 
@@ -139,9 +124,8 @@ class HuggingchatStreamer:
139
  else:
140
  logger_func = logger.warn
141
 
142
- logger_func(status_code_str)
143
-
144
  logger.enter_quiet(not verbose)
 
145
 
146
  if status_code != 200:
147
  logger_func(res.text)
@@ -176,17 +160,17 @@ class HuggingchatStreamer:
176
 
177
  logger.exit_quiet(not verbose)
178
 
179
- def chat_response(
180
- self,
181
- prompt: str = None,
182
- temperature: float = 0.5,
183
- top_p: float = 0.95,
184
- max_new_tokens: int = None,
185
- api_key: str = None,
186
- use_cache: bool = False,
187
- ):
188
  self.get_hf_chat_id()
189
- self.get_conversation_id()
190
  message_id = self.get_last_message_id()
191
 
192
  request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"
@@ -200,7 +184,7 @@ class HuggingchatStreamer:
200
  request_body = {
201
  "files": [],
202
  "id": message_id,
203
- "inputs": prompt,
204
  "is_continue": False,
205
  "is_retry": False,
206
  "web_search": False,
@@ -214,20 +198,106 @@ class HuggingchatStreamer:
214
  proxies=PROXIES,
215
  stream=True,
216
  )
217
- self.log_response(res, stream=True, iter_lines=True, verbose=True)
218
  return res
219
 
220
- def chat_return_dict(self, stream_response):
221
- pass
222
 
223
- def chat_return_generator(self, stream_response):
224
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
 
227
  if __name__ == "__main__":
228
- # model = "llama3-70b"
229
- model = "command-r-plus"
 
 
230
  streamer = HuggingchatStreamer(model=model)
231
- prompt = "what is your model?"
232
- streamer.chat_response(prompt=prompt)
 
 
 
 
 
 
 
 
 
233
  # HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer
 
1
  import copy
2
  import json
3
  import re
4
+
5
  import requests
6
+ from curl_cffi import requests as cffi_requests
7
 
 
8
  from tclogger import logger
9
+
10
+ from constants.models import MODEL_MAP
 
 
 
 
 
 
11
  from constants.envs import PROXIES
12
+ from constants.headers import HUGGINGCHAT_POST_HEADERS, HUGGINGCHAT_SETTINGS_POST_DATA
 
 
 
 
13
  from messagers.message_outputer import OpenaiStreamOutputer
14
+ from messagers.message_composer import MessageComposer
15
+ from messagers.token_checker import TokenChecker
16
 
17
 
18
+ class HuggingchatRequester:
19
  def __init__(self, model: str):
20
  if model in MODEL_MAP.keys():
21
  self.model = model
22
  else:
23
+ self.model = "nous-mixtral-8x7b"
24
  self.model_fullname = MODEL_MAP[self.model]
 
 
 
 
 
 
 
 
25
 
26
  def get_hf_chat_id(self):
27
  request_url = "https://huggingface.co/chat/settings"
 
32
  request_body.update(extra_body)
33
  logger.note(f"> hf-chat ID:", end=" ")
34
 
35
+ res = cffi_requests.post(
36
  request_url,
37
  headers=HUGGINGCHAT_POST_HEADERS,
38
  json=request_body,
39
  proxies=PROXIES,
40
  timeout=10,
41
+ impersonate="chrome",
42
  )
43
  self.hf_chat_id = res.cookies.get("hf-chat")
44
  if self.hf_chat_id:
 
46
  else:
47
  logger.warn(f"[{res.status_code}]")
48
  logger.warn(res.text)
49
+ raise ValueError(f"Failed to get hf-chat ID: {res.text}")
50
 
51
+ def get_conversation_id(self, system_prompt: str = ""):
52
  request_url = "https://huggingface.co/chat/conversation"
53
  request_headers = HUGGINGCHAT_POST_HEADERS
54
  extra_headers = {
 
57
  request_headers.update(extra_headers)
58
  request_body = {
59
  "model": self.model_fullname,
60
+ "preprompt": system_prompt,
61
  }
62
  logger.note(f"> Conversation ID:", end=" ")
63
 
 
105
  logger.success(f"[{message_id}]")
106
  else:
107
  logger.warn(f"[{res.status_code}]")
108
+ raise ValueError("Failed to get message ID!")
109
 
110
  return message_id
111
 
 
124
  else:
125
  logger_func = logger.warn
126
 
 
 
127
  logger.enter_quiet(not verbose)
128
+ logger_func(status_code_str)
129
 
130
  if status_code != 200:
131
  logger_func(res.text)
 
160
 
161
  logger.exit_quiet(not verbose)
162
 
163
+ def chat_completions(self, messages: list[dict], iter_lines=False, verbose=False):
164
+ composer = MessageComposer(model=self.model)
165
+ system_prompt, input_prompt = composer.decompose_to_system_and_input_prompt(
166
+ messages
167
+ )
168
+
169
+ checker = TokenChecker(input_str=system_prompt + input_prompt, model=self.model)
170
+ checker.check_token_limit()
171
+
172
  self.get_hf_chat_id()
173
+ self.get_conversation_id(system_prompt=system_prompt)
174
  message_id = self.get_last_message_id()
175
 
176
  request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"
 
184
  request_body = {
185
  "files": [],
186
  "id": message_id,
187
+ "inputs": input_prompt,
188
  "is_continue": False,
189
  "is_retry": False,
190
  "web_search": False,
 
198
  proxies=PROXIES,
199
  stream=True,
200
  )
201
+ self.log_response(res, stream=True, iter_lines=iter_lines, verbose=verbose)
202
  return res
203
 
 
 
204
 
205
+ class HuggingchatStreamer:
206
+ def __init__(self, model: str):
207
+ if model in MODEL_MAP.keys():
208
+ self.model = model
209
+ else:
210
+ self.model = "nous-mixtral-8x7b"
211
+ self.model_fullname = MODEL_MAP[self.model]
212
+ self.message_outputer = OpenaiStreamOutputer(model=self.model)
213
+
214
+ def chat_response(self, messages: list[dict], verbose=False):
215
+ requester = HuggingchatRequester(model=self.model)
216
+ return requester.chat_completions(
217
+ messages=messages, iter_lines=False, verbose=verbose
218
+ )
219
+
220
+ def chat_return_generator(self, stream_response: requests.Response, verbose=False):
221
+ is_finished = False
222
+ for line in stream_response.iter_lines():
223
+ line = line.decode("utf-8")
224
+ line = re.sub(r"^data:\s*", "", line)
225
+ line = line.strip()
226
+ if not line:
227
+ continue
228
+
229
+ content = ""
230
+ content_type = "Completions"
231
+ try:
232
+ data = json.loads(line, strict=False)
233
+ msg_type = data.get("type")
234
+ if msg_type == "status":
235
+ msg_status = data.get("status")
236
+ continue
237
+ elif msg_type == "stream":
238
+ content_type = "Completions"
239
+ content = data.get("token", "")
240
+ if verbose:
241
+ logger.success(content, end="")
242
+ elif msg_type == "finalAnswer":
243
+ content_type = "Finished"
244
+ content = ""
245
+ full_content = data.get("text")
246
+ if verbose:
247
+ logger.success("\n[Finished]")
248
+ is_finished = True
249
+ break
250
+ else:
251
+ continue
252
+ except Exception as e:
253
+ logger.warn(e)
254
+
255
+ output = self.message_outputer.output(
256
+ content=content, content_type=content_type
257
+ )
258
+ yield output
259
+
260
+ if not is_finished:
261
+ yield self.message_outputer.output(content="", content_type="Finished")
262
+
263
+ def chat_return_dict(self, stream_response: requests.Response):
264
+ final_output = self.message_outputer.default_data.copy()
265
+ final_output["choices"] = [
266
+ {
267
+ "index": 0,
268
+ "finish_reason": "stop",
269
+ "message": {"role": "assistant", "content": ""},
270
+ }
271
+ ]
272
+ final_content = ""
273
+ for item in self.chat_return_generator(stream_response):
274
+ try:
275
+ data = json.loads(item)
276
+ delta = data["choices"][0]["delta"]
277
+ delta_content = delta.get("content", "")
278
+ if delta_content:
279
+ final_content += delta_content
280
+ except Exception as e:
281
+ logger.warn(e)
282
+ final_output["choices"][0]["message"]["content"] = final_content.strip()
283
+ return final_output
284
 
285
 
286
  if __name__ == "__main__":
287
+ # model = "command-r-plus"
288
+ model = "llama3-70b"
289
+ # model = "zephyr-141b"
290
+
291
  streamer = HuggingchatStreamer(model=model)
292
+ messages = [
293
+ {
294
+ "role": "system",
295
+ "content": "You are an LLM developed by CloseAI.\nYour name is Niansuh-Copilot.",
296
+ },
297
+ {"role": "user", "content": "Hello, what is your role?"},
298
+ {"role": "assistant", "content": "I am an LLM."},
299
+ {"role": "user", "content": "What is your name?"},
300
+ ]
301
+
302
+ streamer.chat_response(messages=messages)
303
  # HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer