IliaLarchenko commited on
Commit
cb330d2
1 Parent(s): 855406f

Grader refactoring

Browse files
Files changed (1) hide show
  1. tests/grader.py +42 -26
tests/grader.py CHANGED
@@ -3,6 +3,35 @@ from typing import Dict, Any, List
3
  from openai import OpenAI
4
  from tests.testing_prompts import grader_prompt
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def grade(json_file_path: str, model: str = "gpt-4o", suffix: str = "") -> Dict[str, Any]:
8
  """
@@ -13,9 +42,13 @@ def grade(json_file_path: str, model: str = "gpt-4o", suffix: str = "") -> Dict[
13
  :param suffix: Suffix to add to the feedback file name.
14
  :return: Feedback dictionary.
15
  """
16
- client = OpenAI(base_url="https://api.openai.com/v1")
17
- with open(json_file_path) as file:
18
- interview_data = json.load(file)
 
 
 
 
19
 
20
  interview_summary_list = generate_interview_summary(interview_data)
21
 
@@ -24,8 +57,7 @@ def grade(json_file_path: str, model: str = "gpt-4o", suffix: str = "") -> Dict[
24
  {"role": "user", "content": f"Please evaluate the interviewer based on the following data: \n {'\n'.join(interview_summary_list)}"},
25
  ]
26
 
27
- response = client.chat.completions.create(model=model, messages=messages, temperature=0, response_format={"type": "json_object"})
28
- feedback = json.loads(response.choices[0].message.content)
29
 
30
  populate_feedback_metadata(feedback, json_file_path, interview_data, model)
31
  calculate_overall_score(feedback)
@@ -35,26 +67,10 @@ def grade(json_file_path: str, model: str = "gpt-4o", suffix: str = "") -> Dict[
35
  return feedback
36
 
37
 
38
- def generate_interview_summary(interview_data: Dict[str, Any]) -> List[str]:
39
- """
40
- Generate a summary of the interview data.
41
-
42
- :param interview_data: Dictionary containing interview data.
43
- :return: List of summary strings.
44
- """
45
- summary = [
46
- f"Interview type: {interview_data['inputs']['interview_type']}",
47
- f"Interview difficulty: {interview_data['inputs']['difficulty']}",
48
- f"Interview topic: {interview_data['inputs']['topic']}",
49
- ]
50
- if interview_data["inputs"]["requirements"]:
51
- summary.append(f"Interview requirements: {interview_data['inputs']['requirements']}")
52
- summary.append(f"Problem statement proposed by interviewer: {interview_data['problem_statement']}")
53
- summary.append(f"\nTranscript of the whole interview below:")
54
- summary += interview_data["transcript"]
55
- summary.append(f"\nTHE MAIN PART OF THE INTERVIEW ENDED HERE.")
56
- summary.append(f"Feedback provided by interviewer: {interview_data['feedback']}")
57
- return summary
58
 
59
 
60
  def populate_feedback_metadata(feedback: Dict[str, Any], json_file_path: str, interview_data: Dict[str, Any], model: str) -> None:
@@ -104,4 +120,4 @@ def save_feedback(json_file_path: str, feedback: Dict[str, Any], suffix: str) ->
104
  :param suffix: Suffix to add to the feedback file name.
105
  """
106
  with open(json_file_path.replace(".json", f"_feedback_{suffix}.json"), "w") as file:
107
- json.dump(feedback, file, indent=4)
 
3
  from openai import OpenAI
4
  from tests.testing_prompts import grader_prompt
5
 
6
+ BASE_URL = "https://api.openai.com/v1"
7
+ JSON_INDENT = 4
8
+
9
+
10
+ def format_interview_data(interview_data):
11
+ return [
12
+ f"Interview type: {interview_data['inputs']['interview_type']}",
13
+ f"Interview difficulty: {interview_data['inputs']['difficulty']}",
14
+ f"Interview topic: {interview_data['inputs']['topic']}",
15
+ ]
16
+
17
+
18
+ def generate_interview_summary(interview_data: Dict[str, Any]) -> List[str]:
19
+ """
20
+ Generate a summary of the interview data.
21
+
22
+ :param interview_data: Dictionary containing interview data.
23
+ :return: List of summary strings.
24
+ """
25
+ summary = format_interview_data(interview_data)
26
+ if interview_data["inputs"]["requirements"]:
27
+ summary.append(f"Interview requirements: {interview_data['inputs']['requirements']}")
28
+ summary.append(f"Problem statement proposed by interviewer: {interview_data['problem_statement']}")
29
+ summary.append(f"\nTranscript of the whole interview below:")
30
+ summary += interview_data["transcript"]
31
+ summary.append(f"\nTHE MAIN PART OF THE INTERVIEW ENDED HERE.")
32
+ summary.append(f"Feedback provided by interviewer: {interview_data['feedback']}")
33
+ return summary
34
+
35
 
36
  def grade(json_file_path: str, model: str = "gpt-4o", suffix: str = "") -> Dict[str, Any]:
37
  """
 
42
  :param suffix: Suffix to add to the feedback file name.
43
  :return: Feedback dictionary.
44
  """
45
+ try:
46
+ with open(json_file_path) as file:
47
+ interview_data = json.load(file)
48
+ except FileNotFoundError:
49
+ return {"error": "File not found"}
50
+ except json.JSONDecodeError:
51
+ return {"error": "Invalid JSON format"}
52
 
53
  interview_summary_list = generate_interview_summary(interview_data)
54
 
 
57
  {"role": "user", "content": f"Please evaluate the interviewer based on the following data: \n {'\n'.join(interview_summary_list)}"},
58
  ]
59
 
60
+ feedback = call_openai_api(messages, model)
 
61
 
62
  populate_feedback_metadata(feedback, json_file_path, interview_data, model)
63
  calculate_overall_score(feedback)
 
67
  return feedback
68
 
69
 
70
+ def call_openai_api(messages, model):
71
+ client = OpenAI(base_url=BASE_URL)
72
+ response = client.chat.completions.create(model=model, messages=messages, temperature=0, response_format={"type": "json_object"})
73
+ return json.loads(response.choices[0].message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
 
76
  def populate_feedback_metadata(feedback: Dict[str, Any], json_file_path: str, interview_data: Dict[str, Any], model: str) -> None:
 
120
  :param suffix: Suffix to add to the feedback file name.
121
  """
122
  with open(json_file_path.replace(".json", f"_feedback_{suffix}.json"), "w") as file:
123
+ json.dump(feedback, file, indent=JSON_INDENT)