phyloforfun commited on
Commit
8570ea5
1 Parent(s): a78800a
vouchervision/LLM_chatGPT_3_5.py CHANGED
@@ -223,6 +223,8 @@ def structured_output_parser(is_azure, MODEL, llm, prompt_template, logger, prom
223
  return None
224
 
225
  def structured_output_parser_for_function_calls_fail(is_azure, MODEL, failed_response, logger, llm, prompt_version, is_helper=False, try_ind=0):
 
 
226
  if try_ind > 5:
227
  return None
228
 
@@ -272,13 +274,17 @@ def structured_output_parser_for_function_calls_fail(is_azure, MODEL, failed_res
272
  except json.decoder.JSONDecodeError as e:
273
  try_ind += 1
274
  error_message = str(e)
275
- redo_content = f'The error messsage is: {error_message}\nThe broken JSON object is: {output.content}'
276
- logger.info(f'[Failed JSON Object]\n{output.content}')
277
- refined_response = structured_output_parser_for_function_calls_fail(is_azure, MODEL, redo_content, logger, llm, prompt_version, is_helper, try_ind)
 
 
278
  except:
279
  try_ind += 1
280
- logger.info(f'[Failed JSON Object]\n{output.content}')
281
- refined_response = structured_output_parser_for_function_calls_fail(is_azure, MODEL, output.content, logger, llm, prompt_version, is_helper, try_ind)
 
 
282
 
283
  return refined_response
284
 
 
223
  return None
224
 
225
  def structured_output_parser_for_function_calls_fail(is_azure, MODEL, failed_response, logger, llm, prompt_version, is_helper=False, try_ind=0):
226
+ if try_ind == 0:
227
+ original_failed_response = failed_response
228
  if try_ind > 5:
229
  return None
230
 
 
274
  except json.decoder.JSONDecodeError as e:
275
  try_ind += 1
276
  error_message = str(e)
277
+ redo_content = f'The error messsage is: {error_message}\nThe broken JSON object is: {original_failed_response}' # Use original_failed_response here
278
+ logger.info(f'[Failed JSON Object]\n{original_failed_response}') # And here
279
+ refined_response = structured_output_parser_for_function_calls_fail(
280
+ is_azure, MODEL, redo_content, logger, llm, prompt_version, is_helper, try_ind, original_failed_response
281
+ )
282
  except:
283
  try_ind += 1
284
+ logger.info(f'[Failed JSON Object]\n{original_failed_response}') # And here
285
+ refined_response = structured_output_parser_for_function_calls_fail(
286
+ is_azure, MODEL, original_failed_response, logger, llm, prompt_version, is_helper, try_ind, original_failed_response
287
+ )
288
 
289
  return refined_response
290
 
vouchervision/general_utils.py CHANGED
@@ -34,11 +34,22 @@ def get_cfg_from_full_path(path_cfg):
34
  cfg = yaml.full_load(ymlfile)
35
  return cfg
36
 
37
- def num_tokens_from_string(string: str, encoding_name: str) -> int:
38
- encoding = tiktoken.get_encoding(encoding_name)
39
- if isinstance(string, dict):
40
- string = json.dumps(string, sort_keys=False)
41
- num_tokens = len(encoding.encode(string))
 
 
 
 
 
 
 
 
 
 
 
42
  return num_tokens
43
 
44
  def add_to_expense_report(dir_home, data):
 
34
  cfg = yaml.full_load(ymlfile)
35
  return cfg
36
 
37
+ def num_tokens_from_string(string, encoding_name):
38
+ try:
39
+ # Ensure the encoding is obtained correctly.
40
+ encoding = tiktoken.get_encoding(encoding_name)
41
+
42
+ # Convert dictionary to string if it is not already a string
43
+ if isinstance(string, dict):
44
+ string = json.dumps(string, ensure_ascii=False)
45
+
46
+ # Encode the string and return the number of tokens.
47
+ num_tokens = len(encoding.encode(string))
48
+ except Exception as e:
49
+ # If there's any error, log it and return 0.
50
+ print(f"An error occurred: {e}")
51
+ num_tokens = 0
52
+
53
  return num_tokens
54
 
55
  def add_to_expense_report(dir_home, data):