Spaces:
Running
Running
:zap: [Enhance] Quieter openai auth, use cffi to request hf-chat id, and console token count for exceeds
Browse files
messagers/token_checker.py
CHANGED
@@ -40,5 +40,7 @@ class TokenChecker:
|
|
40 |
|
41 |
def check_token_limit(self):
|
42 |
if self.get_token_redundancy() <= 0:
|
43 |
-
raise ValueError(
|
|
|
|
|
44 |
return True
|
|
|
40 |
|
41 |
def check_token_limit(self):
|
42 |
if self.get_token_redundancy() <= 0:
|
43 |
+
raise ValueError(
|
44 |
+
f"Prompt exceeded token limit: {self.count_tokens()} > {self.get_token_limit()}"
|
45 |
+
)
|
46 |
return True
|
networks/huggingchat_streamer.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import copy
|
2 |
import json
|
3 |
import re
|
|
|
4 |
import requests
|
|
|
5 |
|
6 |
from tclogger import logger
|
7 |
|
@@ -30,7 +32,7 @@ class HuggingchatRequester:
|
|
30 |
request_body.update(extra_body)
|
31 |
logger.note(f"> hf-chat ID:", end=" ")
|
32 |
|
33 |
-
res =
|
34 |
request_url,
|
35 |
headers=HUGGINGCHAT_POST_HEADERS,
|
36 |
json=request_body,
|
|
|
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 |
|
|
|
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,
|
networks/openai_streamer.py
CHANGED
@@ -171,18 +171,21 @@ class OpenaiStreamer:
|
|
171 |
|
172 |
def check_token_limit(self, messages: list[dict]):
|
173 |
token_limit = TOKEN_LIMIT_MAP[self.model]
|
174 |
-
|
175 |
-
|
176 |
-
)
|
177 |
if token_redundancy <= 0:
|
178 |
-
raise ValueError(
|
|
|
|
|
179 |
return True
|
180 |
|
181 |
-
def chat_response(self, messages: list[dict]):
|
182 |
self.check_token_limit(messages)
|
|
|
183 |
requester = OpenaiRequester()
|
184 |
requester.auth()
|
185 |
-
|
|
|
186 |
|
187 |
def chat_return_generator(self, stream_response: requests.Response, verbose=False):
|
188 |
content_offset = 0
|
|
|
171 |
|
172 |
def check_token_limit(self, messages: list[dict]):
|
173 |
token_limit = TOKEN_LIMIT_MAP[self.model]
|
174 |
+
token_count = self.count_tokens(messages)
|
175 |
+
token_redundancy = int(token_limit - TOKEN_RESERVED - token_count)
|
|
|
176 |
if token_redundancy <= 0:
|
177 |
+
raise ValueError(
|
178 |
+
f"Prompt exceeded token limit: {token_count} > {token_limit}"
|
179 |
+
)
|
180 |
return True
|
181 |
|
182 |
+
def chat_response(self, messages: list[dict], verbose=False):
|
183 |
self.check_token_limit(messages)
|
184 |
+
logger.enter_quiet(not verbose)
|
185 |
requester = OpenaiRequester()
|
186 |
requester.auth()
|
187 |
+
logger.exit_quiet(not verbose)
|
188 |
+
return requester.chat_completions(messages, verbose=verbose)
|
189 |
|
190 |
def chat_return_generator(self, stream_response: requests.Response, verbose=False):
|
191 |
content_offset = 0
|