rag#
- class besser.agent.nlp.rag.rag.RAG(agent, vector_store, splitter, llm_name, llm_prompt=None, k=4, num_previous_messages=0)[source]#
Bases:
object
A Retrieval Augmented Generation (RAG) implementation.
A vector stores contains vectorized representations (i.e., embeddings) of chunks of data (text). For a given input query, a retriever gets the k most similar stored embeddings to the input embedding.
This is usually used to, given a query or question, retrieve those chunks that could be helpful to give it an answer. Then, an LLM is in charge of generating that answer, given the original query and the retrieved data as context.
- Parameters:
agent (Agent) β the agent the RAG engine belongs to
vector_store (langchain_core.vectorstores.base.VectorStore) β the vector store of the RAG engine
splitter (langchain_text_splitters.base.TextSplitter) β the text splitter of the RAG engine
llm_name (str) β the name of the LLM of the RAG engine. It must have been previously created and assigned to the agent
llm_prompt (str) β the prompt containing the detailed instructions for the answer generation by the LLM. If none is provided, the
default prompt
will be usedk (int) β number of chunks to retrieve from the vector store
num_previous_messages (int) β number of previous messages of the conversation to add to the LLM prompt context. Necessary a connection to
MonitoringDB
.
- _nlp_engine#
the NLPEngine that handles the NLP processes of the agent the RAG engine belongs to
- Type:
- vector_store#
the vector store of the RAG engine
- splitter#
the text splitter of the RAG engine
- llm_name#
the name of the LLM of the RAG engine. It must have been previously created and assigned to the agent
- Type:
- llm_prompt#
the prompt containing the detailed instructions for the answer generation by the LLM. If none is provided, the
default prompt
will be used- Type:
- num_previous_messages#
number of previous messages of the conversation to add to the LLM prompt context. Necessary a connection to
MonitoringDB
.- Type:
- DEFAULT_LLM_PROMPT = "You are an assistant for question-answering tasks. Based on the previous messages in the conversation (if provided), and additional context retrieved from a database (if provided), answer the user question. If you don't know the answer, just say that you don't know. Note that if the question refers to a previous message, you may have to ignore the context since it is retrieved from the database based only on the question (the retrieval does not take into account the previous messages). Use three sentences maximum and keep the answer concise"#
- create_prompt(history, docs, question, llm_prompt=None)[source]#
Creates the prompt for the LLM answer generation.
- Parameters:
docs (list[langchain_core.documents.base.Document]) β the retrieved documents to use as context in the prompt
question (str) β the user question
llm_prompt (str) β the prompt containing the detailed instructions for the answer generation by the LLM. If none is provided, the RAGβs default value will be used
- Returns:
the LLM prompt
- Return type:
- run(message, session=None, llm_prompt=None, llm_name=None, k=None, num_previous_messages=None)[source]#
Run the RAG engine.
- Parameters:
session (Session) β the session of the user that started this request. Must be provided if the chat history wants to be added as context to the LLM prompt.
message (str) β the message to be used as RAG query
llm_prompt (str) β the prompt containing the detailed instructions for the answer generation by the LLM. If none is provided, the RAGβs default value will be used
llm_name (str) β the name of the LLM to use. If none is provided, the RAGβs default value will be used
k (int) β the number of (top) documents to get. If none is provided, the RAGβs default value will be used
num_previous_messages (int) β number of previous messages of the conversation to add to the LLM prompt context. If none is provided, the RAGβs default value will be used. Necessary a connection to
MonitoringDB
.
- Returns:
the resulting RAG message
- Return type: