-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add AgentConfigUpdate & initial judges
#4547
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
theomonnom
wants to merge
7
commits into
main
Choose a base branch
from
theo/config-update
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9af2755
wip
theomonnom 754ce38
wip
theomonnom c703089
Update judge.py
theomonnom dfd036f
fix
theomonnom 94e4e53
Update observability.py
theomonnom 5a2c218
support LIVEKIT_EVALS_VERBOSE & fix judges
theomonnom baed998
use function tools
theomonnom 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,38 @@ | ||
| from .evaluation import ( | ||
| EvaluationResult, | ||
| Evaluator, | ||
| JudgeGroup, | ||
| ) | ||
| from .judge import ( | ||
| Judge, | ||
| JudgmentResult, | ||
| Verdict, | ||
| accuracy_judge, | ||
| coherence_judge, | ||
| conciseness_judge, | ||
| handoff_judge, | ||
| relevancy_judge, | ||
| safety_judge, | ||
| task_completion_judge, | ||
| tool_use_judge, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| # Evaluation | ||
| "EvaluationResult", | ||
| "Evaluator", | ||
| "JudgeGroup", | ||
| # Core types | ||
| "Judge", | ||
| "JudgmentResult", | ||
| "Verdict", | ||
| # Built-in judges | ||
| "accuracy_judge", | ||
| "coherence_judge", | ||
| "conciseness_judge", | ||
| "handoff_judge", | ||
| "relevancy_judge", | ||
| "safety_judge", | ||
| "task_completion_judge", | ||
| "tool_use_judge", | ||
| ] |
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,189 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import os | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING, Protocol | ||
|
|
||
| from ..llm import LLM, ChatContext | ||
| from .judge import JudgmentResult | ||
|
|
||
| _evals_verbose = int(os.getenv("LIVEKIT_EVALS_VERBOSE", 0)) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..inference import LLMModels | ||
|
|
||
|
|
||
| class Evaluator(Protocol): | ||
| """Protocol for any object that can evaluate a conversation.""" | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Name identifying this evaluator.""" | ||
| ... | ||
|
|
||
| async def evaluate( | ||
| self, | ||
| *, | ||
| chat_ctx: ChatContext, | ||
| reference: ChatContext | None = None, | ||
| llm: LLM | None = None, | ||
| ) -> JudgmentResult: ... | ||
|
|
||
|
|
||
| @dataclass | ||
| class EvaluationResult: | ||
| """Result of evaluating a conversation with a group of judges.""" | ||
|
|
||
| judgments: dict[str, JudgmentResult] = field(default_factory=dict) | ||
| """Individual judgment results keyed by judge name.""" | ||
|
|
||
| @property | ||
| def score(self) -> float: | ||
| """Score from 0.0 to 1.0. Pass=1, maybe=0.5, fail=0.""" | ||
| if not self.judgments: | ||
| return 0.0 | ||
| total = 0.0 | ||
| for j in self.judgments.values(): | ||
| if j.passed: | ||
| total += 1.0 | ||
| elif j.uncertain: | ||
| total += 0.5 | ||
| return total / len(self.judgments) | ||
|
|
||
| @property | ||
| def all_passed(self) -> bool: | ||
| """True if all judgments passed. Maybes count as not passed.""" | ||
| return all(j.passed for j in self.judgments.values()) | ||
|
|
||
| @property | ||
| def any_passed(self) -> bool: | ||
| """True if at least one judgment passed.""" | ||
| return any(j.passed for j in self.judgments.values()) | ||
|
|
||
| @property | ||
| def majority_passed(self) -> bool: | ||
| """True if more than half of the judgments passed.""" | ||
| if not self.judgments: | ||
| return True | ||
| return self.score > len(self.judgments) / 2 | ||
|
Comment on lines
+64
to
+69
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.
🐛 Suggested fix def majority_passed(self) -> bool:
"""True if more than half of the judgments passed."""
if not self.judgments:
return True
- return self.score > len(self.judgments) / 2
+ passed = sum(1 for j in self.judgments.values() if j.passed)
+ return passed > len(self.judgments) / 2🤖 Prompt for AI Agents |
||
|
|
||
| @property | ||
| def none_failed(self) -> bool: | ||
| """True if no judgments explicitly failed. Maybes are allowed.""" | ||
| return not any(j.failed for j in self.judgments.values()) | ||
|
|
||
| class JudgeGroup: | ||
| """A group of judges that evaluate conversations together. | ||
|
|
||
| Automatically tags the session with judgment results when called within a job context. | ||
|
|
||
| Example: | ||
| ```python | ||
| async def on_session_end(ctx: JobContext) -> None: | ||
| judges = JudgeGroup( | ||
| llm="openai/gpt-4o-mini", | ||
| judges=[ | ||
| task_completion_judge(), | ||
| accuracy_judge(), | ||
| ], | ||
| ) | ||
|
|
||
| report = ctx.make_session_report() | ||
| result = await judges.evaluate(report.chat_history) | ||
| # Results are automatically tagged to the session | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| llm: LLM | LLMModels | str, | ||
| judges: list[Evaluator] | None = None, | ||
| ) -> None: | ||
| """Initialize a JudgeGroup. | ||
|
|
||
| Args: | ||
| llm: The LLM to use for evaluation. Can be an LLM instance or a model | ||
| string like "openai/gpt-4o-mini" (uses LiveKit inference gateway). | ||
| judges: The judges to run during evaluation. | ||
| """ | ||
| if isinstance(llm, str): | ||
| from ..inference import LLM as InferenceLLM | ||
|
|
||
| self._llm: LLM = InferenceLLM(llm) | ||
| else: | ||
| self._llm = llm | ||
|
|
||
| self._judges = judges or [] | ||
|
|
||
| @property | ||
| def llm(self) -> LLM: | ||
| """The LLM used for evaluation.""" | ||
| return self._llm | ||
|
|
||
| @property | ||
| def judges(self) -> list[Evaluator]: | ||
| """The judges to run during evaluation.""" | ||
| return self._judges | ||
|
|
||
| async def evaluate( | ||
| self, | ||
| chat_ctx: ChatContext, | ||
| *, | ||
| reference: ChatContext | None = None, | ||
| ) -> EvaluationResult: | ||
| """Evaluate a conversation with all judges. | ||
|
|
||
| Automatically tags the session with results when called within a job context. | ||
|
|
||
| Args: | ||
| chat_ctx: The conversation to evaluate. | ||
| reference: Optional reference conversation for comparison. | ||
|
|
||
| Returns: | ||
| EvaluationResult containing all judgment results. | ||
| """ | ||
| from ..job import get_job_context | ||
| from ..log import logger | ||
|
|
||
| # Run all judges concurrently | ||
| async def run_judge(judge: Evaluator) -> tuple[str, JudgmentResult | BaseException]: | ||
| try: | ||
| result = await judge.evaluate( | ||
| chat_ctx=chat_ctx, | ||
| reference=reference, | ||
| llm=self._llm, | ||
| ) | ||
| return judge.name, result | ||
| except Exception as e: | ||
| logger.warning(f"Judge '{judge.name}' failed: {e}") | ||
| return judge.name, e | ||
|
|
||
| results = await asyncio.gather(*[run_judge(j) for j in self._judges]) | ||
|
|
||
| # Filter out failed judges | ||
| judgments: dict[str, JudgmentResult] = {} | ||
| for name, result in results: | ||
| if isinstance(result, JudgmentResult): | ||
| judgments[name] = result | ||
|
|
||
| evaluation_result = EvaluationResult(judgments=judgments) | ||
|
|
||
| if _evals_verbose: | ||
| print("\n+ JudgeGroup evaluation results:") | ||
| for name, result in results: | ||
| if isinstance(result, JudgmentResult): | ||
| print(f" [{name}] verdict={result.verdict}") | ||
| print(f" reasoning: {result.reasoning}\n") | ||
| else: | ||
| print(f" [{name}] ERROR: {result}\n") | ||
|
|
||
| # Auto-tag if running within a job context | ||
| try: | ||
| ctx = get_job_context() | ||
| ctx.tagger._evaluation(evaluation_result) | ||
| except RuntimeError: | ||
| pass # Not in a job context, skip tagging | ||
|
|
||
| return evaluation_result | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Short-session early return skips tagging outcomes.
At Line 180–182, returning early prevents
ctx.tagger.success()/fail(...)from being called, so short sessions never get tagged—even if a booking completed. Consider skipping evaluation but still tagging.🔧 Suggested restructure
🤖 Prompt for AI Agents
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.
it's fine bro
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.
@theomonnom, understood! I'll leave the implementation as is. 👍