-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjudge.py
More file actions
36 lines (28 loc) · 1.12 KB
/
Copy pathjudge.py
File metadata and controls
36 lines (28 loc) · 1.12 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
from langchain_groq import ChatGroq
from prompt import JUDGE_PROMPT
import json
from pydantic import BaseModel, Field
class Evaluation(BaseModel):
accuracy: int = Field(..., ge=0, le=10)
hallucination: bool = Field("True if the answer contains hallucnation else false")
feedback: str = Field("brief comment on the quality of the answer")
judge_llm = ChatGroq(
model_name = "llama-3.3-70b-versatile",
)
structured_judge_llm = judge_llm.with_structured_output(Evaluation)
def evaulate_answer(question:str, answer:str)->dict:
prompt = JUDGE_PROMPT.format(question = question, answer = answer)
try:
response = structured_judge_llm.invoke(prompt)
evaluation = response.model_dump() if hasattr(response, "model_dump") else response.dict()
except Exception as e:
evaluation={
"accuracy": 0,
"hallucination": True,
"feedback": f"Failed to parse evaluation: {e}"
}
return evaluation
question = "What is the capital of France?"
answer = "It is Berlin"
result = evaulate_answer(question,answer)
print("Evaluation result: ", result)