Spaces:
Sleeping

rag_saas / app /services /evaluation.py
GodRad's picture
Upload 31 files
abd4592 verified
Raw History Blame Contribute Delete
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)