Source code for haive.core.schema.prebuilt.rag_state
"""RAG (Retrieval-Augmented Generation) state schema for haive agents."""fromtypingimportAnyfromlangchain_core.documentsimportDocumentfrompydanticimportFieldfromhaive.core.schema.prebuilt.messages_stateimportMessagesState
[docs]classRAGState(MessagesState):"""State schema for RAG (Retrieval-Augmented Generation) workflows. This schema extends MessagesState with fields specific to RAG operations: - Document retrieval and storage - Query/question tracking - Context management - Retrieved document scoring and metadata """# Query/Question fieldsquery:str=Field(default="",description="Current query/question being processed")original_query:str|None=Field(default=None,description="Original query before any transformations")# Document fieldsdocuments:list[Document]=Field(default_factory=list,description="Retrieved documents for the current query")# Context fieldscontext:str=Field(default="",description="Formatted context from retrieved documents")# Retrieval metadataretrieval_metadata:dict[str,Any]=Field(default_factory=dict,description="Metadata about the retrieval process (scores, sources, etc.)",)# Answer/Response fieldsanswer:str|None=Field(default=None,description="Generated answer based on retrieved context")# Optional fields for advanced RAG workflowshypothetical_document:str|None=Field(default=None,description="Hypothetical document for HyDE RAG workflows")reranked_documents:list[Document]|None=Field(default=None,description="Documents after reranking process")# Shared fields for multi-agent RAG__shared_fields__=["messages","query","documents","context","answer"]
[docs]defformat_documents_as_context(self,separator:str="\n\n")->str:"""Format documents into a context string."""ifnotself.documents:return""context_parts=[]fori,docinenumerate(self.documents):# Include metadata if availablesource=doc.metadata.get("source",f"Document {i+1}")context_parts.append(f"[Source: {source}]\n{doc.page_content}")self.context=separator.join(context_parts)returnself.context
[docs]defadd_document(self,document:Document)->None:"""Add a document to the retrieved documents list."""self.documents.append(document)
[docs]defclear_documents(self)->None:"""Clear all retrieved documents."""self.documents.clear()self.context=""self.retrieval_metadata.clear()
[docs]defget_top_documents(self,k:int=5)->list[Document]:"""Get the top k documents based on retrieval scores."""# If we have reranked documents, use thosedocs=self.reranked_documentsifself.reranked_documentselseself.documentsreturndocs[:k]