Api for generating writing task 2 questions and add postman collection.

This commit is contained in:
Cristiano Ferreira
2023-06-20 23:01:01 +01:00
parent 48a1197d56
commit 07e68e2650
4 changed files with 159 additions and 15 deletions

View File

@@ -7,8 +7,8 @@ from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
MAX_TOKENS = 4097
TEMPERATURE = 0.1
TOP_P = 0.9
FREQUENCY_PENALTY = 0.5
@@ -26,28 +26,23 @@ def process_response(input_string):
return json_obj
def check_fields(obj, fields):
return all(field in obj for field in fields)
def check_fields(obj):
if "overall" in obj and "task_response" in obj and "comment" in obj:
return True
else:
return False
def make_openai_call(messages, token_count):
def make_openai_call(messages, token_count, fields_to_check, temperature):
global try_count
result = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=int(MAX_TOKENS - token_count - 500),
temperature=float(TEMPERATURE),
max_tokens=int(MAX_TOKENS - token_count - 300),
temperature=float(temperature),
top_p=float(TOP_P),
frequency_penalty=float(FREQUENCY_PENALTY),
messages=messages
)
processed_response = process_response(result["choices"][0]["message"]["content"])
if check_fields(processed_response) is False and try_count < TRY_LIMIT:
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
try_count = try_count + 1
return make_openai_call(messages, token_count)
return make_openai_call(messages, token_count, fields_to_check)
elif try_count >= TRY_LIMIT:
try_count = 0
return result["choices"][0]["message"]["content"]