Husnain commited on
Commit
a44e5b9
1 Parent(s): 97134c0

⚡ [Enhance] Quieter openai auth, use cffi to request hf-chat id, and …

Browse files
Files changed (1) hide show
  1. 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
- token_redundancy = int(
175
- token_limit - TOKEN_RESERVED - self.count_tokens(messages)
176
- )
177
  if token_redundancy <= 0:
178
- raise ValueError(f"Prompt exceeded token limit: {token_limit}")
 
 
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
- return requester.chat_completions(messages, verbose=False)
 
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"]