Spaces:
Build error
Build error
:gem: [Feature] New HuggingchatStreamer: get_conversation_id
Browse files- constants/networks.py +3 -0
- networks/huggingchat_streamer.py +80 -0
constants/networks.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
REQUESTS_HEADERS = {
|
2 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
|
3 |
+
}
|
networks/huggingchat_streamer.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import re
|
3 |
+
import requests
|
4 |
+
|
5 |
+
|
6 |
+
from tclogger import logger
|
7 |
+
from transformers import AutoTokenizer
|
8 |
+
|
9 |
+
from constants.models import (
|
10 |
+
MODEL_MAP,
|
11 |
+
STOP_SEQUENCES_MAP,
|
12 |
+
TOKEN_LIMIT_MAP,
|
13 |
+
TOKEN_RESERVED,
|
14 |
+
)
|
15 |
+
from constants.envs import PROXIES
|
16 |
+
from constants.networks import REQUESTS_HEADERS
|
17 |
+
from messagers.message_outputer import OpenaiStreamOutputer
|
18 |
+
|
19 |
+
|
20 |
+
class HuggingchatStreamer:
|
21 |
+
def __init__(self, model: str):
|
22 |
+
if model in MODEL_MAP.keys():
|
23 |
+
self.model = model
|
24 |
+
else:
|
25 |
+
self.model = "mixtral-8x7b"
|
26 |
+
self.model_fullname = MODEL_MAP[self.model]
|
27 |
+
self.message_outputer = OpenaiStreamOutputer(model=self.model)
|
28 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
29 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)
|
30 |
+
|
31 |
+
def count_tokens(self, text):
|
32 |
+
tokens = self.tokenizer.encode(text)
|
33 |
+
token_count = len(tokens)
|
34 |
+
logger.note(f"Prompt Token Count: {token_count}")
|
35 |
+
return token_count
|
36 |
+
|
37 |
+
def get_conversation_id(self, preprompt: str = ""):
|
38 |
+
request_url = "https://huggingface.co/chat/conversation"
|
39 |
+
request_body = {
|
40 |
+
"model": self.model_fullname,
|
41 |
+
"preprompt": preprompt,
|
42 |
+
}
|
43 |
+
logger.note(f"> Conversation ID:", end=" ")
|
44 |
+
res = requests.post(
|
45 |
+
request_url,
|
46 |
+
headers=REQUESTS_HEADERS,
|
47 |
+
json=request_body,
|
48 |
+
proxies=PROXIES,
|
49 |
+
timeout=10,
|
50 |
+
)
|
51 |
+
if res.status_code == 200:
|
52 |
+
conversation_id = res.json()["conversationId"]
|
53 |
+
logger.success(f"[{conversation_id}]")
|
54 |
+
else:
|
55 |
+
logger.warn(f"[{res.status_code}]")
|
56 |
+
raise ValueError("Failed to get conversation ID!")
|
57 |
+
self.conversation_id = conversation_id
|
58 |
+
|
59 |
+
def chat_response(
|
60 |
+
self,
|
61 |
+
prompt: str = None,
|
62 |
+
temperature: float = 0.5,
|
63 |
+
top_p: float = 0.95,
|
64 |
+
max_new_tokens: int = None,
|
65 |
+
api_key: str = None,
|
66 |
+
use_cache: bool = False,
|
67 |
+
):
|
68 |
+
pass
|
69 |
+
|
70 |
+
def chat_return_dict(self, stream_response):
|
71 |
+
pass
|
72 |
+
|
73 |
+
def chat_return_generator(self, stream_response):
|
74 |
+
pass
|
75 |
+
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
streamer = HuggingchatStreamer(model="mixtral-8x7b")
|
79 |
+
conversation_id = streamer.get_conversation_id()
|
80 |
+
# python -m networks.huggingchat_streamer
|