# -*- coding: utf-8 -*-"""A general implementation of the knowledge class in AgentScope RAG module."""fromtypingimportAnyfrom._readerimportDocumentfrom..messageimportTextBlockfrom._knowledge_baseimportKnowledgeBase
[docs]classSimpleKnowledge(KnowledgeBase):"""A simple knowledge base implementation."""
[docs]asyncdefretrieve(self,query:str,limit:int=5,score_threshold:float|None=None,**kwargs:Any,)->list[Document]:"""Retrieve relevant documents by the given queries. Args: query (`str`): The query string to retrieve relevant documents. limit (`int`, defaults to 5): The number of relevant documents to retrieve. score_threshold: float | None = None, The threshold of the score to filter the results. **kwargs (`Any`): Other keyword arguments for the vector database search API. Returns: `list[Document]`: A list of relevant documents. TODO: handle the case when the query is too long. """res_embedding=awaitself.embedding_model([TextBlock(type="text",text=query,),],)res=awaitself.embedding_store.search(res_embedding.embeddings[0],limit=limit,score_threshold=score_threshold,**kwargs,)returnres
[docs]asyncdefadd_documents(self,documents:list[Document],**kwargs:Any,)->None:"""Add documents to the knowledge Args: documents (`list[Document]`): The list of documents to add. """# Prepare the content to be embeddedfordocindocuments:if(doc.metadata.content["type"]notinself.embedding_model.supported_modalities):raiseValueError(f"The embedding model {self.embedding_model.model_name} "f"does not support {doc.metadata.content['type']} data.",)# Get the embeddingsres_embeddings=awaitself.embedding_model([_.metadata.contentfor_indocuments],)fordoc,embeddinginzip(documents,res_embeddings.embeddings):doc.embedding=embeddingawaitself.embedding_store.add(documents)