vec_txt_table.py 1.77 KB
Newer Older
1
from .k_db import PostgresDB
陈正乐 committed
2

3 4 5 6 7 8 9
TABLE_VEC_TXT = """
CREATE TABLE vec_txt (
    vector_id varchar(36) PRIMARY KEY, 
    text text, 
    paragraph_id varchar(40) not null
)
"""
陈正乐 committed
10 11 12


# 025a9bee-2eb2-47f5-9722-525e05a0442b
13 14 15 16 17 18 19 20 21
class TxtVector:
    def __init__(self, db: PostgresDB) -> None:
        self.db = db

    def insert(self, vectors):
        query = f"INSERT INTO vec_txt(vector_id,text,paragraph_id) VALUES"
        args = []
        for value in vectors:
            value = list(value)
陈正乐 committed
22
            query += "(%s,%s,%s),"
23
            args.extend(value)
陈正乐 committed
24
        query = query[:len(query) - 1]
25 26
        query += f"ON conflict(vector_id) DO UPDATE SET text = EXCLUDED.text,paragraph_id = EXCLUDED.paragraph_id;"
        # query += ";"
陈正乐 committed
27 28 29 30 31
        self.db.execute_args(query, args)

    def delete(self, ids):
        for item in ids:
            query = f"delete FROM vec_txt WHERE vector_id = '%s'" % (item,)
32
            self.db.execute(query)
陈正乐 committed
33

34 35
    def search(self, search: str):
        query = f"SELECT paragraph_id,text FROM vec_txt WHERE vector_id = %s"
陈正乐 committed
36
        self.db.execute_args(query, [search])
37
        answer = self.db.fetchall()
38
        # print(answer)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        return answer[0]

    def create_table(self):
        query = f"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'vec_txt')"
        self.db.execute(query)
        exists = self.db.fetchall()[0][0]
        if not exists:
            query = TABLE_VEC_TXT
            self.db.execute(query)

    def drop_table(self):
        query = f"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'vec_txt')"
        self.db.execute(query)
        exists = self.db.fetchall()[0][0]
        if exists:
            query = "DROP TABLE vec_txt"
            self.db.format(query)
陈正乐 committed
56
            print("drop table vec_txt ok")