diff --git a/app.py b/app.py index a6897e3..1eb1c57 100644 --- a/app.py +++ b/app.py @@ -10,8 +10,7 @@ from helper.firebase_helper import * from helper.heygen_api import create_videos_and_save_to_db from helper.speech_to_text_helper import * from helper.token_counter import count_tokens -from helper.openai_interface import make_openai_call, make_openai_instruct_call -from grading_summary.grading_summary import calculate_grading_summary +from helper.openai_interface import * import os import re import logging diff --git a/grading_summary/grading_summary.py b/grading_summary/grading_summary.py deleted file mode 100644 index 87d1e6c..0000000 --- a/grading_summary/grading_summary.py +++ /dev/null @@ -1,88 +0,0 @@ -import json - -import openai -import os -from dotenv import load_dotenv - -load_dotenv() -openai.api_key = os.getenv("OPENAI_API_KEY") - -chat_config = {'max_tokens': 1000, 'temperature': 0.2} -section_keys = ['reading', 'listening', 'writing', 'speaking', 'level'] -grade_top_limit = 9 - -tools = [{ - "type": "function", - "function": { - "name": "save_evaluation_and_suggestions", - "description": "Saves the evaluation and suggestions requested by input.", - "parameters": { - "type": "object", - "properties": { - "evaluation": { - "type": "string", - "description": "A comment on the IELTS section grade obtained in the specific section and what it could mean without suggestions.", - }, - "suggestions": { - "type": "string", - "description": "A small paragraph text with suggestions on how to possibly get a better grade than the one obtained.", - }, - }, - "required": ["evaluation", "suggestions"], - }, - } -}] - - -def calculate_grading_summary(body): - extracted_sections = extract_existing_sections_from_body(body, section_keys) - - ret = [] - - for section in extracted_sections: - openai_response_dict = calculate_section_grade_summary(section) - ret = ret + [{'code': section['code'], 'name': section['name'], 'grade': section['grade'], - 'evaluation': openai_response_dict['evaluation'], - 'suggestions': openai_response_dict['suggestions']}] - - return {'sections': ret} - - -def calculate_section_grade_summary(section): - res = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - max_tokens=chat_config['max_tokens'], - temperature=chat_config['temperature'], - tools=tools, - messages=[ - { - "role": "user", - "content": "You are a IELTS test section grade evaluator. You will receive a IELTS test section name and the grade obtained in the section. You should offer a comment on this grade with also suggestions on how to possibly get a better grade.", - }, - { - "role": "user", - "content": "Section: " + str(section['name']) + " Grade: " + str(section['grade']), - }, - {"role": "user", "content": "Speak in third person."}, - {"role": "user", "content": "Please save the evaluation and suggestions generated."} - ]) - - return parse_openai_response(res) - - -def parse_openai_response(response): - if 'choices' in response and len(response['choices']) > 0 and 'message' in response['choices'][ - 0] and 'tool_calls' in response['choices'][0]['message'] and isinstance( - response['choices'][0]['message']['tool_calls'], list) and len( - response['choices'][0]['message']['tool_calls']) > 0 and \ - response['choices'][0]['message']['tool_calls'][0]['function']['arguments']: - return json.loads(response['choices'][0]['message']['tool_calls'][0]['function']['arguments']) - else: - return {'evaluation': "", 'suggestions': ""} - - -def extract_existing_sections_from_body(my_dict, keys_to_extract): - if 'sections' in my_dict and isinstance(my_dict['sections'], list) and len(my_dict['sections']) > 0: - return list(filter( - lambda item: 'code' in item and item['code'] in keys_to_extract and 'grade' in item and 'name' in item, - my_dict['sections'])) diff --git a/helper/openai_interface.py b/helper/openai_interface.py index 5c6bf99..ea8757d 100644 --- a/helper/openai_interface.py +++ b/helper/openai_interface.py @@ -16,6 +16,33 @@ TRY_LIMIT = 1 try_count = 0 +# GRADING SUMMARY +chat_config = {'max_tokens': 1000, 'temperature': 0.2} +section_keys = ['reading', 'listening', 'writing', 'speaking', 'level'] +grade_top_limit = 9 + +tools = [{ + "type": "function", + "function": { + "name": "save_evaluation_and_suggestions", + "description": "Saves the evaluation and suggestions requested by input.", + "parameters": { + "type": "object", + "properties": { + "evaluation": { + "type": "string", + "description": "A comment on the IELTS section grade obtained in the specific section and what it could mean without suggestions.", + }, + "suggestions": { + "type": "string", + "description": "A small paragraph text with suggestions on how to possibly get a better grade than the one obtained.", + }, + }, + "required": ["evaluation", "suggestions"], + }, + } +}] +### def process_response(input_string, quotation_check_field): if '{' in input_string: try: @@ -141,3 +168,58 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check, else: try_count = 0 return processed_response + + +# GRADING SUMMARY +def calculate_grading_summary(body): + extracted_sections = extract_existing_sections_from_body(body, section_keys) + + ret = [] + + for section in extracted_sections: + openai_response_dict = calculate_section_grade_summary(section) + ret = ret + [{'code': section['code'], 'name': section['name'], 'grade': section['grade'], + 'evaluation': openai_response_dict['evaluation'], + 'suggestions': openai_response_dict['suggestions']}] + + return {'sections': ret} + + +def calculate_section_grade_summary(section): + res = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + max_tokens=chat_config['max_tokens'], + temperature=chat_config['temperature'], + tools=tools, + messages=[ + { + "role": "user", + "content": "You are a IELTS test section grade evaluator. You will receive a IELTS test section name and the grade obtained in the section. You should offer a comment on this grade with also suggestions on how to possibly get a better grade.", + }, + { + "role": "user", + "content": "Section: " + str(section['name']) + " Grade: " + str(section['grade']), + }, + {"role": "user", "content": "Speak in third person."}, + {"role": "user", "content": "Please save the evaluation and suggestions generated."} + ]) + + return parse_openai_response(res) + + +def parse_openai_response(response): + if 'choices' in response and len(response['choices']) > 0 and 'message' in response['choices'][ + 0] and 'tool_calls' in response['choices'][0]['message'] and isinstance( + response['choices'][0]['message']['tool_calls'], list) and len( + response['choices'][0]['message']['tool_calls']) > 0 and \ + response['choices'][0]['message']['tool_calls'][0]['function']['arguments']: + return json.loads(response['choices'][0]['message']['tool_calls'][0]['function']['arguments']) + else: + return {'evaluation': "", 'suggestions': ""} + + +def extract_existing_sections_from_body(my_dict, keys_to_extract): + if 'sections' in my_dict and isinstance(my_dict['sections'], list) and len(my_dict['sections']) > 0: + return list(filter( + lambda item: 'code' in item and item['code'] in keys_to_extract and 'grade' in item and 'name' in item, + my_dict['sections'])) \ No newline at end of file diff --git a/postman/ielts.postman_collection.json b/postman/ielts.postman_collection.json index 1a9bde3..78a4003 100644 --- a/postman/ielts.postman_collection.json +++ b/postman/ielts.postman_collection.json @@ -1106,7 +1106,7 @@ "response": [] }, { - "name": "Fetch Answer Tips Copy", + "name": "Get Grading Summary", "request": { "auth": { "type": "bearer",