Skip to content

Commit

Permalink
feat: allow metric traces
Browse files Browse the repository at this point in the history
fixes: #250

Allow to retrieve metric logs calculation
  • Loading branch information
victor-liquidstudio committed Nov 16, 2023
1 parent 65b77f5 commit f5f9d39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ragas/metrics/_faithfulness.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Faithfulness(MetricWithLLM):
name: str = "faithfulness"
evaluation_mode: EvaluationMode = EvaluationMode.qac
batch_size: int = 15

def init_model(self):
super().init_model()
self.metric_traces = {
"statements": [],
"verdicts": []
}

def _score_batch(
self: t.Self,
Expand Down Expand Up @@ -91,6 +98,7 @@ def _score_batch(
statements = output[0].text.split("\n")
list_statements.append(statements)

self.metric_traces["statements"] += list_statements
prompts = []
for context, statements in zip(contexts, list_statements):
statements_str: str = "\n".join(
Expand All @@ -110,6 +118,7 @@ def _score_batch(
final_answer = final_answer.lower()
for i, output in enumerate(outputs):
output = output[0].text.lower().strip()
self.metric_traces["verdicts"].append(output)
if output.find(final_answer) != -1:
output = output[output.find(final_answer) + len(final_answer) :]
score = sum(
Expand Down
1 change: 1 addition & 0 deletions src/ragas/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def get_batches(self, dataset_size: int) -> list[range]:
@dataclass
class MetricWithLLM(Metric):
llm: RagasLLM = field(default_factory=llm_factory)
metric_traces: dict = field(default_factory=dict)

def init_model(self):
"""
Expand Down

0 comments on commit f5f9d39

Please sign in to comment.