Collection
Tip
To understand the general idea better, visit the Collection concept page.
dbally.create_collection
create_collection(name: str, llm_client: LLMClient, event_handlers: Optional[List[EventHandler]] = None, view_selector: Optional[ViewSelector] = None, nl_responder: Optional[NLResponder] = None) -> Collection
Create a new Collection that is a container for registering views and the main entrypoint to db-ally features.
Unlike instantiating a Collection directly, this function provides a set of default values for various dependencies like LLM client, view selector, IQL generator, and NL responder.
Example
from dbally import create_collection
from dbally.llm_client.openai_client import OpenAIClient
collection = create_collection("my_collection", llm_client=OpenAIClient())
PARAMETER | DESCRIPTION |
---|---|
name |
Name of the collection is available for Event handlers and is used to distinguish different db-ally runs.
TYPE:
|
llm_client |
LLM client used by the collection to generate views and respond to natural language queries.
TYPE:
|
event_handlers |
Event handlers used by the collection during query executions. Can be used to log events as CLIEventHandler or to validate system performance as LangSmithEventHandler.
TYPE:
|
view_selector |
View selector used by the collection to select the best view for the given query. If None, a new instance of LLMViewSelector will be used.
TYPE:
|
nl_responder |
NL responder used by the collection to respond to natural language queries. If None, a new instance of NLResponder will be used.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Collection
|
a new instance of db-ally Collection |
RAISES | DESCRIPTION |
---|---|
ValueError
|
if default LLM client is not configured |
Source code in src/dbally/_main.py
dbally.Collection
Collection(name: str, view_selector: ViewSelector, llm_client: LLMClient, event_handlers: List[EventHandler], nl_responder: NLResponder, n_retries: int = 3)
Collection is a container for a set of views that can be used by db-ally to answer user questions.
Tip
It is recommended to create new collections using the dbally.create_colletion
function instead of instantiating this class directly.
PARAMETER | DESCRIPTION |
---|---|
name |
Name of the collection is available for Event handlers and is used to distinguish different db-ally runs.
TYPE:
|
view_selector |
As you register more then one View within single collection, before generating the IQL query, a View that fits query the most is selected by the ViewSelector.
TYPE:
|
llm_client |
LLM client used by the collection to generate views and respond to natural language queries.
TYPE:
|
event_handlers |
Event handlers used by the collection during query executions. Can be used to log events as CLIEventHandler or to validate system performance as LangSmithEventHandler.
TYPE:
|
nl_responder |
Object that translates RAW response from db-ally into natural language.
TYPE:
|
n_retries |
IQL generator may produce invalid IQL. If this is the case this argument specifies how many times db-ally will try to regenerate it. Previous try with the error message is appended to the chat history to guide next generations.
TYPE:
|
Source code in src/dbally/collection.py
add
Register new View that will be available to query via the collection.
PARAMETER | DESCRIPTION |
---|---|
view |
A class inherithing from BaseView. Object of this type will be initialized during query execution. We expect Class instead of object, as otherwise Views must have been implemented stateless, which would be cumbersome.
TYPE:
|
builder |
Optional factory function that will be used to create the View instance. Use it when you need to pass outcome of API call or database connection to the view and it can change over time.
TYPE:
|
name |
Custom name of the view (defaults to the name of the class).
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
if view with the given name is already registered or views class possess some non-default arguments. |
Example of custom builder
usage
def build_dogs_df_view():
dogs_df = request.get("https://dog.ceo/api/breeds/list")
return DogsDFView(dogs_df)
collection.add(DogsDFView, build_dogs_df_view)
Source code in src/dbally/collection.py
get
get(name: str) -> BaseView
Returns an instance of the view with the given name
PARAMETER | DESCRIPTION |
---|---|
name |
Name of the view to return
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
BaseView
|
View instance |
RAISES | DESCRIPTION |
---|---|
NoViewFoundError
|
If there is no view with the given name |
Source code in src/dbally/collection.py
list
Lists all registered view names and their descriptions
RETURNS | DESCRIPTION |
---|---|
Dict[str, str]
|
Dictionary of view names and descriptions |
Source code in src/dbally/collection.py
ask
async
ask(question: str, dry_run: bool = False, return_natural_response: bool = False) -> ExecutionResult
Ask question in a text form and retrieve the answer based on the available views.
Question answering is composed of following steps
- View Selection
- IQL Generation
- IQL Parsing
- Query Building
- Query Execution
PARAMETER | DESCRIPTION |
---|---|
question |
question posed using natural language representation e.g "What job offers for Data Scientists do we have?"
TYPE:
|
dry_run |
if True, only generate the query without executing it
TYPE:
|
return_natural_response |
if True (and dry_run is False as natural response requires query results), the natural response will be included in the answer
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ExecutionResult
|
ExecutionResult object representing the result of the query execution. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
if collection is empty |
IQLError
|
if incorrect IQL was generated |
ValueError
|
if incorrect IQL was generated |
Source code in src/dbally/collection.py
get_similarity_indexes
List all similarity indexes from all structured views in the collection.
RETURNS | DESCRIPTION |
---|---|
Dict[AbstractSimilarityIndex, List[Tuple[str, str, str]]]
|
Dictionary with similarity indexes as keys and values containing lists of places where they are used |
Dict[AbstractSimilarityIndex, List[Tuple[str, str, str]]]
|
(represented by a tupple containing view name, method name and argument name) |
Source code in src/dbally/collection.py
update_similarity_indexes
async
Update all similarity indexes from all structured views in the collection.
RAISES | DESCRIPTION |
---|---|
IndexUpdateError
|
if updating any of the indexes fails. The exception provides |
Source code in src/dbally/collection.py
dbally.data_models.execution_result.ExecutionResult
dataclass
ExecutionResult(results: List[Dict[str, Any]], context: Dict[str, Any], execution_time: float, execution_time_view: float, view_name: str, textual_response: Optional[str] = None)
Represents the collection-level result of the query execution.
PARAMETER | DESCRIPTION |
---|---|
results |
List of dictionaries containing the results of the query execution,
each dictionary represents a row in the result set with column names as keys.
The exact structure of the result set depends on the view that was used to execute the query,
which can be obtained from the
TYPE:
|
context |
Dictionary containing addtional metadata about the query execution.
TYPE:
|
execution_time |
Time taken to execute the entire query, including view selection and all other operations, in seconds.
TYPE:
|
execution_time_view |
Time taken that the selected view took to execute the query, in seconds.
TYPE:
|
view_name |
Name of the view that was used to execute the query.
TYPE:
|
textual_response |
Optional text response that can be used to display the query results in a human-readable format.
TYPE:
|
dbally.collection.IndexUpdateError
Bases: Exception
Exception for when updating any of the Collection's similarity indexes fails.
Provides a dictionary mapping failed indexes to their
respective exceptions as the failed_indexes
attribute.
PARAMETER | DESCRIPTION |
---|---|
failed_indexes |
Dictionary mapping failed indexes to their respective exceptions.
TYPE:
|