Skip to content

Commit

Permalink
Refactored metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
antoninoLorenzo committed Jul 16, 2024
1 parent 93ac087 commit 0839866
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions test/benchmarks/rag/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ def compute(self, *args, **kwargs) -> float:
"""Needs to be implemented to evaluate a metric"""
pass

def query(self, sys_prompt: str, usr_prompt: str) -> str:
""""""
messages = [
{'role': 'system', 'content': sys_prompt},
{'role': 'user', 'content': usr_prompt}
]
response = self.llm.query(messages)
result = ''
for chunk in response:
result += chunk
return self.extract_response(result)

@staticmethod
def extract_response(response):
"""Extracts the json results from response"""
Expand All @@ -119,31 +131,19 @@ class ContextRecall(Metric):

def compute(self, answer: str, context: str):
"""Computes context recall given answer and context"""
messages = [
{'role': 'system', 'content': self.system_prompt},
{'role': 'user', 'content': self.user_prompt.format(answer=answer, context=context)}
]

response = self.llm.query(messages)
result = ''
for chunk in response:
result += chunk
return self.extract_response(result)
return self.query(
self.system_prompt,
self.user_prompt.format(answer=answer, context=context)
)


class ContextPrecision(Metric):
"""Assesses how much the context was useful in generating the answer"""

def compute(self, question: str, answer: str, context: str):
"""Uses question, answer and context"""
messages = [
{'role': 'system', 'content': self.system_prompt},
{'role': 'user', 'content': self.user_prompt.format(question=question, answer=answer, context=context)}
]

response = self.llm.query(messages)
result = ''
for chunk in response:
result += chunk
return self.extract_response(result)
return self.query(
self.system_prompt,
self.user_prompt.format(question=question, answer=answer, context=context)
)

0 comments on commit 0839866

Please sign in to comment.