Spaces:
Running
Running
:gem: [Feature] Enable chat response with get_message_id
Browse filesFiles changed (2)
4 unmodified lines567891016 unmodified lines2728293031324 unmodified lines# "openchat-3.5": "openchat/openchat-3.5-0106","gemma-7b": "google/gemma-1.1-7b-it","command-r-plus": "CohereForAI/c4ai-command-r-plus","default": "mistralai/Mixtral-8x7B-Instruct-v0.1",}16 unmodified lines"gemma-7b": 8192,"gpt-3.5-turbo": 8192,"command-r-plus": 32768,}TOKEN_RESERVED = 204 unmodified lines56789101116 unmodified lines282930313233344 unmodified lines# "openchat-3.5": "openchat/openchat-3.5-0106","gemma-7b": "google/gemma-1.1-7b-it","command-r-plus": "CohereForAI/c4ai-command-r-plus","llama3-70b": "meta-llama/Meta-Llama-3-70B-Instruct","default": "mistralai/Mixtral-8x7B-Instruct-v0.1",}16 unmodified lines"gemma-7b": 8192,"gpt-3.5-turbo": 8192,"command-r-plus": 32768,"llama3-70b": 8192,}TOKEN_RESERVED = 20
30 unmodified lines3132333435363738394041424348 unmodified lines9293949596979810 unmodified lines10911011111211311411511611764 unmodified lines18218318418518618718831 unmodified lines22022122222322422522630 unmodified linesself.model = "mixtral-8x7b"self.model_fullname = MODEL_MAP[self.model]self.message_outputer = OpenaiStreamOutputer(model=self.model)self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)def count_tokens(self, text):tokens = self.tokenizer.encode(text)token_count = len(tokens)logger.note(f"Prompt Token Count: {token_count}")return token_countdef get_hf_chat_id(self):request_url = "https://huggingface.co/chat/settings"48 unmodified linesself.conversation_id = conversation_idreturn conversation_iddef get_message_id(self):request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}/__data.json?x-sveltekit-invalidated=11"request_headers = HUGGINGCHAT_POST_HEADERSextra_headers = {10 unmodified linestimeout=10,)if res.status_code == 200:data = res.json()# TODO - extract message_idlogger.success(f"[{message_id}]")else:logger.warn(f"[{res.status_code}]")64 unmodified lines):self.get_hf_chat_id()self.get_conversation_id()message_id = self.get_message_id()request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"request_headers = copy.deepcopy(HUGGINGCHAT_POST_HEADERS)31 unmodified linesif __name__ == "__main__":streamer = HuggingchatStreamer(model="mixtral-8x7b")prompt = "who are you?"streamer.chat_response(prompt=prompt)# HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer30 unmodified lines3132333435363738394041424348 unmodified lines9293949596979810 unmodified lines10911011111211311411511611711811912012112264 unmodified lines18718818919019119219331 unmodified lines22522622722822923023123223330 unmodified linesself.model = "mixtral-8x7b"self.model_fullname = MODEL_MAP[self.model]self.message_outputer = OpenaiStreamOutputer(model=self.model)# self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)# def count_tokens(self, text):# tokens = self.tokenizer.encode(text)# token_count = len(tokens)# logger.note(f"Prompt Token Count: {token_count}")# return token_countdef get_hf_chat_id(self):request_url = "https://huggingface.co/chat/settings"48 unmodified linesself.conversation_id = conversation_idreturn conversation_iddef get_last_message_id(self):request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}/__data.json?x-sveltekit-invalidated=11"request_headers = HUGGINGCHAT_POST_HEADERSextra_headers = {10 unmodified linestimeout=10,)if res.status_code == 200:data = res.json()["nodes"][1]["data"]# find the last element which matches the format of uuid4uuid_pattern = re.compile(r"^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$")for item in data:if type(item) == str and uuid_pattern.match(item):message_id = itemlogger.success(f"[{message_id}]")else:logger.warn(f"[{res.status_code}]")64 unmodified lines):self.get_hf_chat_id()self.get_conversation_id()message_id = self.get_last_message_id()request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"request_headers = copy.deepcopy(HUGGINGCHAT_POST_HEADERS)31 unmodified linesif __name__ == "__main__":# model = "llama3-70b"model = "command-r-plus"streamer = HuggingchatStreamer(model=model)prompt = "what is your model?"streamer.chat_response(prompt=prompt)# HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer
constants/models.py+2
networks/huggingchat_streamer.py+20-13