[docs]classDriaAPIWrapper:"""Wrapper around Dria API. This wrapper facilitates interactions with Dria's vector search and retrieval services, including creating knowledge bases, inserting data, and fetching search results. Attributes: api_key: Your API key for accessing Dria. contract_id: The contract ID of the knowledge base to interact with. top_n: Number of top results to fetch for a search. """
[docs]def__init__(self,api_key:str,contract_id:Optional[str]=None,top_n:int=10):try:fromdriaimportDria,ModelsexceptImportError:logger.error("""Dria is not installed. Please install Dria to use this wrapper. You can install Dria using the following command: pip install dria """)returnself.api_key=api_keyself.models=Modelsself.contract_id=contract_idself.top_n=top_nself.dria_client=Dria(api_key=self.api_key)ifself.contract_id:self.dria_client.set_contract(self.contract_id)
[docs]defcreate_knowledge_base(self,name:str,description:str,category:str,embedding:str,)->str:"""Create a new knowledge base."""contract_id=self.dria_client.create(name=name,embedding=embedding,category=category,description=description)logger.info(f"Knowledge base created with ID: {contract_id}")self.contract_id=contract_idreturncontract_id
[docs]definsert_data(self,data:List[Dict[str,Any]])->str:"""Insert data into the knowledge base."""response=self.dria_client.insert_text(data)logger.info(f"Data inserted: {response}")returnresponse
[docs]defsearch(self,query:str)->List[Dict[str,Any]]:"""Perform a text-based search."""results=self.dria_client.search(query,top_n=self.top_n)logger.info(f"Search results: {results}")returnresults
[docs]defquery_with_vector(self,vector:List[float])->List[Dict[str,Any]]:"""Perform a vector-based query."""vector_query_results=self.dria_client.query(vector,top_n=self.top_n)logger.info(f"Vector query results: {vector_query_results}")returnvector_query_results
[docs]defrun(self,query:Union[str,List[float]])->Optional[List[Dict[str,Any]]]:"""Method to handle both text-based searches and vector-based queries. Args: query: A string for text-based search or a list of floats for vector-based query. Returns: The search or query results from Dria. """ifisinstance(query,str):returnself.search(query)elifisinstance(query,list)andall(isinstance(item,float)foriteminquery):returnself.query_with_vector(query)else:logger.error("""Invalid query type. Please provide a string for text search or a list of floats for vector query.""")returnNone