Retrievers¶
See Retrievers for how to build and fill one.
Retriever¶
The abstract interface the classifiers are typed against. Implement these four methods to substitute a different vector store or search service.
rac4os._vector_database.Retriever.Retriever
¶
Bases: ABC
Abstract class that integrates with all rac4os classifiers that build on retrieval. If you want to use a custom retrieval approach that is not supported
by rac4os use this class and implement your custom logic here. If the approach does not need to build a custom, vector database just do not implement the
related methods, just implement the search_category method. This is the only method that is required for the retrieval based classification approaches.
Source code in rac4os/_vector_database/Retriever.py
__init__
abstractmethod
¶
Set up whatever the backend needs to answer search_category.
Nothing in the package calls this with a fixed argument list, so an implementation is free to take whatever its backend needs: a path and a collection name for a local store, a URL and a key for a hosted search service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
whatever your implementation requires. |
{}
|
Source code in rac4os/_vector_database/Retriever.py
create_collection
abstractmethod
¶
Write a batch of documents into the backend.
Only needed when your retriever builds its own index. A wrapper around an existing search service can leave this unimplemented.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
list[str]
|
unique id per document. |
required |
documents
|
list[str]
|
the texts that get embedded and searched over. |
required |
metadatas
|
list[dict]
|
one metadata dict per document, carrying everything about the code that is not the searched text. |
required |
Source code in rac4os/_vector_database/Retriever.py
index
abstractmethod
¶
Fill the retriever from a classification system.
Only needed when your retriever builds its own index. This is the method that decides what a category's searchable text is: the choice of what to embed, and what to keep alongside as metadata, is the single biggest lever on retrieval quality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_system
|
ClassificationSystem
|
the taxonomy to index. |
required |
codes_to_include
|
list[str] | None
|
restrict indexing to these codes; None indexes every code in the system. |
None
|
**kwargs
|
Any
|
implementation-specific options. |
{}
|
Source code in rac4os/_vector_database/Retriever.py
search_category
abstractmethod
¶
Return the k categories closest to a query.
This is the only method the retrieval-based classifiers actually call, so it is the only one you have to implement. Each returned candidate is serialised into the generator's prompt as it comes back from here, which means the shape you return is the shape the model reasons over.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
the text to search for. |
required |
k
|
int
|
how many candidates to return. |
required |
where
|
dict | None
|
optional metadata filter, e.g. restricting results to one level or one branch. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
k candidates, each carrying at least the code and the text that was matched. |
Source code in rac4os/_vector_database/Retriever.py
VectorStore¶
rac4os._vector_database.vector_store.VectorStore
¶
Bases: Retriever
Interface for filling and querying the chromaDB (persistent) client. Builds a chromaDB collection from a predefined ClassificationSystem
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_name
|
str
|
name of the collection to create/load |
required |
chromadb_path
|
str
|
path of the persistent client to load or create |
required |
embedding_function_type
|
str
|
which backend embeds documents and queries - "openai", "ollama" or "sentence_transformers". |
required |
embedding_batch_size
|
int
|
how many documents go per |
16
|
**embedding_kwargs
|
Any
|
forwarded untouched to the chosen embedding
function, e.g. |
{}
|
Source code in rac4os/_vector_database/vector_store.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
__init__
¶
__init__(collection_name, chromadb_path, embedding_function_type, embedding_batch_size=16, **embedding_kwargs)
Opens the persistent chromaDB client and gets or creates the collection.
The embedding function is built here from embedding_function_type and
embedding_kwargs and attached to the collection, so queries are embedded with the
same model that indexed it. The collection is created with cosine distance.
Nothing is indexed by constructing a store: an existing collection is reused as it
stands, and a new one starts empty until index is called. This is why pointing at the
wrong collection_name shows up as an empty candidate list rather than as an error.
Source code in rac4os/_vector_database/vector_store.py
create_collection
¶
Fills the chromaDB clients collection with the contents, that will be searchable afterwards.
Paramters
ids (list[str]): ids of the given tuple documents (list[str]): documents to be indexed/vectorised metadatas (list[dict[str, Any]]): metadata of the documents to be inserted into the db
Source code in rac4os/_vector_database/vector_store.py
index
¶
Interface for indexing a ClassificationSystem. Takes a ClassificationSystem object and inserts it into
a chromaDB collection of the persistent client. All the codes of the given classification system will be index,
based on their description, which will serve as documents, all other information associated with code, will be
stored in the metadata of the tuples. If you only want to index a certain subset of the classification system use
codes_to_include, and insert a list of codes to index. One thing is important to notice at this point. Because chromaDB
does not allow, metadata entries themselves to be dictionaries, all dicts will be dumped to a json-string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classification_system
|
ClassificationSystem
|
classification system to be index in the vector store |
required |
codes_to_include
|
list[str] | None
|
list of codes to be indexed, when this paramter is left blank all entries of the classification system will indexed. |
None
|
**kwargs
|
Any
|
additional key word args, at this points they will be ignored. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in rac4os/_vector_database/vector_store.py
search_category
¶
Function for searching the indexed chromaDB collection of the classification system.
Paramters
query (str): query to search the collection k (int): number of DB entries to be returned where (dict[str,Any]|None): optional metadata filter which can be used to set contraints to the returned results. See chromaDB documentation for references.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list of k codes that are the result of the similiarity search. |
Source code in rac4os/_vector_database/vector_store.py
Embedding functions¶
rac4os._vector_database.vector_store.get_ef
¶
Takes an embedding function type, maps the embedding function type to the correct interface and returns the loaded ef interface, given the defined kwargs.
Paramters
embedding_function_type (Literal["openai", "ollama", "sentence_transformers"]): tpye of embedding function to use. **kwargs (Any): arguments used to specify what model and paramters to use when loading the embedding function. Must correspond to what the chromaDB embedding function takes as input
Returns:
| Type | Description |
|---|---|
EmbeddingFunction
|
embedding function |