Implementation:Neuml Txtai Callable Resolver
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, Configuration, Dynamic Resolution |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for resolving function configuration to callable function references within an embeddings instance provided by txtai.
Description
The Functions class resolves string-based function configurations into live callable references. This enables declarative configuration of custom SQL functions, pipelines, and other callable operations that can be used in embeddings queries.
The resolution process works as follows:
- String references: A string like
"database.reindex"is split on "." and each part is resolved step by step. The first part is looked up as an attribute of the embeddings instance. Subsequent parts create Reference objects that lazily resolve attributes on first invocation. - Module imports: If the first part is not an attribute of the embeddings instance, it is treated as a Python module path and imported via
__import__. - Non-string callables: Functions and other callables are passed through unchanged.
- Dict-style configs: If a function config element is a dict, the "function" key is resolved while other keys are preserved.
The companion Reference class implements lazy attribute resolution. A Reference stores an object handle and attribute name, resolving them only on first call. If the attribute is a function or method, it is executed with the provided arguments. If it is a plain attribute, its value is returned. References can be nested (a Reference's object can itself be a Reference), enabling chained attribute access like embeddings.database.reindex.
Usage
Use Functions when configuring custom SQL functions or callable operations in txtai embeddings. It is used internally by the embeddings system to resolve function declarations in configuration files into executable references. The lazy resolution ensures functions work correctly regardless of embeddings component initialization order.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File:
src/python/txtai/embeddings/index/functions.py
Signature
class Functions:
def __init__(self, embeddings)
def __call__(self, config) -> list
def reset(self)
def function(self, function) -> callable or Reference
class Reference:
def __init__(self, obj, attribute)
def __call__(self, *args) -> any
def reset(self)
Import
from txtai.embeddings.index.functions import Functions, Reference
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| embeddings | Embeddings | Yes | Embeddings instance used for resolving attribute references. |
| config | dict | Yes (__call__) | Configuration dictionary containing a functions key with a list of function definitions. Each element can be a string (module.path.function), a callable, or a dict with a "function" key. |
| obj | object | Yes (Reference) | Object handle for attribute lookup. |
| attribute | str | Yes (Reference) | Name of the attribute to resolve on the object. |
| args | any | No (Reference.__call__) | Arguments passed to the resolved function when the attribute is callable. |
Outputs
| Name | Type | Description |
|---|---|---|
| functions | list | List of resolved callable function references (or dicts with resolved "function" keys). |
| reference result | any | Result of calling the resolved attribute: function return value if callable, attribute value otherwise. |
Usage Examples
from txtai.embeddings import Embeddings
# Configure embeddings with custom SQL functions
embeddings = Embeddings({
"path": "sentence-transformers/all-MiniLM-L6-v2",
"content": True,
"functions": [
# Reference to an embeddings attribute method
{"name": "reindex", "function": "database.reindex"},
# Direct callable
{"name": "upper", "function": str.upper},
# Module path string
{"name": "custom", "function": "mymodule.custom_function"},
]
})
# Functions are resolved internally during embeddings initialization
# and can be used in SQL queries:
# embeddings.search("SELECT upper(text) FROM txtai WHERE similar('query')")
# The Functions class can also be used directly
from txtai.embeddings.index.functions import Functions
resolver = Functions(embeddings)
functions = resolver({
"functions": ["database.search"]
})
# functions[0] is now a callable Reference to embeddings.database.search
# Reset resolved references (useful when embeddings components are reinitialized)
resolver.reset()