File size: 7,764 Bytes
04f0bde
 
 
c6f6149
04f0bde
 
 
 
 
 
c6f6149
04f0bde
 
 
 
c6f6149
04f0bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f6149
 
 
04f0bde
 
 
 
 
 
 
 
 
 
c6f6149
04f0bde
 
 
 
 
 
0e573d0
 
04f0bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f6149
04f0bde
 
c6f6149
04f0bde
 
 
 
 
 
 
 
 
 
 
c6f6149
 
 
 
 
 
 
 
 
 
 
 
04f0bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f6149
 
04f0bde
 
 
 
c6f6149
04f0bde
 
c6f6149
04f0bde
c6f6149
 
 
 
04f0bde
 
 
 
c6f6149
04f0bde
c6f6149
04f0bde
 
c6f6149
04f0bde
 
 
 
 
 
c6f6149
04f0bde
 
c6f6149
 
 
04f0bde
c6f6149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04f0bde
c6f6149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import pandas as pd
import hashlib
import requests
from typing import List, Optional
from datetime import datetime
from langchain.schema.embeddings import Embeddings
from streamlit.runtime.uploaded_file_manager import UploadedFile
from clickhouse_connect import get_client
from multiprocessing.pool import ThreadPool
from langchain.vectorstores.myscale import MyScaleWithoutJSON, MyScaleSettings
from .helper import create_retriever_tool

parser_url = "https://api.unstructured.io/general/v0/general"


def parse_files(api_key, user_id, files: List[UploadedFile]):
    def parse_file(file: UploadedFile):
        headers = {
            "accept": "application/json",
            "unstructured-api-key": api_key,
        }
        data = {"strategy": "auto", "ocr_languages": ["eng"]}
        file_hash = hashlib.sha256(file.read()).hexdigest()
        file_data = {"files": (file.name, file.getvalue(), file.type)}
        response = requests.post(
            parser_url, headers=headers, data=data, files=file_data
        )
        json_response = response.json()
        if response.status_code != 200:
            raise ValueError(str(json_response))
        texts = [
            {
                "text": t["text"],
                "file_name": t["metadata"]["filename"],
                "entity_id": hashlib.sha256(
                    (file_hash + t["text"]).encode()
                ).hexdigest(),
                "user_id": user_id,
                "created_by": datetime.now(),
            }
            for t in json_response
            if t["type"] == "NarrativeText" and len(t["text"].split(" ")) > 10
        ]
        return texts

    with ThreadPool(8) as p:
        rows = []
        for r in p.imap_unordered(parse_file, files):
            rows.extend(r)
        return rows


def extract_embedding(embeddings: Embeddings, texts):
    if len(texts) > 0:
        embs = embeddings.embed_documents(
            [t["text"] for _, t in enumerate(texts)])
        for i, _ in enumerate(texts):
            texts[i]["vector"] = embs[i]
        return texts
    raise ValueError("No texts extracted!")


class PrivateKnowledgeBase:
    def __init__(
        self,
        host,
        port,
        username,
        password,
        embedding: Embeddings,
        parser_api_key,
        db="chat",
        kb_table="private_kb",
        tool_table="private_tool",
    ) -> None:
        super().__init__()
        kb_schema_ = f"""
            CREATE TABLE IF NOT EXISTS {db}.{kb_table}(
                entity_id String,
                file_name String,
                text String,
                user_id String,
                created_by DateTime,
                vector Array(Float32),
                CONSTRAINT cons_vec_len CHECK length(vector) = 768,
                VECTOR INDEX vidx vector TYPE MSTG('metric_type=Cosine')
            ) ENGINE = ReplacingMergeTree ORDER BY entity_id
        """
        tool_schema_ = f"""
            CREATE TABLE IF NOT EXISTS {db}.{tool_table}(
                tool_id String,
                tool_name String,
                file_names Array(String),
                user_id String,
                created_by DateTime,
                tool_description String
            ) ENGINE = ReplacingMergeTree ORDER BY tool_id
        """
        self.kb_table = kb_table
        self.tool_table = tool_table
        config = MyScaleSettings(
            host=host,
            port=port,
            username=username,
            password=password,
            database=db,
            table=kb_table,
        )
        client = get_client(
            host=config.host,
            port=config.port,
            username=config.username,
            password=config.password,
        )
        client.command("SET allow_experimental_object_type=1")
        client.command(kb_schema_)
        client.command(tool_schema_)
        self.parser_api_key = parser_api_key
        self.vstore = MyScaleWithoutJSON(
            embedding=embedding,
            config=config,
            must_have_cols=["file_name", "text", "created_by"],
        )

    def list_files(self, user_id, tool_name=None):
        query = f"""
        SELECT DISTINCT file_name, COUNT(entity_id) AS num_paragraph, 
            arrayMax(arrayMap(x->length(x), groupArray(text))) AS max_chars
        FROM {self.vstore.config.database}.{self.kb_table}
        WHERE user_id = '{user_id}' GROUP BY file_name
        """
        return [r for r in self.vstore.client.query(query).named_results()]

    def add_by_file(
        self, user_id, files: List[UploadedFile], **kwargs
    ):
        data = parse_files(self.parser_api_key, user_id, files)
        data = extract_embedding(self.vstore.embeddings, data)
        self.vstore.client.insert_df(
            self.kb_table,
            pd.DataFrame(data),
            database=self.vstore.config.database,
        )

    def clear(self, user_id):
        self.vstore.client.command(
            f"DELETE FROM {self.vstore.config.database}.{self.kb_table} "
            f"WHERE user_id='{user_id}'"
        )
        query = f"""DELETE FROM {self.vstore.config.database}.{self.tool_table} 
                    WHERE user_id  = '{user_id}'"""
        self.vstore.client.command(query)

    def create_tool(
        self, user_id, tool_name, tool_description, files: Optional[List[str]] = None
    ):
        self.vstore.client.insert_df(
            self.tool_table,
            pd.DataFrame(
                [
                    {
                        "tool_id": hashlib.sha256(
                            (user_id + tool_name).encode("utf-8")
                        ).hexdigest(),
                        "tool_name": tool_name,
                        "file_names": files,
                        "user_id": user_id,
                        "created_by": datetime.now(),
                        "tool_description": tool_description,
                    }
                ]
            ),
            database=self.vstore.config.database,
        )

    def list_tools(self, user_id, tool_name=None):
        extended_where = f"AND tool_name = '{tool_name}'" if tool_name else ""
        query = f"""
        SELECT tool_name, tool_description, length(file_names) 
        FROM {self.vstore.config.database}.{self.tool_table}
        WHERE user_id = '{user_id}' {extended_where}
        """
        return [r for r in self.vstore.client.query(query).named_results()]

    def remove_tools(self, user_id, tool_names):
        tool_names = ",".join([f"'{t}'" for t in tool_names])
        query = f"""DELETE FROM {self.vstore.config.database}.{self.tool_table}
                    WHERE user_id  = '{user_id}' AND tool_name IN [{tool_names}]"""
        self.vstore.client.command(query)

    def as_tools(self, user_id, tool_name=None):
        tools = self.list_tools(user_id=user_id, tool_name=tool_name)
        retrievers = {
            t["tool_name"]: create_retriever_tool(
                self.vstore.as_retriever(
                    search_kwargs={
                        "where_str": (
                            f"user_id='{user_id}' "
                            f"""AND file_name IN (
                                SELECT arrayJoin(file_names) FROM (
                                    SELECT file_names 
                                    FROM {self.vstore.config.database}.{self.tool_table}
                                    WHERE user_id = '{user_id}' AND tool_name = '{t['tool_name']}')
                        )"""
                        )
                    },
                ),
                name=t["tool_name"],
                description=t["tool_description"],
            )
            for t in tools
        }
        return retrievers