File size: 3,351 Bytes
ed4d993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
from typing import Any, Dict, List, Optional, Union

logger = logging.getLogger(__name__)


class DriaAPIWrapper:
    """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.
    """

    def __init__(
        self, api_key: str, contract_id: Optional[str] = None, top_n: int = 10
    ):
        try:
            from dria import Dria, Models
        except ImportError:
            logger.error(
                """Dria is not installed. Please install Dria to use this wrapper.
                
                You can install Dria using the following command:
                pip install dria
                """
            )
            return

        self.api_key = api_key
        self.models = Models
        self.contract_id = contract_id
        self.top_n = top_n
        self.dria_client = Dria(api_key=self.api_key)
        if self.contract_id:
            self.dria_client.set_contract(self.contract_id)

    def create_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_id
        return contract_id

    def insert_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}")
        return response

    def search(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}")
        return results

    def query_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}")
        return vector_query_results

    def run(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.
        """
        if isinstance(query, str):
            return self.search(query)
        elif isinstance(query, list) and all(isinstance(item, float) for item in query):
            return self.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."""
            )
            return None