Collection#
Tip
To understand the general idea better, visit the Collection concept page.
dbally.collection.Collection
#
Collection(name: str, view_selector: ViewSelector, llm: LLM, nl_responder: NLResponder, event_handlers: Optional[List[EventHandler]] = None, n_retries: int = 3, fallback_collection: Optional[Collection] = None)
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_collection
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 than one View within single collection, before generating the IQL query, a View that fits query the most is selected by the ViewSelector.
TYPE:
|
llm |
LLM used by the collection to generate views and respond to natural language queries.
TYPE:
|
nl_responder |
Object that translates RAW response from db-ally into natural language.
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:
|
fallback_collection |
collection to be asked when the ask function could not find answer in views registered
TYPE:
|
Source code in src/dbally/collection/collection.py
add
#
Register new View that will be available to query via the collection.
PARAMETER | DESCRIPTION |
---|---|
view |
A class inheriting 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/collection.py
set_fallback
#
set_fallback(fallback_collection: Collection) -> Collection
Set fallback collection which will be asked if the ask to base collection does not succeed.
PARAMETER | DESCRIPTION |
---|---|
fallback_collection |
Collection to be asked in case of base collection failure.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Collection
|
The fallback collection to create chains call |
Source code in src/dbally/collection/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/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/collection.py
get_all_event_handlers
#
get_all_event_handlers() -> List[EventHandler]
Retrieves all event handlers, including those from a fallback collection if available.
This method returns a list of event handlers. If there is no fallback collection, it simply returns the event handlers stored in the current object. If a fallback collection is available, it combines the event handlers from both the current object and the fallback collection, ensuring no duplicates.
RETURNS | DESCRIPTION |
---|---|
List[EventHandler]
|
A list of event handlers. |
Source code in src/dbally/collection/collection.py
ask
async
#
ask(question: str, dry_run: bool = False, return_natural_response: bool = False, llm_options: Optional[LLMOptions] = None, event_tracker: Optional[EventTracker] = None) -> 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:
|
llm_options |
options to use for the LLM client. If provided, these options will be merged with the default options provided to the LLM client, prioritizing option values other than NOT_GIVEN
TYPE:
|
event_tracker |
Event tracker object for given ask.
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 |
NoViewFoundError
|
if question does not match to any registered view, |
UnsupportedQueryError
|
if the question could not be answered |
IndexUpdateError
|
if index update failed |
Source code in src/dbally/collection/collection.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
get_similarity_indexes
#
List all similarity indexes from all views in the collection.
RETURNS | DESCRIPTION |
---|---|
Dict[AbstractSimilarityIndex, List[IndexLocation]]
|
Mapping of similarity indexes to their locations, following view format. |
For
|
TYPE:
|
Source code in src/dbally/collection/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/collection.py
dbally.collection.results.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.exceptions.IndexUpdateError
#
IndexUpdateError(failed_indexes: Dict[AbstractSimilarityIndex, Exception], failed_locations: List[IndexLocation])
Bases: DbAllyError
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:
|
failed_locations |
List of locations of failed indexes.
TYPE:
|
Source code in src/dbally/collection/exceptions.py
dbally.collection.exceptions.NoViewFoundError
#
Bases: DbAllyError
Error raised when there is no view with the given name.
PARAMETER | DESCRIPTION |
---|---|
view_name |
Name of the view that was not found.
TYPE:
|