LoRA Dispatcher

LoRA Dispatcher with Intelligent Routing

Automatically selects and applies the most suitable LoRA adapter based on query content using sentence embeddings and classification.

class ual_adapter.core.dispatcher.LoRADispatcher(encoder_model: str = 'all-MiniLM-L6-v2', confidence_threshold: float = 0.7, cache_embeddings: bool = True)[source]

Bases: object

Intelligent dispatcher that routes queries to appropriate domain LoRAs.

Uses sentence embeddings and a trained classifier to determine which domain-specific adapter to use for a given query.

__init__(encoder_model: str = 'all-MiniLM-L6-v2', confidence_threshold: float = 0.7, cache_embeddings: bool = True)[source]

Initialize the LoRA dispatcher.

Parameters:
  • encoder_model – Sentence transformer model for embeddings

  • confidence_threshold – Minimum confidence for domain selection

  • cache_embeddings – Whether to cache computed embeddings

register_domain(domain_name: str, adapter_weights: Dict[str, Tensor], training_texts: List[str], metadata: Dict[str, Any] | None = None) None[source]

Register a domain-specific LoRA adapter.

Parameters:
  • domain_name – Name of the domain

  • adapter_weights – LoRA weights for this domain

  • training_texts – Sample texts from this domain for routing

  • metadata – Optional metadata about the domain

route_query(query: str, return_all_scores: bool = False) Tuple[str | None, float, Dict[str, float] | None][source]

Determine which domain adapter to use for a query.

Parameters:
  • query – The input query text

  • return_all_scores – Whether to return scores for all domains

Returns:

Tuple of (selected_domain, confidence, all_scores_dict)

apply_adapter(model: Module, query: str, verbose: bool = True) Tuple[Module, str, float][source]

Apply the appropriate adapter to a model based on the query.

Parameters:
  • model – The base model to apply adapter to

  • query – The input query

  • verbose – Whether to log routing decisions

Returns:

Tuple of (model_with_adapter, selected_domain, confidence)

analyze_domain_overlap() Dict[str, Any][source]

Analyze overlap between registered domains.

save(path: str) None[source]

Save dispatcher state to disk.

classmethod load(path: str) LoRADispatcher[source]

Load dispatcher state from disk.

class ual_adapter.core.dispatcher.DomainAdapter(name: str, weights: Dict[str, Tensor], metadata: Dict[str, Any])[source]

Bases: object

Container for domain-specific adapter information.

__init__(name: str, weights: Dict[str, Tensor], metadata: Dict[str, Any])[source]

Initialize domain adapter.

Parameters:
  • name – Domain name

  • weights – LoRA weights for this domain

  • metadata – Additional information about the domain

get_info() Dict[str, Any][source]

Get information about this domain adapter.

LoRADispatcher Class

class ual_adapter.core.dispatcher.LoRADispatcher(encoder_model: str = 'all-MiniLM-L6-v2', confidence_threshold: float = 0.7, cache_embeddings: bool = True)[source]

Intelligent dispatcher that routes queries to appropriate domain LoRAs.

Uses sentence embeddings and a trained classifier to determine which domain-specific adapter to use for a given query.

__init__(encoder_model: str = 'all-MiniLM-L6-v2', confidence_threshold: float = 0.7, cache_embeddings: bool = True)[source]

Initialize the LoRA dispatcher.

Parameters:
  • encoder_model – Sentence transformer model for embeddings

  • confidence_threshold – Minimum confidence for domain selection

  • cache_embeddings – Whether to cache computed embeddings

register_domain(domain_name: str, adapter_weights: Dict[str, Tensor], training_texts: List[str], metadata: Dict[str, Any] | None = None) None[source]

Register a domain-specific LoRA adapter.

Parameters:
  • domain_name – Name of the domain

  • adapter_weights – LoRA weights for this domain

  • training_texts – Sample texts from this domain for routing

  • metadata – Optional metadata about the domain

route_query(query: str, return_all_scores: bool = False) Tuple[str | None, float, Dict[str, float] | None][source]

Determine which domain adapter to use for a query.

Parameters:
  • query – The input query text

  • return_all_scores – Whether to return scores for all domains

Returns:

Tuple of (selected_domain, confidence, all_scores_dict)

apply_adapter(model: Module, query: str, verbose: bool = True) Tuple[Module, str, float][source]

Apply the appropriate adapter to a model based on the query.

Parameters:
  • model – The base model to apply adapter to

  • query – The input query

  • verbose – Whether to log routing decisions

Returns:

Tuple of (model_with_adapter, selected_domain, confidence)

analyze_domain_overlap() Dict[str, Any][source]

Analyze overlap between registered domains.

save(path: str) None[source]

Save dispatcher state to disk.

classmethod load(path: str) LoRADispatcher[source]

Load dispatcher state from disk.

DomainAdapter Class

class ual_adapter.core.dispatcher.DomainAdapter(name: str, weights: Dict[str, Tensor], metadata: Dict[str, Any])[source]

Container for domain-specific adapter information.

__init__(name: str, weights: Dict[str, Tensor], metadata: Dict[str, Any])[source]

Initialize domain adapter.

Parameters:
  • name – Domain name

  • weights – LoRA weights for this domain

  • metadata – Additional information about the domain

get_info() Dict[str, Any][source]

Get information about this domain adapter.