|
|
@ -1,11 +1,9 @@ |
|
|
|
from typing import List, Tuple |
|
|
|
|
|
|
|
# from deepsearcher.configuration import vector_db, embedding_model, llm |
|
|
|
from deepsearcher import configuration |
|
|
|
from deepsearcher.vector_db.base import RetrievalResult |
|
|
|
|
|
|
|
|
|
|
|
def query(original_query: str, max_iter: int = 3) -> Tuple[str, List[RetrievalResult]]: |
|
|
|
def query(original_query: str, max_iter: int = 3) -> tuple[str, list[RetrievalResult]]: |
|
|
|
""" |
|
|
|
Query the knowledge base with a question and get an answer. |
|
|
|
|
|
|
@ -27,7 +25,7 @@ def query(original_query: str, max_iter: int = 3) -> Tuple[str, List[RetrievalRe |
|
|
|
|
|
|
|
def retrieve( |
|
|
|
original_query: str, max_iter: int = 3 |
|
|
|
) -> Tuple[List[RetrievalResult], List[str]]: |
|
|
|
) -> tuple[list[RetrievalResult], list[str]]: |
|
|
|
""" |
|
|
|
Retrieve relevant information from the knowledge base without generating an answer. |
|
|
|
|
|
|
@ -48,47 +46,3 @@ def retrieve( |
|
|
|
original_query, max_iter=max_iter |
|
|
|
) |
|
|
|
return retrieved_results, [] |
|
|
|
|
|
|
|
|
|
|
|
def naive_retrieve(query: str, collection: str = None, top_k=10) -> List[RetrievalResult]: |
|
|
|
""" |
|
|
|
Perform a simple retrieval from the knowledge base using the naive RAG approach. |
|
|
|
|
|
|
|
This function uses the naive RAG agent to retrieve information from the knowledge base |
|
|
|
without any advanced techniques like iterative refinement. |
|
|
|
|
|
|
|
Args: |
|
|
|
query: The question or query to search for. |
|
|
|
collection: The name of the collection to search in. If None, searches in all collections. |
|
|
|
top_k: The maximum number of results to return. |
|
|
|
|
|
|
|
Returns: |
|
|
|
A list of retrieval results. |
|
|
|
""" |
|
|
|
naive_rag = configuration.naive_rag |
|
|
|
all_retrieved_results, _ = naive_rag.retrieve(query) |
|
|
|
return all_retrieved_results |
|
|
|
|
|
|
|
|
|
|
|
def naive_rag_query( |
|
|
|
query: str, collection: str = None, top_k=10 |
|
|
|
) -> Tuple[str, List[RetrievalResult]]: |
|
|
|
""" |
|
|
|
Query the knowledge base using the naive RAG approach and get an answer. |
|
|
|
|
|
|
|
This function uses the naive RAG agent to query the knowledge base and generate |
|
|
|
an answer based on the retrieved information, without any advanced techniques. |
|
|
|
|
|
|
|
Args: |
|
|
|
query: The question or query to search for. |
|
|
|
collection: The name of the collection to search in. If None, searches in all collections. |
|
|
|
top_k: The maximum number of results to consider. |
|
|
|
|
|
|
|
Returns: |
|
|
|
A tuple containing: |
|
|
|
- The generated answer as a string |
|
|
|
- A list of retrieval results that were used to generate the answer |
|
|
|
""" |
|
|
|
naive_rag = configuration.naive_rag |
|
|
|
answer, retrieved_results = naive_rag.query(query) |
|
|
|
return answer, retrieved_results |
|
|
|