KB Search
lean_automator.kb.search
¶
Generates embeddings and performs semantic search in the Knowledge Base.
This module provides functions for generating text embeddings using a Gemini client and performing semantic search within the Knowledge Base by comparing vector similarity (cosine similarity) between a query embedding and stored embeddings.
Classes¶
Functions¶
generate_embedding(text: str, task_type: str, client: GeminiClient) -> Optional[np.ndarray]
async
¶
Generates an embedding for the given text.
Uses the configured Gemini client to create a vector representation of the input text suitable for the specified task type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text content to embed. |
required |
task_type
|
str
|
The task type for the embedding (e.g., "RETRIEVAL_DOCUMENT", "RETRIEVAL_QUERY"). |
required |
client
|
GeminiClient
|
An initialized GeminiClient instance. |
required |
Returns:
Type | Description |
---|---|
Optional[ndarray]
|
Optional[np.ndarray]: A numpy array representing the embedding vector, |
Optional[ndarray]
|
or None if generation fails or the client is unavailable. |
Source code in lean_automator/kb/search.py
find_similar_items(query_text: str, search_field: str, client: GeminiClient, *, task_type_query: str = 'RETRIEVAL_QUERY', db_path: Optional[str] = None, top_n: int = 5) -> List[Tuple[KBItem, float]]
async
¶
Finds KBItems with embeddings similar to the query text.
Generates an embedding for the query text and performs a brute-force cosine similarity search across all items in the database that have a pre-computed embedding for the specified field ('nl' or 'latex').
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_text
|
str
|
The natural language query. |
required |
search_field
|
str
|
Which embedding field to search against ('nl' or 'latex'). |
required |
client
|
GeminiClient
|
An initialized GeminiClient instance used for generating the query embedding. |
required |
task_type_query
|
str
|
The task type for embedding the query. Defaults to "RETRIEVAL_QUERY". |
'RETRIEVAL_QUERY'
|
db_path
|
Optional[str]
|
Path to the database file. If None, uses DEFAULT_DB_PATH. Defaults to None. |
None
|
top_n
|
int
|
The maximum number of similar items to return. Defaults to 5. |
5
|
Returns:
Type | Description |
---|---|
List[Tuple[KBItem, float]]
|
List[Tuple[KBItem, float]]: A list of tuples, each containing a |
List[Tuple[KBItem, float]]
|
matching KBItem object and its similarity score (float between -1 and 1). |
List[Tuple[KBItem, float]]
|
The list is sorted by similarity score in descending order. Returns an |
List[Tuple[KBItem, float]]
|
empty list if the client is unavailable, embedding generation fails, |
List[Tuple[KBItem, float]]
|
database access fails, no items have embeddings, or no matches are found. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in lean_automator/kb/search.py
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|