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\": , \"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"}