Open xidian237 opened 2 months ago
To resolve the AssertionError: "AnswerSimilarity must be set"
when using the answer_correctness
metric in Ragas version 0.1.16 with Python 3.11.9, you need to ensure that the AnswerSimilarity
object is properly initialized. This is required when the second weight in the weights
list is not zero.
Here is the relevant part of the code that checks for the AnswerSimilarity
object:
if self.answer_similarity is None and self.weights[1] != 0:
self.answer_similarity = AnswerSimilarity(
llm=self.llm, embeddings=self.embeddings
)
Make sure that the AnswerSimilarity
object is set correctly in the init
method of the AnswerCorrectness
class. Here is the complete init
method for reference:
def init(self, run_config: RunConfig):
super().init(run_config)
if self.answer_similarity is None and self.weights[1] != 0:
self.answer_similarity = AnswerSimilarity(
llm=self.llm, embeddings=self.embeddings
)
Ensure that you call the init
method with the appropriate RunConfig
object before using the answer_correctness
metric. This will initialize the AnswerSimilarity
object if it is not already set and the second weight is not zero.
Here is an example of how you might initialize and use the AnswerCorrectness
metric:
from ragas.run_config import RunConfig
from ragas.metrics._answer_correctness import AnswerCorrectness
# Create a RunConfig object with necessary parameters
run_config = RunConfig(llm=your_llm, embeddings=your_embeddings)
# Initialize the AnswerCorrectness metric
answer_correctness = AnswerCorrectness()
answer_correctness.init(run_config)
# Now you can use the answer_correctness metric without encountering the AssertionError
Replace your_llm
and your_embeddings
with the appropriate objects required for your use case [1].
To continue talking to Dosu, mention @dosu.
@dosu
The weights is default:
weights: list[float] = field(default_factory=lambda: [0.75, 0.25])
To resolve the AssertionError: "AnswerSimilarity must be set"
when the weights
are set to their default values [0.75, 0.25]
, you need to ensure that the answer_similarity
attribute is properly initialized in the AnswerCorrectness
class. This is done in the init
method.
Here is the relevant part of the code:
def init(self, run_config: RunConfig):
super().init(run_config)
if self.answer_similarity is None and self.weights[1] != 0:
self.answer_similarity = AnswerSimilarity(
llm=self.llm, embeddings=self.embeddings
)
Make sure that the init
method is called with the appropriate RunConfig
and that llm
and embeddings
are properly initialized before calling this method. This will ensure that answer_similarity
is set correctly, avoiding the AssertionError
.
Here is an example of how you might initialize and use the AnswerCorrectness
metric:
from ragas.run_config import RunConfig
from ragas.metrics._answer_correctness import AnswerCorrectness
# Assuming llm and embeddings are already initialized
run_config = RunConfig(llm=your_llm, embeddings=your_embeddings)
answer_correctness_metric = AnswerCorrectness()
answer_correctness_metric.init(run_config)
This ensures that the answer_similarity
attribute is set when the weights
are [0.75, 0.25]
[1].
To continue talking to Dosu, mention @dosu.
@dosu, This is my code:
metrics = [globals()[metric_name] for metric_name in [metric_name]] result = evaluate(dataset=dataset, metrics=metrics, llm=gen_ai_hub_llm, embeddings=gen_ai_hub_embedding_model, raise_exceptions=True) print(result)
I didn't use the way you mentioned.
To resolve the AssertionError: "AnswerSimilarity must be set"
in your code, you need to ensure that the AnswerCorrectness
metric is properly initialized with the AnswerSimilarity
object. This can be done by calling the init
method with the appropriate RunConfig
object before using the evaluate
function.
Here is how you can modify your code to include the initialization step:
from ragas.run_config import RunConfig
from ragas.metrics._answer_correctness import AnswerCorrectness
# Initialize the RunConfig with your LLM and embeddings
run_config = RunConfig(llm=gen_ai_hub_llm, embeddings=gen_ai_hub_embedding_model)
# Initialize the AnswerCorrectness metric
answer_correctness_metric = AnswerCorrectness()
answer_correctness_metric.init(run_config)
# Use the initialized metric in the evaluate function
metrics = [answer_correctness_metric]
result = evaluate(dataset=dataset, metrics=metrics, llm=gen_ai_hub_llm,
embeddings=gen_ai_hub_embedding_model, raise_exceptions=True)
print(result)
This ensures that the AnswerSimilarity
object is properly set in the AnswerCorrectness
metric, avoiding the AssertionError
[1].
To continue talking to Dosu, mention @dosu.
[ ] I have checked the documentation and related resources and couldn't resolve my bug.
Hi experts,
I use the answer_correctness as the metric, but it failed due to AssertionError: "AssertionError: AnswerSimilarity must be set".
Ragas version: 0.1.16 Python version: 3.11.9
Code to Reproduce I want to call the answer_correctness, here is the test code, and, this aserror occurs occasionally.:
Error trace
Additional context I noticed that the AnswerSimilarity will be inited: