File size: 1,836 Bytes
37b9a66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from openai import OpenAI

def generate_chatgpt_prompt(question, guidelines, examples):
    """Generate a prompt for ChatGPT based on question, guidelines, and examples."""
    prompt = f"Here is an essay question and assessment guidelines:\n\nQuestion:\n{question}\n\nGuidelines:\n{guidelines}\n\n"
    
    if examples:
        prompt += "Here are examples of previously graded essays:\n"
        for example in examples:
            prompt += f"Essay: {example['essay']}\nMark: {example['mark']}\nReason: {example['reason']}\n\n"
    
    prompt += "Please assess the following essays based on the provided guidelines and give a mark and reasoning in JSON format."
    return prompt

def assess_essay_with_gpt(essay_text, question, guidelines, examples=None):
    """Get ChatGPT to assess the essay based on provided question and guidelines."""
    prompt = generate_chatgpt_prompt(question, guidelines, examples)
    prompt += f"\n\nEssay to assess:\n{essay_text}\n\nProvide the response in JSON format as follows:\n{{\"mark\": <mark>, \"reason\": \"<reason>\"}}. Do not include ```json``` in the response."

    client = OpenAI()

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "You are an AI assistant that assesses essays."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=150,
        temperature=0.2
    )
    
    # Extract the content from the response
    result = response.choices[0].message.content.strip()
    
    # Parse the JSON response
    try:
        return json.loads(result)
    except json.JSONDecodeError:
        print('result:', result)
        # Handle cases where the response might not be valid JSON
        return {"mark": 0, "reason": "Error: Unable to parse response"}