-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchdocs.py
More file actions
34 lines (25 loc) · 1.11 KB
/
searchdocs.py
File metadata and controls
34 lines (25 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from fastembed import TextEmbedding
from sqlalchemy import select
from sqlalchemy.orm import Session
from dbmodel import DocumentChunk
import dbconnect
model = TextEmbedding() # using BAAI/bge-small-en-v1.5
def search(query: str) -> dict:
"""
Search for relevant document chunks based on a query.
This function takes a search query, computes its embedding using the global
``model``, and then performs a similarity search against the document chunks
stored in the database. The search results are returned as a dictionary
containing the most relevant chunks along with their metadata.
Args:
query (str): The search query string.
Returns:
dict: A dictionary containing the search results, which includes the
relevant document chunks and their associated metadata.
Raises:
Exception: Propagates any exceptions that occur during embedding or database
operations so callers can handle or log them as needed.
"""
query_embedding = list(model.embed(query))[0]
results = dbconnect.search_documents(query_embedding);
return results