4.3 OpenAI-2

The initial OpenAI function, known as ‘dict_creator2’ is responsible for generating the dictionary or JSON.

Step 1: ‘dict_creator2’ accepts three parameters
i) The user-provided prompt.
ii) The content, which is extracted using the ‘content_extraction()’ function.
iii) The ‘ml’ variable representing the updated maximum length specified in the ‘config_file.’

Step 2: Use your OpenAI API key to facilitate this process.

Step 3: Subsequently, the ‘dict_creator2’ function will embark on the task of generating the dictionary or JSON output.

def dict_creator2(prompt, content, ml):
  openai.api_key = "here use your OpenAI key"

  response = openai.ChatCompletion.create(
    model="gpt-4-0613",
    messages=[
      {
        "role": "system",
        "content": prompt
      },
      {
        "role": "user",
        "content": content
      }
    ],
    temperature=0,
    max_tokens=ml,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
  )
  converted_dict = response['choices'][0]['message']['content']
  return converted_dict
prompt = "Generated prompt according to your need"
content = "Put the extracted content from the input file"
ml = "Max length will be fetched by the config file"
dict_creator2(prompt, str(content), ml, api_key)
  1. def dict_creator2(prompt, content, ml):: This line defines a function named dict_creator2 that takes three parameters: prompt, content, and ml. These parameters are expected to be provided when the function is called.
  2. openai.api_key = "here use your OpenAI key": This line sets the API key for OpenAI. This key is necessary to authenticate and interact with OpenAI’s models.
  3. The next block of code makes an API call to OpenAI’s Chat Completion API using the openai.ChatCompletion.create method. This method sends a request to the GPT-4 model and retrieves a response.
    • model="gpt-4-0613": Specifies the model to be used, in this case, gpt-4-0613.
    • messages=[...]: This parameter provides a list of messages for the conversation. The conversation starts with a system message followed by a user message.
      • The system message ("role": "system", "content": prompt) sets the context or instructions for the conversation. The prompt variable is used here.
      • The user message ("role": "user", "content": content) contains the actual input or query provided by the user. The content variable is used here.
    • The remaining parameters like temperature, max_tokens, top_p, frequency_penalty, and presence_penalty are used to fine-tune the behavior of the model during text generation.
  4. converted_dict = response['choices'][0]['message']['content']: This line extracts the generated content from the response received from the API call. It accesses the ‘content’ property of the first choice in the response.
  5. return converted_dict: The function returns the generated content as converted_dict.

In summary, this function serves as an interface to interact with the GPT-4 model using OpenAI’s API. It takes a prompt and user content as input, sends a request to the model, and returns the generated text based on the provided inputs.

Step 4: Once the dictionary is recreated, the configuration file will again undergo an update, setting the maximum length to 8000.

# Final Config updater

def update_config_file():
  with open('config file path', 'r') as file:
    config_data = json.load(file)
  config_data['max_length'] = 8000
  with open('config file path here', 'w') as file:
    json.dump(config_data, file, indent=4)
  return config_data['max_length']

print(f"{update_config_file()}")
  1. def update_config_file(): – This line defines a Python function named update_config_file.
  2. with open('config file path', 'r') as file: – This line opens a JSON file located at the specified path in read mode (‘r’). It uses a with statement to ensure that the file is properly closed after it’s done being used. The content of the JSON file is loaded into the config_data variable using json.load(file).
  3. config_data['max_length'] = 8000 – This line updates a specific key in the JSON data. It sets the value of the ‘max_length’ key to 8000.
  4. with open('config file path here', 'w') as file: – This line opens a file (the path to which should be provided where ‘config file path here’ is written) in write mode (‘w’). This is where the updated JSON data will be written.
  5. json.dump(config_data, file, indent=4) – This line writes the updated config_data back to the file in a nicely formatted JSON format. The indent=4 argument is used to specify an indentation of 4 spaces for better readability.
  6. return config_data['max_length'] – This line returns the updated value of ‘max_length’ from the config_data dictionary.
  7. print(f"{update_config_file()}") – This line calls the update_config_file function and prints the value returned by the function. In this case, it will print the updated ‘max_length’ value, which is 8000.

In summary, the code reads a JSON configuration file, updates a specific value in that file, and then writes the updated configuration back to the file. Finally, it returns and prints the updated ‘max_length’ value.