Spaces:
Paused
Paused
:gem: [Feature] HuggingchatStreamer: Build pipeline of chat_response
Browse files
networks/huggingchat_streamer.py
CHANGED
|
@@ -31,7 +31,6 @@ class HuggingchatStreamer:
|
|
| 31 |
self.model = "mixtral-8x7b"
|
| 32 |
self.model_fullname = MODEL_MAP[self.model]
|
| 33 |
self.message_outputer = OpenaiStreamOutputer(model=self.model)
|
| 34 |
-
# export HF_ENDPOINT=https://hf-mirror.com
|
| 35 |
self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)
|
| 36 |
|
| 37 |
def count_tokens(self, text):
|
|
@@ -155,7 +154,37 @@ class HuggingchatStreamer:
|
|
| 155 |
api_key: str = None,
|
| 156 |
use_cache: bool = False,
|
| 157 |
):
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
def chat_return_dict(self, stream_response):
|
| 161 |
pass
|
|
@@ -166,5 +195,6 @@ class HuggingchatStreamer:
|
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
streamer = HuggingchatStreamer(model="mixtral-8x7b")
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
| 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):
|
|
|
|
| 154 |
api_key: str = None,
|
| 155 |
use_cache: bool = False,
|
| 156 |
):
|
| 157 |
+
self.get_hf_chat_id()
|
| 158 |
+
self.get_conversation_id()
|
| 159 |
+
message_id = self.get_message_id()
|
| 160 |
+
|
| 161 |
+
request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"
|
| 162 |
+
request_headers = copy.deepcopy(HUGGINGCHAT_POST_HEADERS)
|
| 163 |
+
extra_headers = {
|
| 164 |
+
"Content-Type": "text/event-stream",
|
| 165 |
+
"Referer": request_url,
|
| 166 |
+
"Cookie": f"hf-chat={self.hf_chat_id}",
|
| 167 |
+
}
|
| 168 |
+
request_headers.update(extra_headers)
|
| 169 |
+
request_body = {
|
| 170 |
+
"files": [],
|
| 171 |
+
"id": message_id,
|
| 172 |
+
"inputs": prompt,
|
| 173 |
+
"is_continue": False,
|
| 174 |
+
"is_retry": False,
|
| 175 |
+
"web_search": False,
|
| 176 |
+
}
|
| 177 |
+
self.log_request(request_url, method="POST")
|
| 178 |
+
|
| 179 |
+
res = requests.post(
|
| 180 |
+
request_url,
|
| 181 |
+
headers=request_headers,
|
| 182 |
+
json=request_body,
|
| 183 |
+
proxies=PROXIES,
|
| 184 |
+
stream=True,
|
| 185 |
+
)
|
| 186 |
+
self.log_response(res, stream=True, iter_lines=True, verbose=True)
|
| 187 |
+
return res
|
| 188 |
|
| 189 |
def chat_return_dict(self, stream_response):
|
| 190 |
pass
|
|
|
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
| 197 |
streamer = HuggingchatStreamer(model="mixtral-8x7b")
|
| 198 |
+
prompt = "who are you?"
|
| 199 |
+
streamer.chat_response(prompt=prompt)
|
| 200 |
+
# HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer
|