Spaces:
Paused
Paused
Husnain
commited on
⚡ [Enhance] Quieter openai auth, use cffi to request hf-chat id, and …
Browse files- networks/openai_streamer.py +10 -6
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
|
|
@@ -203,6 +206,7 @@ class OpenaiStreamer:
|
|
| 203 |
is_finished = True
|
| 204 |
else:
|
| 205 |
content_type = "Completions"
|
|
|
|
| 206 |
try:
|
| 207 |
data = json.loads(line, strict=False)
|
| 208 |
message_role = data["message"]["author"]["role"]
|
|
|
|
| 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
|
|
|
|
| 206 |
is_finished = True
|
| 207 |
else:
|
| 208 |
content_type = "Completions"
|
| 209 |
+
delta_content = ""
|
| 210 |
try:
|
| 211 |
data = json.loads(line, strict=False)
|
| 212 |
message_role = data["message"]["author"]["role"]
|