Repository navigation
LCORE-0000: Refactor Agent Construction #3204
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Factory for building compaction capabilities.""" | ||
|
|
||
| from dataclasses import replace | ||
| from typing import Optional | ||
|
|
||
| from pydantic_ai_harness.compaction import ( | ||
| ClampOversizedMessages, | ||
| ClearToolResults, | ||
| SummarizingCompaction, | ||
| TieredCompaction, | ||
| ) | ||
|
|
||
| import constants | ||
| from agent.types.settings import CompactionSettings | ||
| from configuration import configuration | ||
| from providers.inference.errors import ( | ||
| ModelNotAvailableError, | ||
| ModelTypeNotSupportedError, | ||
| ProviderNotFoundError, | ||
| ) | ||
| from providers.inference.registry import ProviderRegistry | ||
| from utils.token_estimator import estimate_tokens | ||
|
|
||
|
|
||
| class CompactionFactory: # pylint: disable=too-few-public-methods | ||
| """Factory for building compaction capabilities.""" | ||
|
|
||
| @staticmethod | ||
| def build_capability(settings: CompactionSettings) -> Optional[TieredCompaction]: | ||
| """Build a tiered compaction capability from compaction settings. | ||
|
|
||
| Disabled settings and models that cannot be resolved produce no | ||
| capability. The summarizer and tier thresholds come from the loaded | ||
| compaction configuration. | ||
|
|
||
| Parameters: | ||
| settings: Compaction settings, including whether compaction is | ||
| enabled and which provider and model summarize history. | ||
|
|
||
| Returns: | ||
| A tiered compaction capability, or ``None`` when compaction is | ||
| disabled or the summarizer model cannot be resolved. | ||
|
|
||
| """ | ||
| if not settings.enabled: | ||
| return None | ||
|
|
||
| try: | ||
| model = ProviderRegistry().resolve_model_instance( | ||
| "llm", | ||
| settings.provider, | ||
| settings.model, | ||
| ) | ||
| except ( | ||
| ProviderNotFoundError, | ||
|
asimurka marked this conversation as resolved.
Resolved
|
||
| ModelNotAvailableError, | ||
| ModelTypeNotSupportedError, | ||
| ): | ||
|
Comment on lines
+54
to
+58
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.
|
||
| return None | ||
|
|
||
|
asimurka marked this conversation as resolved.
Resolved
asimurka marked this conversation as resolved.
Resolved
|
||
| # pylint: disable-next=fixme | ||
| # TODO: Implement context window calculation from ProviderRegistry() | ||
| context_window = None | ||
|
|
||
| keep_tokens = None | ||
| if context_window is not None: | ||
| keep_tokens = int( | ||
| context_window * configuration.compaction.buffer_max_ratio | ||
| ) | ||
|
|
||
| summarizer = SummarizingCompaction( | ||
| model=model, | ||
| max_messages=1, | ||
| keep_messages=configuration.compaction.buffer_turns, | ||
| keep_tokens=keep_tokens, | ||
| tokenizer=estimate_tokens, | ||
| incremental=True, | ||
| preserve_first_user_message=True, | ||
| keep_user_messages=True, | ||
| ) | ||
|
|
||
| if configuration.compaction.summary_prompt is not None: | ||
| summarizer = replace( | ||
| summarizer, summary_prompt=configuration.compaction.summary_prompt | ||
| ) | ||
|
|
||
| return TieredCompaction( | ||
| tiers=[ | ||
| ClampOversizedMessages( | ||
| max_part_tokens=50000, tokenizer=estimate_tokens | ||
| ), | ||
| ClearToolResults(max_tokens=1, tokenizer=estimate_tokens), | ||
| summarizer, | ||
| ], | ||
| target_fraction=configuration.compaction.threshold_ratio, | ||
| context_window=context_window, | ||
| fallback_context_window=constants.DEFAULT_CONTEXT_WINDOW, | ||
| tokenizer=estimate_tokens, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """Factory for building knowledge capabilities from knowledge source settings.""" | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from sentence_transformers import CrossEncoder | ||
|
|
||
| from agent.types.settings import KnowledgeSourceSettings | ||
| from configuration import configuration | ||
| from knowledge.capability import Knowledge | ||
| from knowledge.registry import KnowledgeSourceRegistry | ||
| from knowledge.sources.base import KnowledgeSource | ||
| from log import get_logger | ||
| from providers.inference.errors import ( | ||
| ModelNotAvailableError, | ||
| ModelTypeNotSupportedError, | ||
| ProviderNotFoundError, | ||
| ) | ||
| from providers.inference.registry import ProviderRegistry | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class KnowledgeFactory: # pylint: disable=too-few-public-methods | ||
| """Factory for building knowledge capabilities.""" | ||
|
|
||
| @staticmethod | ||
| def build_capabilities( | ||
| settings: list[KnowledgeSourceSettings], | ||
| ) -> Optional[Knowledge]: | ||
| """Build a knowledge capability from knowledge source settings. | ||
|
|
||
| Sources are resolved against the process knowledge-source registry. | ||
| Disabled settings are skipped. The query only selects which sources | ||
| are enabled. Inline and tool chunk caps are read from the loaded RAG | ||
| configuration. Each source's score weight is applied after merged | ||
| matches are ranked. When inline reranking is enabled, the | ||
| cross-encoder is loaded from the inference provider registry and | ||
| attached to the capability. OKP search mode and the chunk filter are | ||
| fixed on the registered OKP source. | ||
|
|
||
| Parameters: | ||
| settings: Per-request knowledge source selection. | ||
|
|
||
| Returns: | ||
| One knowledge capability covering every enabled, registered | ||
| source, or ``None`` when no sources are built. The capability's | ||
| reranker is ``None`` when reranking is disabled or the model | ||
| cannot be resolved. | ||
|
|
||
| """ | ||
| if sources := _get_enabled_sources(settings): | ||
| return Knowledge( | ||
| sources=sources, | ||
| reranker=_get_reranker(), | ||
| auto_max_matches=configuration.rag.retrieval.inline.max_chunks, | ||
| tool_max_matches=configuration.rag.retrieval.tool.max_chunks, | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| def _get_enabled_sources( | ||
| settings: list[KnowledgeSourceSettings], | ||
| ) -> list[KnowledgeSource]: | ||
| """Return registered sources for the given settings, in settings order. | ||
|
|
||
| Parameters: | ||
| settings: Knowledge source settings to resolve. | ||
|
|
||
| Returns: | ||
| Registered sources whose settings are enabled. Disabled settings and | ||
| unknown ids are skipped. Unknown ids are logged. | ||
|
|
||
| """ | ||
| registry = KnowledgeSourceRegistry() | ||
| sources: list[KnowledgeSource] = [] | ||
| for setting in settings: | ||
| if not setting.enabled: | ||
| continue | ||
| source = registry.find_source(setting.id) | ||
| if source is None: | ||
| logger.warning( | ||
| "Knowledge source '%s' is not registered; skipping", | ||
| setting.id, | ||
| ) | ||
| continue | ||
| sources.append(source) | ||
| return sources | ||
|
|
||
|
|
||
| def _get_reranker() -> Optional[CrossEncoder]: | ||
| """Resolve the inline reranker from the inference provider registry. | ||
|
|
||
| Uses ``configuration.reranker.provider`` and ``configuration.reranker.model`` | ||
| with the typed rerank getter. The model is already loaded on the provider. | ||
|
|
||
| Returns: | ||
| Loaded cross-encoder, or ``None`` when reranking is disabled or the | ||
| model cannot be resolved. | ||
|
|
||
| """ | ||
| reranker = configuration.reranker | ||
| if reranker is None or not reranker.enabled: | ||
| logger.debug("Reranker is disabled, not loading cross-encoder model") | ||
| return None | ||
|
|
||
| try: | ||
| model = ProviderRegistry().resolve_model_instance( | ||
| "rerank", | ||
| reranker.provider, | ||
| reranker.model, | ||
| ) | ||
| except ( | ||
| ProviderNotFoundError, | ||
| ModelNotAvailableError, | ||
| ModelTypeNotSupportedError, | ||
| ) as exc: | ||
| logger.warning( | ||
| "Could not resolve cross-encoder for reranking " | ||
| "(provider=%s model=%s): %s", | ||
| reranker.provider, | ||
| reranker.model, | ||
| exc, | ||
| ) | ||
| return None | ||
|
|
||
| logger.info( | ||
| "Resolved cross-encoder for knowledge reranking: provider=%s model=%s", | ||
| reranker.provider, | ||
| reranker.model, | ||
| ) | ||
| return model |
Uh oh!
There was an error while loading. Please reload this page.