Skip to content

Commit

Permalink
Create llm_openai.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nowickit-umich authored Nov 17, 2024
1 parent de1718b commit a8a2a34
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions llm_openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from openai import OpenAI
import numpy as np
import time

# API key
file = open("openai.secret")
key = file.readline().strip()
client = OpenAI(api_key=key)
file.close()

# Load the .npy training file
file_path = 'WP-train.npy'
data = np.load(file_path, allow_pickle=True)

#
count = 0
correct = 0
for item in data:
count += 1
# API is rate limited - batch mode could be used
time.sleep(0.1)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer the multiple choice question by returning only the correct answer by itself. The answer is almost never None of the above."},
{
"role": "user",
"content": item["question"] + " Select One: " + str(item["choice_list"])
}
]
)
guess = response.choices[0].message.content
print("GUESS:",guess,"ANSWER:",item["answer"])
if guess == item["answer"]:
correct += 1

print(correct/count)

0 comments on commit a8a2a34

Please sign in to comment.