import json import openai import os import re from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") MAX_TOKENS = 4097 TOP_P = 0.9 FREQUENCY_PENALTY = 0.5 TRY_LIMIT = 1 try_count = 0 def process_response(input_string): if '{' in input_string: try: # Find the index of the first occurrence of '{' index = input_string.index('{') # Extract everything after the first '{' (inclusive) result = input_string[index:] parsed_string = result.replace("\"", "\\\"") pattern = r"(?= TRY_LIMIT: try_count = 0 return result["choices"][0]["message"]["content"] else: try_count = 0 return processed_response