-
Notifications
You must be signed in to change notification settings - Fork 20
feat: support 'same-as-agent' model option for legacy evaluators #1048
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
Chibionos
wants to merge
2
commits into
main
Choose a base branch
from
feat/same-as-agent-evaluator-model
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.
+120
−11
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,16 @@ | |
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
| from time import time | ||
| from typing import Any, Awaitable, Iterable, Iterator, Sequence, Tuple | ||
| from typing import ( | ||
| Any, | ||
| Awaitable, | ||
| Iterable, | ||
| Iterator, | ||
| Protocol, | ||
| Sequence, | ||
| Tuple, | ||
| runtime_checkable, | ||
| ) | ||
|
|
||
| import coverage | ||
| from opentelemetry import context as context_api | ||
|
|
@@ -67,6 +76,27 @@ | |
| set_execution_context, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class LLMAgentFactoryProtocol(Protocol): | ||
| """Protocol for factories that can provide agent model information. | ||
|
|
||
| Runtime factories that implement this protocol can be queried for | ||
| the agent's configured LLM model, enabling features like 'same-as-agent' | ||
| model resolution for evaluators. | ||
| """ | ||
|
|
||
| def get_agent_model(self) -> str | None: | ||
| """Return the agent's configured LLM model name. | ||
|
|
||
| Returns: | ||
| The model name from agent settings (e.g., 'gpt-4o-2024-11-20'), | ||
| or None if no model is configured. | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| class ExecutionSpanExporter(SpanExporter): | ||
| """Custom exporter that stores spans grouped by execution ids.""" | ||
|
|
@@ -601,6 +631,41 @@ async def run_evaluator( | |
|
|
||
| return result | ||
|
|
||
| def _get_agent_model(self) -> str | None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens when we specify a custom model settings cc - @mathurk @AAgnihotry |
||
| """Get agent model from factory or agent.json fallback. | ||
|
|
||
| First checks if the runtime factory implements LLMAgentFactoryProtocol | ||
| and can provide the model directly. Falls back to reading agent.json | ||
| from disk if the protocol is not implemented. | ||
|
|
||
| Returns: | ||
| The model name from agent settings, or None if not found. | ||
| """ | ||
| # Prefer getting model from factory if it implements the protocol | ||
| if isinstance(self.factory, LLMAgentFactoryProtocol): | ||
| model = self.factory.get_agent_model() | ||
| if model: | ||
| logger.debug(f"Got agent model from factory: {model}") | ||
| return model | ||
|
|
||
| # Fallback: read from agent.json file | ||
| if self.context.entrypoint: | ||
| agent_json = Path(self.context.entrypoint) | ||
| else: | ||
| agent_json = Path.cwd() / "agent.json" | ||
|
|
||
| if agent_json.exists(): | ||
| try: | ||
| with open(agent_json, "r", encoding="utf-8") as f: | ||
| data = json.load(f) | ||
| model = data.get("settings", {}).get("model") | ||
| if model: | ||
| logger.debug(f"Got agent model from file: {model}") | ||
| return model | ||
| except (json.JSONDecodeError, OSError): | ||
| return None | ||
| return None | ||
|
|
||
| def _load_evaluators( | ||
| self, evaluation_set: EvaluationSet | ||
| ) -> list[BaseEvaluator[Any, Any, Any]]: | ||
|
|
@@ -611,6 +676,9 @@ def _load_evaluators( | |
| raise ValueError("eval_set cannot be None") | ||
| evaluators_dir = Path(eval_set).parent.parent / "evaluators" | ||
|
|
||
| # Load agent model for 'same-as-agent' resolution in legacy evaluators | ||
| agent_model = self._get_agent_model() | ||
|
|
||
| # If evaluatorConfigs is specified, use that (new field with weights) | ||
| # Otherwise, fall back to evaluatorRefs (old field without weights) | ||
| if ( | ||
|
|
@@ -638,7 +706,9 @@ def _load_evaluators( | |
| try: | ||
| evaluator_id = data.get("id") | ||
| if evaluator_id in evaluator_ref_ids: | ||
| evaluator = EvaluatorFactory.create_evaluator(data, evaluators_dir) | ||
| evaluator = EvaluatorFactory.create_evaluator( | ||
| data, evaluators_dir, agent_model=agent_model | ||
| ) | ||
| evaluators.append(evaluator) | ||
| found_evaluator_ids.add(evaluator_id) | ||
| except Exception as e: | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would suggest passing in
agent_model_settingsobject instead, so that @mathurk @AAgnihotry do not have to do double work :)