fix(evaluator): inject configured LLM into custom metrics#134
Open
thesamet wants to merge 1 commit intoarklexai:mainfrom
Open
fix(evaluator): inject configured LLM into custom metrics#134thesamet wants to merge 1 commit intoarklexai:mainfrom
thesamet wants to merge 1 commit intoarklexai:mainfrom
Conversation
Custom metrics previously loaded the LLM themselves by reading a hardcoded `config.yaml` path at import time. This caused an immediate crash when the configured provider differed from what the metric expected (e.g. running with Anthropic while metrics tried to instantiate an OpenAI client). The evaluator now passes the already-configured LLM instance to any custom metric whose __init__ declares an `llm` parameter. Metrics without the parameter continue to work unchanged (backward-compatible). Update both built-in example metrics to use the injected LLM. Fixes arklexai#131
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Custom metrics previously loaded the LLM themselves by reading a hardcoded
config.yamlpath at import time, causing an immediate crash when the configured provider differed from what the metric expected (e.g. running with Anthropic while metrics tried to instantiate an OpenAI client).__init__signature and passes the already-configured LLM instance when anllmparameter is declaredself.llmwhen no LLM was injected raises a descriptiveRuntimeErrorinstead of an opaqueAttributeErrorbank-insurance,e-commerce) to use the injected LLMllm=Noneinjection patternCloses #131
Changes
arksim/evaluator/evaluator.py: detectllmparameter in custom metric constructors and inject the configured LLM instancearksim/evaluator/base_metric.py: exposellmas a property on both base classes that raises a clearRuntimeErrorwhen accessed without injection; addllmparameter docs to bothQuantitativeMetricandQualitativeMetricdocstringsexamples/bank-insurance/custom_metrics.py,examples/e-commerce/custom_metrics.py: remove hardcoded config loading, accept injected LLM viaself.llmdocs/main/evaluate-conversation.mdx: update both metric type examples to show thellm=Noneinjection patterntests/unit/test_evaluator_class.py: new tests covering injection for quant and qual metrics, backward-compat (no-param) case, andRuntimeErrorwhenself.llmis accessed without injectionDocumentation
docs/(if behavior, config, or API changed)README.md(if installation, quickstart, or usage changed)How to Test
ruff check .passesruff format --check .passespytest tests/passesNotes
Backward-compatible: any existing custom metric that does not declare an
llmparameter in__init__is instantiated as before. Only metrics that opt in by declaring the parameter receive the injected LLM. Metrics that declarellmbut try to useself.llmwithout injection now get a descriptive error pointing to the fix.Reviewers
/cc @arklexai/arksim-maintainers