Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(llmobs): implement answer relevancy ragas metric #11738

Closed
wants to merge 21 commits into from

Conversation

lievan
Copy link
Contributor

@lievan lievan commented Dec 16, 2024

Implements answer relevancy metric for ragas integration.

About Answer Relevancy

Answer relevancy metric focuses on assessing how pertinent the generated answer is to the given prompt. A lower score is assigned to answers that are incomplete or contain redundant information and higher scores indicate better relevancy. This metric is computed using the question, the retrieved contexts and the answer.

The Answer Relevancy is defined as the mean cosine similarity of the original question to a number of artificial questions, which where generated (reverse engineered) based on the response.

Example trace

image

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@lievan lievan requested a review from a team as a code owner December 16, 2024 15:25
Copy link
Contributor

CODEOWNERS have been resolved as:

ddtrace/llmobs/_evaluators/ragas/answer_relevancy.py                    @DataDog/ml-observability
tests/llmobs/llmobs_cassettes/tests.llmobs.test_llmobs_ragas_answer_relevancy_evaluator.emits_traces_and_evaluations_on_exit.yaml  @DataDog/ml-observability
tests/llmobs/llmobs_cassettes/tests.llmobs.test_llmobs_ragas_evaluators.test_ragas_answer_relevancy_emits_traces.yaml  @DataDog/ml-observability
tests/llmobs/llmobs_cassettes/tests.llmobs.test_llmobs_ragas_evaluators.test_ragas_answer_relevancy_submits_evaluation.yaml  @DataDog/ml-observability
tests/llmobs/llmobs_cassettes/tests.llmobs.test_llmobs_ragas_evaluators.test_ragas_answer_relevancy_submits_evaluation_on_span_with_custom_keys.yaml  @DataDog/ml-observability
tests/llmobs/llmobs_cassettes/tests.llmobs.test_llmobs_ragas_evaluators.test_ragas_answer_relevancy_submits_evaluation_on_span_with_question_in_messages.yaml  @DataDog/ml-observability
ddtrace/llmobs/_evaluators/ragas/base.py                                @DataDog/ml-observability
ddtrace/llmobs/_evaluators/ragas/models.py                              @DataDog/ml-observability
ddtrace/llmobs/_evaluators/runner.py                                    @DataDog/ml-observability
tests/llmobs/_utils.py                                                  @DataDog/ml-observability
tests/llmobs/conftest.py                                                @DataDog/ml-observability
tests/llmobs/test_llmobs_ragas_evaluators.py                            @DataDog/ml-observability

@datadog-dd-trace-py-rkomorn
Copy link

Datadog Report

Branch report: evan.li/answer-relevancy
Commit report: 812a162
Test service: dd-trace-py

✅ 0 Failed, 558 Passed, 910 Skipped, 7m 20.85s Total duration (28m 50.51s time saved)

@lievan lievan changed the base branch from evan.li/remaining-ragas-metrics to main December 16, 2024 15:38
@lievan lievan changed the base branch from main to evan.li/remaining-ragas-metrics December 16, 2024 15:38
@pr-commenter
Copy link

pr-commenter bot commented Dec 16, 2024

Benchmarks

Benchmark execution time: 2024-12-16 16:02:25

Comparing candidate commit 812a162 in PR branch evan.li/answer-relevancy with baseline commit 956e32d in branch evan.li/remaining-ragas-metrics.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 394 metrics, 2 unstable metrics.

Raises: NotImplementedError if the ragas library is not found or if ragas version is not supported.
"""
super().__init__(llmobs_service)
self.ragas_answer_relevancy_instance = self._get_answer_relevancy_instance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dumb question - what is the purpose of having a different LLM instance per eval metric runner? Are they not all references to the same base OpenAI() LLM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each eval metric runner has access to one instance of a ragas metric (answer_relevancy, context_precision, faithfulness) each of these ragas metrics have a seperate llm attribute. We maintain a reference to the ragas metric, not the llm attribute

but yea, the default is openai for all of them

Comment on lines +103 to +105
question = cp_inputs["question"]
contexts = cp_inputs["contexts"]
answer = cp_inputs["answer"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before, doesn't seem necessary to explicitly separate into new variables since they're only called once immediately at most

ddtrace/llmobs/_evaluators/ragas/base.py Outdated Show resolved Hide resolved
ddtrace/llmobs/_evaluators/ragas/base.py Outdated Show resolved Hide resolved
ddtrace/llmobs/_evaluators/ragas/base.py Outdated Show resolved Hide resolved
@@ -52,7 +54,7 @@ def __init__(self, interval: float, llmobs_service=None, evaluators=None):
if evaluator in SUPPORTED_EVALUATORS:
evaluator_init_state = "ok"
try:
self.evaluators.append(SUPPORTED_EVALUATORS[evaluator](llmobs_service=llmobs_service))
self.evaluators.append(SUPPORTED_EVALUATORS[evaluator](llmobs_service=llmobs_service)) # noqa: E501
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this fmt comment required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was a error being raised by mypy due to the use of ABC. I think it was a bug since it only appeared when we have 3 or more evaluators (see: python/mypy#13044). But we are not using an abstract class for BaseRagasEvaluator anyway, so no need for this anymore

@lievan lievan requested a review from a team as a code owner January 13, 2025 17:15
@lievan lievan requested review from a team as code owners January 13, 2025 17:15
@lievan lievan requested review from ZStriker19, erikayasuda, nikita-tkachenko-datadog and wconti27 and removed request for a team January 13, 2025 17:15
@lievan lievan changed the base branch from evan.li/remaining-ragas-metrics to main January 13, 2025 17:15
@lievan lievan requested review from a team and removed request for a team, ZStriker19, erikayasuda, nikita-tkachenko-datadog and wconti27 January 13, 2025 17:16
@lievan
Copy link
Contributor Author

lievan commented Jan 13, 2025

Going to re-open another PR (#11915) with latest changes from @main

@lievan lievan closed this Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants