RAG¶
- class rag.rag_processor.RAGProcessor(llm: BaseLLM, max_token: int, temperature: float, top_p: float, retriever, top_k: int)¶
Class implementing the RAG process
- Parameters:
llm (BaseLLM)
max_token (int)
temperature (float)
top_p (float)
retriever
top_k (int)
- init_retriever_client(retrieval_method: str = 'top_k')¶
Initialize and return a retriever client based on retrieval_method.
- Returns:
An instance of the appropriate retriever client based on retrieval_method.
- Return type:
object
- create_rag_message(context_docs: List[Any], query: str) List[Dict] ¶
Format the RAG message to send to the OpenAI API.
- Parameters:
context_docs (str) – Context matching the query according to the retrieval process
query (str) – User input question
- Returns:
Contains the message in the correct format to send to the OpenAI API
- Return type:
list of dict
- retrieve(db: Session, request: RAGRequest, language: str = None, tag: str = None, k: int = 0)¶
Retrieve context documents related to the user input question.
- Parameters:
db (Session) – Database session
request (RAGRequest) – User input question
language (str) – Question and context documents language
tag (str) – Tag to filter the context documents
k (int, default 0) – Number of context documents to return
- process(db: Session, request: RAGRequest, language: str = None, tag: str = None)¶
Process a RAGRequest to retrieve relevant documents and generate a response.
This method retrieves relevant documents from the database, constructs a context from the documents, and then uses an LLM client to generate a response based on the request query and the context.
- Parameters:
db (Session) – The database session to use for retrieving documents.
request (RAGRequest) – The request to process.
language (str, optional) – The language of the documents to retrieve. If not specified, documents in all languages are considered.
tag (str, optional) – The tag to filter the documents to retrieve. If not specified, all documents are considered.
- Returns:
The response generated by the LLM client.
- Return type:
str
- async embed(text_input: EmbeddingRequest)¶
Get the embedding of an embedding request.
- Parameters:
text_input (EmbeddingRequest)
- Returns:
The requested text embedding
- Return type:
dict