from ragas_experimental.llm import ragas_llmfrom openai import OpenAIllm = ragas_llm(provider="openai",model="gpt-4o",client=OpenAI())my_metric = NumericMetric( name='helpfulness', llm=llm, prompt="Evaluate if given answer is helpful\n\n{response}",range=(0,10),)result = my_metric.score(response="this is my response")result #gives "low"result.reason #gives reasoning from llm
"The provided input lacks context or content to determine if it is helpful as it merely states 'this is my response' without any additional information."
Write custom numeric metric
from ragas_experimental.metric import MetricResult@numeric_metric(llm=llm, prompt="Evaluate if given answer is helpful\n\n{response}", name='new_metric',range=(0,10))def my_metric(llm,prompt,**kwargs):class response_model(BaseModel): output: int reason: str traces = {} traces['input'] = kwargs response = llm.generate(prompt.format(**kwargs),response_model=response_model) traces['output'] = response.dict() total = response.outputif total <1: score =0else: score =10return MetricResult(result=score,reason=response.reason,traces=traces)result = my_metric.score(response='my response') # resultresult # 10result.reason # the reason for the answerresult1 = my_metric.score(response='my response 1') # resultresult2 = my_metric.score(response='my response 2') # resultresult1 + result2 # should be addable and behave like a float