-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_answers.py
More file actions
52 lines (42 loc) · 1.66 KB
/
check_answers.py
File metadata and controls
52 lines (42 loc) · 1.66 KB
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
import os
import json
import requests
from dotenv import load_dotenv
load_dotenv()
CLOUDFLARE_ACCOUNT_ID = os.getenv("CLOUDFLARE_ACCOUNT_ID")
CLOUDFLARE_API_KEY = os.getenv("CLOUDFLARE_API_TOKEN")
def check_answers(question: str, answer: str):
headers = {
"Authorization": f"Bearer {CLOUDFLARE_API_KEY}",
"Content-Type": "application/json",
}
payload = json.dumps({
"messages": [
{
"role": "system",
"content": (
"You are an expert evaluator for interview answers."
" Given a question and a user's answer, grade the answer on a scale of 1 to 10."
" Also provide a short 1-2 sentence feedback."
" Respond strictly in JSON format like this: {\"score\": number, \"feedback\": \"your feedback here\"}."
" Only output the JSON, nothing else."
)
},
{
"role": "user",
"content": f"Question: {question}\nAnswer: {answer}"
}
]
})
model = "@cf/meta/llama-3.1-8b-instruct"
url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/{model}"
response = requests.post(url, headers=headers, data=payload)
if response.status_code != 200:
raise Exception(f"Failed to call AI API: {response.text}")
response_data = response.json()
raw_response = response_data["result"]["response"]
try:
evaluation = json.loads(raw_response)
except json.JSONDecodeError:
raise Exception(f"Invalid AI response: {raw_response}")
return evaluation