4.2 OpenAI-1
The purpose of using OpenAI GPT is to create a dictionary which will have the details of the resume input file in key-value pair. It will be used to format the docx file through their key-value pairs.
The initial OpenAI function, known as ‘dict_creator1,’ is responsible for generating an exception message related to the OpenAI token limit issue.
Step 1: ‘dict_creator1’ 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 maximum length specified in the ‘config_file.’
Step 2: utilize your OpenAI API key and provided one exception pattern.
Step 3: Evaluates whether the provided ‘max_length’ adheres to the criteria. If it does, the function generates a dictionary; otherwise, it responds with an exception message.
OpenAI offers about 2-3 versions for each of their models. The goal is to monetize based on the number of tokens used. These versions are categorized as follows:
- 4097 tokens
- 8192 tokens
- 16385 tokens
However, there can be a challenge in determining the correct amount of tokens for chat completion. OpenAI allocates the specified number of tokens regardless of whether you end up using them or not.
To address this, we can make two calls to OpenAI chat completion. The first call is to obtain the post log message, which provides information about the token requirement. The second call is to actually process the data.
Even with this approach, there’s still a potential issue. If we make two calls to OpenAI simultaneously within a minute of the first completion, it may result in a “ratelimiterror” due to using more than 10,000 tokens per minute. To overcome this, we can introduce a 60-second delay before making the second call.
# Dictionary error extrator
prompt = "Give the prompt that you have engineered in OpenAI playground "
def dict_creator1(prompt, content, ml, api_key):
openai.api_key = api_key
pattern = r"This model's maximum context length is 8192 tokens"
try:
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']
flag = 1
return converted_dict
except openai.error.OpenAIError as error:
if pattern in str(error):
main_err = error
return main_err
else:
raise Exception(f"{error}")
exception = dict_creator1(prompt, str(content), maxlength, api_key)
print(exception)
prompt = "Give the prompt that you have engineered in OpenAI playground "
- This is a string variable called
prompt
which seems to be a placeholder for the user to input a prompt. It’s meant to instruct the model.
- This is a string variable called
def dict_creator1(prompt, content, ml):
- This defines a function named
dict_creator1
that takes three parameters:prompt
,content
, andml
. This function appears to be designed to interact with the OpenAI API.
- This defines a function named
pattern = r"This model's maximum context length is 8192 tokens"
- This sets a regular expression pattern, which might be used later in the code.
- Inside the function, there is a
try
block which attempts to do the following:a.response = openai.ChatCompletion.create(...)
- This is a call to OpenAI’s API, specifically the
ChatCompletion
endpoint. It sends a request with a specified model, role (system or user), and content (prompt or user message).It also sets various parameters like temperature, max_tokens, top_p, frequency_penalty, and presence_penalty.
converted_dict = response['choices'][0]['message']['content']
- Assuming the API call is successful, this line extracts the content of the model’s response from the API response.
flag = 1
- This sets a flag to 1. The purpose of this flag is not clear in the provided code.
return converted_dict
- It returns the content of the model’s response.
- This is a call to OpenAI’s API, specifically the
- The
except openai.error.OpenAIError as error:
block handles exceptions raised by the OpenAI API. If there’s an exception, it checks whether the exception contains the pattern defined earlier. If it does, it returns the exception. Otherwise, it raises a new exception. - Finally, there is a call to the
dict_creator1
function:
exception = dict_creator1(prompt, str(content), maxlength)
print(exception)
prompt
is passed as the first argument,str(content)
(which suggests thatcontent
is a variable) as the second, andmaxlength
as the third argument.- The result of calling
dict_creator1
is stored in the variablee
xception, which is then printed.
- Mode:
- In the context of a language model, “mode” typically refers to the specific way the model operates. This might include options like “chat mode” for generating conversational responses or “completion mode” for finishing sentences.
- Model:
- The “model” refers to the underlying architecture and parameters that define how the language model works. For instance, GPT-3.5 is a specific model developed by OpenAI.
- Temperature:
- Temperature is a parameter that influences the randomness of the generated output. Higher values (e.g., 0.8) make the output more random and creative, while lower values (e.g., 0.2) make it more focused and deterministic.
- Stop Sequence:
- A “stop sequence” is a specific set of characters or tokens that, when encountered in the generated text, signal the end of a response. For example, “\n\n” could be a stop sequence.
- Top P:
- Top P, also known as nucleus sampling, is a method used during text generation where the model considers only the most probable tokens until a certain cumulative probability (p) is reached. This helps to prevent the generation of overly diverse or nonsensical output.
- Frequency Penalty:
- Frequency penalty is a parameter that can be applied during text generation. It discourages the repetition of certain words or tokens in the output, making the generated text more diverse.
- Presence Penalty:
- Presence penalty is another parameter used during text generation. It encourages the model to include specific words or tokens in its response. This is useful for ensuring that certain topics or information are addressed.
Step 1: The ‘regular_fn’ function will receive an exception log parameter from the ‘dict_creator1’ function.
Step 2: Within the exception message, it will identify a specific pattern.
Step 3: Subsequently, it will compute the maximum length and proceed to update the configuration file.
# First config updater
er_msg = "This model's maximum context length is 8192 tokens. However, you requested 12139 tokens (6139 in the messages, 6000 in the completion). Please reduce the length of the messages or completion."
def regular_fn(er_msg):
patt = r'\((\d{1,6})'
m = re.search(patt, er_msg)
content_length = m.group(1)
max_length = 8191 - int(content_length)
if max_length < 1000 or max_length == 1000:
raise Exception(f"{er_msg}")
else:
with open('config file path here', 'r') as file:
config_data = json.load(file)
new_max_length = max_length
try:
if isinstance(new_max_length, int):
config_data['max_length'] = new_max_length
with open('config file path here', 'w') as file:
json.dump(config_data, file, indent=4)
except Exception as e:
print(f"Exception: {e}")
return new_max_length
print(f"The updated maxlength is : {regular_fn(error_message)}")
- Exception Message and Explanation:
er_msg
is a variable storing a specific error message related to model configuration.- The error message highlights that the model’s maximum context length is 8192 tokens but the user requested 12139 tokens, which exceeds the limit.
- Function Definition:
regular_fn(er_msg)
is a function that takeser_msg
as an argument.- This function aims to adjust the
max_length
parameter in a configuration file based on the provided error message.
- Regular Expression:
patt = r'\((\d{1,6})'
defines a regular expression pattern. It appears to be looking for a pattern that starts with an opening parenthesis, followed by one to six digits.- This suggests that it’s trying to extract a numerical value enclosed in parentheses.
- Search for Pattern:
m = re.search(patt, er_msg)
uses the regular expression pattern to search for a match in the error message (er_msg
).- If a match is found, it assigns the result to
m
.
- Extract Content Length:
content_length = m.group(1)
extracts the content length from the match found in the previous step.
- Calculate New Maximum Length:
max_length = 8191 - int(content_length)
computes a new maximum length based on the extracted content length.
- Exception Handling:
- If the calculated
max_length
is less than 1000 or equal to 1000, it raises an exception with the original error message. - Otherwise, it proceeds with adjusting the configuration file.
- If the calculated
- Read Configuration Data:
with open('config file path here', 'r') as file:
opens a configuration file for reading.
- Load JSON Data:
config_data = json.load(file)
loads the JSON data from the configuration file.
- Update Maximum Length:
It attempts to update the maximum length in the configuration data if the new length is an integer.
Write Updated Configuration:
with open('config file path here', 'w') as file:
opens the configuration file for writing.json.dump(config_data, file, indent=4)
writes the updated configuration data to the file with indentation for readability.
- Exception Handling (Inside Try Block):
- If an exception occurs during the update process, it prints an error message.
- Return New Maximum Length:
- The function returns the newly calculated maximum length.
- Print Result:
print(f"The updated maxlength is : {regular_fn(error_message)}")
calls the function with the providederror_message
and prints the result.