Download app/services/evaluation.py from GodRad/rag_saas: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/GodRad/rag_saas/resolve/main/app/services/evaluation.py
- Command line
-
hf download hf://spaces/GodRad/rag_saas/app/services/evaluation.py
-
curl -L -o evaluation.py https://huggingface.co/spaces/GodRad/rag_saas/resolve/main/app/services/evaluation.py
1.61 kB
| from typing import List, Dict, Any | |
| from litellm import completion | |
| from app.core.config import settings | |
| class EvaluationService: | |
| async def evaluate_faithfulness(self, query: str, answer: str, context: List[str]) -> Dict[str, Any]: | |
| prompt = f""" | |
| Evaluate if the following answer is faithful to the context. | |
| Provide a score from 0 to 1 and a brief reason. | |
| Context: | |
| {" ".join(context)} | |
| Answer: | |
| {answer} | |
| Output JSON: | |
| {{ | |
| "score": 0.0, | |
| "reason": "" | |
| }} | |
| """ | |
| response = await completion( | |
| model=settings.DEFAULT_LLM_MODEL, | |
| messages=[{"role": "user", "content": prompt}], | |
| response_format={"type": "json_object"} | |
| ) | |
| import json | |
| return json.loads(response.choices[0].message.content) | |
| async def evaluate_relevance(self, query: str, answer: str) -> Dict[str, Any]: | |
| prompt = f""" | |
| Evaluate if the following answer is relevant to the user's query. | |
| Provide a score from 0 to 1 and a brief reason. | |
| Query: | |
| {query} | |
| Answer: | |
| {answer} | |
| Output JSON: | |
| {{ | |
| "score": 0.0, | |
| "reason": "" | |
| }} | |
| """ | |
| response = await completion( | |
| model=settings.DEFAULT_LLM_MODEL, | |
| messages=[{"role": "user", "content": prompt}], | |
| response_format={"type": "json_object"} | |
| ) | |
| import json | |
| return json.loads(response.choices[0].message.content) | |