File size: 1,879 Bytes
8880178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import openai
import pandas as pd
import pandas as pd
import json
import urllib
import math
import time
import random
import re
from tqdm import tqdm
from io import StringIO
import exp_lib

def experiment_1_trial(data_df, model_name):
  x = data_df.sample(frac=1)
  train_df = x.drop_duplicates('Misconception ID')
  test_df = x.iloc[::-1].drop_duplicates('Misconception ID')
  test_df = test_df.reset_index()
  prompt = exp_lib.generate_prompt_test_batch(train_df.to_dict(orient='records'), test_df.to_dict(orient='records'))
  response = exp_lib.get_gpt4_diagnosis(model_name, prompt)
  response_df = pd.read_csv(StringIO(response), header=None, names=["test_example", "diagnosis"])
  test_df["Predicted Diagnosis"] = response_df["diagnosis"].str.strip()
  test_df["Model"] = model_name
  return test_df[['Misconception ID', 'Example Number', 'Topic', 'Predicted Diagnosis', 'Model']]


def experiment_1(input_file_path, model_name, num_iterations, output_file_path):
  data_df = pd.read_json(input_file_path)
  experiment_1_results_list = []
  for i in tqdm(range(num_iterations)):
    try:
      trial_result = experiment_1_trial(data_df, model_name)
      trial_result['Trial'] = i 
      experiment_1_results_list.append(trial_result)
    except Exception as e:
      print(e)  
  experiment_1_results_df = pd.concat(experiment_1_results_list)
  experiment_1_results_df['Correct'] = (experiment_1_results_df['Misconception ID'] == experiment_1_results_df['Predicted Diagnosis'])
  experiment_1_results_df.to_csv(output_file_path)


if __name__ == '__main__':
  experiment_name = 'experiment_1'
  input_file_path = 'data/data.json' 
  model_name = 'gpt-4-turbo'
  num_iterations = 100
  output_file_path = f'outputs/{experiment_name}_{model_name}_{num_iterations}iters.csv'

  experiment_1(
    input_file_path,
    model_name,
    num_iterations,
    output_file_path
  )