-
Notifications
You must be signed in to change notification settings - Fork 977
feat(metrics): add ToolCallF1 for evaluating tool call precision, recall and F1 score #2096
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
Open
daiane-galvao
wants to merge
4
commits into
explodinggradients:main
Choose a base branch
from
daiane-galvao:feat/tool-call-f1-1893
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
efa63b6
feat(metrics): add ToolCallF1 for evaluating tool call precision, rec…
daiane-galvao 4a3ce83
feat(metrics): register ToolCallF1
daiane-galvao d51abf3
feat(metrics): PR suggestions ToolCallF1
daiane-galvao 68209da
fix(tool_call_f1): changes suggestions by AI pipeline
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
from dataclasses import dataclass, field | ||
|
||
from ragas.metrics.base import MultiTurnMetric, MetricType | ||
from ragas.dataset_schema import MultiTurnSample | ||
from ragas.messages import AIMessage | ||
|
||
if t.TYPE_CHECKING: | ||
from langchain_core.callbacks.base import Callbacks | ||
|
||
|
||
@dataclass | ||
class ToolCallF1(MultiTurnMetric): | ||
name: str = "tool_call_f1" | ||
batch_size: int = 1 | ||
is_multi_turn: bool = True | ||
_required_columns: t.Dict[MetricType, t.Set[str]] = field( | ||
default_factory=lambda: { | ||
MetricType.MULTI_TURN: { | ||
"reference_tool_calls", | ||
"user_input", | ||
} | ||
} | ||
) | ||
|
||
def init(self, run_config): | ||
pass | ||
|
||
async def _multi_turn_ascore( | ||
self, sample: MultiTurnSample, callbacks: t.Optional[Callbacks] = None | ||
) -> float: | ||
expected: set[tuple[str, frozenset]] = set() | ||
if sample.reference_tool_calls: | ||
for call in sample.reference_tool_calls: | ||
expected.add((call.name, frozenset(call.args.items()))) | ||
|
||
actual: set[tuple[str, frozenset]] = set() | ||
for msg in sample.user_input: | ||
if isinstance(msg, AIMessage) and msg.tool_calls is not None: | ||
for call in msg.tool_calls: | ||
actual.add((call.name, frozenset(call.args.items()))) | ||
|
||
tp = len(actual & expected) | ||
fp = len(actual - expected) | ||
fn = len(expected - actual) | ||
|
||
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0 | ||
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0 | ||
f1 = ( | ||
2 * precision * recall / (precision + recall) | ||
if (precision + recall) > 0 | ||
else 0.0 | ||
) | ||
|
||
return round(f1, 4) | ||
|
||
async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float: | ||
return await self._multi_turn_ascore(MultiTurnSample(**row), callbacks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pytest | ||
from ragas.messages import ToolCall, AIMessage, HumanMessage | ||
from ragas import MultiTurnSample | ||
from ragas.metrics import ToolCallF1 | ||
|
||
metric = ToolCallF1() | ||
|
||
|
||
def make_sample(expected, predicted): | ||
return MultiTurnSample( | ||
user_input=[ | ||
HumanMessage(content="What is the weather in Paris?"), | ||
AIMessage( | ||
content="Let me check the weather forecast", tool_calls=predicted | ||
), | ||
], | ||
reference_tool_calls=expected, | ||
reference="Expected correct weather tool call", | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_tool_call_f1_full_match(): | ||
expected = [ToolCall(name="WeatherForecast", args={"location": "Paris"})] | ||
predicted = [ToolCall(name="WeatherForecast", args={"location": "Paris"})] | ||
sample = make_sample(expected, predicted) | ||
score = await metric._multi_turn_ascore(sample) | ||
assert score == 1.0 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_tool_call_f1_partial_match(): | ||
expected = [ | ||
ToolCall(name="WeatherForecast", args={"location": "Paris"}), | ||
ToolCall(name="UVIndex", args={"location": "Paris"}), | ||
] | ||
predicted = [ToolCall(name="WeatherForecast", args={"location": "Paris"})] | ||
sample = make_sample(expected, predicted) | ||
score = await metric._multi_turn_ascore(sample) | ||
assert round(score, 2) == 0.67 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_tool_call_f1_no_match(): | ||
expected = [ToolCall(name="WeatherForecast", args={"location": "Paris"})] | ||
predicted = [ToolCall(name="AirQuality", args={"location": "Paris"})] | ||
sample = make_sample(expected, predicted) | ||
score = await metric._multi_turn_ascore(sample) | ||
assert score == 0.0 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_tool_call_f1_extra_call(): | ||
expected = [ToolCall(name="WeatherForecast", args={"location": "Paris"})] | ||
predicted = [ | ||
ToolCall(name="WeatherForecast", args={"location": "Paris"}), | ||
ToolCall(name="AirQuality", args={"location": "Paris"}), | ||
] | ||
sample = make_sample(expected, predicted) | ||
score = await metric._multi_turn_ascore(sample) | ||
assert round(score, 2) == 0.67 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.