| 49 | return response.choices[0].message.content.strip() |
| 50 | |
| 51 | def evaluate_response(question: str, llm_response: str, ground_truth: str, model: str) -> Dict[str, str]: |
| 52 | evaluation_prompt = f"""===Task=== |
| 53 | I need your help in evaluating an answer provided by an LLM against a ground |
| 54 | truth answer. Your task is to determine if the ground truth answer is present in the LLM's |
| 55 | response. Please analyze the provided data and make a decision. |
| 56 | ===Instructions=== |
| 57 | 1. Carefully compare the "Predicted Answer" with the "Ground Truth Answer". |
| 58 | 2. Consider the substance of the answers - look for equivalent information or correct answers. |
| 59 | Do not focus on exact wording unless the exact wording is crucial to the meaning. |
| 60 | 3. Your final decision should be based on whether the meaning and the vital facts of the |
| 61 | "Ground Truth Answer" are present in the "Predicted Answer:" |
| 62 | ===Input Data=== |
| 63 | - Question: {question} |
| 64 | - Predicted Answer: {llm_response} |
| 65 | - Ground Truth Answer: {ground_truth} |
| 66 | ===Output Format=== |
| 67 | Provide your final evaluation in the following format: |
| 68 | "Explanation:" (How you made the decision?) |
| 69 | "Decision:" ("TRUE" or "FALSE" ) |
| 70 | Please proceed with the evaluation.""" |
| 71 | |
| 72 | evaluation_response = client.chat.completions.create( |
| 73 | model=model, |
| 74 | messages=[ |
| 75 | {"role": "system", "content": "You are a helpful assistant."}, |
| 76 | {"role": "user", "content": evaluation_prompt} |
| 77 | ], |
| 78 | max_tokens=300, |
| 79 | n=1, |
| 80 | stop=None, |
| 81 | temperature=0.3, |
| 82 | ) |
| 83 | |
| 84 | evaluation_text = evaluation_response.choices[0].message.content.strip() |
| 85 | |
| 86 | # Extract the decision and explanation |
| 87 | lines = evaluation_text.split('\n') |
| 88 | decision = "FALSE" |
| 89 | explanation = "" |
| 90 | for line in lines: |
| 91 | if line.startswith("Decision:"): |
| 92 | decision = line.split(":")[1].strip().upper() |
| 93 | elif line.startswith("Explanation:"): |
| 94 | explanation = line.split(":", 1)[1].strip() |
| 95 | |
| 96 | return {"decision": decision, "explanation": explanation} |
| 97 | |
| 98 | def main(model: str): |
| 99 | # Load the dataset |