Spaces:
Running
Running
Update app/graph/nodes/evaluator.py
Browse files- app/graph/nodes/evaluator.py +109 -43
app/graph/nodes/evaluator.py
CHANGED
|
@@ -1,43 +1,109 @@
|
|
| 1 |
-
# app/graph/nodes/evaluator.py
|
| 2 |
-
from app.core.llm_engine import llm
|
| 3 |
-
from app.core.prompts.evaluator_prompt import evaluator_prompt
|
| 4 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
-
import json
|
| 6 |
-
|
| 7 |
-
chain = evaluator_prompt | llm | StrOutputParser()
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def evaluator_node(state):
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # app/graph/nodes/evaluator.py
|
| 2 |
+
# from app.core.llm_engine import llm
|
| 3 |
+
# from app.core.prompts.evaluator_prompt import evaluator_prompt
|
| 4 |
+
# from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
# import json
|
| 6 |
+
|
| 7 |
+
# chain = evaluator_prompt | llm | StrOutputParser()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# def evaluator_node(state):
|
| 11 |
+
# query = state.get("query")
|
| 12 |
+
# answer = state.get("final_answer")
|
| 13 |
+
# context = state.get("context", "")
|
| 14 |
+
|
| 15 |
+
# try:
|
| 16 |
+
# response = chain.invoke({
|
| 17 |
+
# "query": query,
|
| 18 |
+
# "answer": answer,
|
| 19 |
+
# "context": context
|
| 20 |
+
# })
|
| 21 |
+
|
| 22 |
+
# # π₯ clean response (important)
|
| 23 |
+
# response = response.strip()
|
| 24 |
+
|
| 25 |
+
# # sometimes model adds ```json
|
| 26 |
+
# if response.startswith("```"):
|
| 27 |
+
# response = response.replace("```json", "").replace("```", "").strip()
|
| 28 |
+
|
| 29 |
+
# evaluation = json.loads(response)
|
| 30 |
+
|
| 31 |
+
# except Exception as e:
|
| 32 |
+
# print("EVALUATOR ERROR β", e)
|
| 33 |
+
|
| 34 |
+
# evaluation = {
|
| 35 |
+
# "relevance_score": 0.5,
|
| 36 |
+
# "context_usage": 0.5,
|
| 37 |
+
# "hallucination": True
|
| 38 |
+
# }
|
| 39 |
+
|
| 40 |
+
# return {
|
| 41 |
+
# **state,
|
| 42 |
+
# "evaluation": evaluation
|
| 43 |
+
# }
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# app/graph/nodes/evaluator.py
|
| 53 |
+
from app.core.llm_engine import llm
|
| 54 |
+
from app.core.prompts.evaluator_prompt import evaluator_prompt
|
| 55 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 56 |
+
import json, re
|
| 57 |
+
|
| 58 |
+
chain = evaluator_prompt | llm | StrOutputParser()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def evaluator_node(state):
|
| 62 |
+
query = state.get("query")
|
| 63 |
+
answer = state.get("final_answer")
|
| 64 |
+
context = state.get("context", "")
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
|
| 68 |
+
response = chain.invoke({
|
| 69 |
+
"query": query,
|
| 70 |
+
"answer": answer,
|
| 71 |
+
"context": context
|
| 72 |
+
}).strip()
|
| 73 |
+
|
| 74 |
+
# π₯ remove markdown/code blocks
|
| 75 |
+
response = re.sub(r"```.*?```", "", response, flags=re.DOTALL).strip()
|
| 76 |
+
|
| 77 |
+
# π₯ extract JSON only
|
| 78 |
+
match = re.search(r"\{.*\}", response, re.DOTALL)
|
| 79 |
+
|
| 80 |
+
if match:
|
| 81 |
+
response = match.group(0)
|
| 82 |
+
|
| 83 |
+
# π₯ validate JSON start
|
| 84 |
+
if not response.startswith("{"):
|
| 85 |
+
raise ValueError("Invalid JSON from LLM")
|
| 86 |
+
|
| 87 |
+
evaluation = json.loads(response)
|
| 88 |
+
|
| 89 |
+
# π₯ clamp values
|
| 90 |
+
evaluation = {
|
| 91 |
+
"relevance_score": min(max(evaluation.get("relevance_score", 0), 0), 1),
|
| 92 |
+
"context_usage": min(max(evaluation.get("context_usage", 0), 0), 1),
|
| 93 |
+
"hallucination": bool(evaluation.get("hallucination", True))
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print("EVALUATOR ERROR β", e)
|
| 98 |
+
|
| 99 |
+
evaluation = {
|
| 100 |
+
"relevance_score": 0.5,
|
| 101 |
+
"context_usage": 0.5,
|
| 102 |
+
"hallucination": True
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
return {
|
| 106 |
+
**state,
|
| 107 |
+
"evaluation": evaluation
|
| 108 |
+
}
|
| 109 |
+
|