| 70 | |
| 71 | |
| 72 | class SQLStorage(CacheStorage): |
| 73 | def __init__( |
| 74 | self, |
| 75 | db_type: str = "mysql", |
| 76 | config=None, |
| 77 | url="./sqlite.db" |
| 78 | ): |
| 79 | self._url = url |
| 80 | # self._engine = sqlite3.connect(url) |
| 81 | self.create() |
| 82 | |
| 83 | def create(self): |
| 84 | # answer_table_sql = """CREATE TABLE IF NOT EXISTS `modelcache_llm_answer` ( |
| 85 | # `id` bigint(20) NOT NULL AUTO_INCREMENT comment '主键', |
| 86 | # `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '创建时间', |
| 87 | # `gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间', |
| 88 | # `question` text NOT NULL comment 'question', |
| 89 | # `answer` text NOT NULL comment 'answer', |
| 90 | # `answer_type` int(11) NOT NULL comment 'answer_type', |
| 91 | # `hit_count` int(11) NOT NULL DEFAULT '0' comment 'hit_count', |
| 92 | # `model` varchar(1000) NOT NULL comment 'model', |
| 93 | # `embedding_data` blob NOT NULL comment 'embedding_data', |
| 94 | # PRIMARY KEY(`id`) |
| 95 | # ) AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT = 'modelcache_llm_answer'; |
| 96 | # """ |
| 97 | answer_table_sql = """CREATE TABLE IF NOT EXISTS modelcache_llm_answer ( |
| 98 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 99 | gmt_create TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 100 | gmt_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 101 | question TEXT NOT NULL, |
| 102 | answer TEXT NOT NULL, |
| 103 | answer_type INTEGER NOT NULL, |
| 104 | hit_count INTEGER NOT NULL DEFAULT 0, |
| 105 | model VARCHAR(1000) NOT NULL, |
| 106 | embedding_data BLOB NOT NULL |
| 107 | ); |
| 108 | """ |
| 109 | |
| 110 | # log_table_sql = """CREATE TABLE IF NOT EXISTS `modelcache_query_log` ( |
| 111 | # `id` bigint(20) NOT NULL AUTO_INCREMENT comment '主键', |
| 112 | # `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '创建时间', |
| 113 | # `gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间', |
| 114 | # `error_code` int(11) NOT NULL comment 'errorCode', |
| 115 | # `error_desc` varchar(1000) NOT NULL comment 'errorDesc', |
| 116 | # `cache_hit` varchar(100) NOT NULL comment 'cacheHit', |
| 117 | # `delta_time` float NOT NULL comment 'delta_time', |
| 118 | # `model` varchar(1000) NOT NULL comment 'model', |
| 119 | # `query` text NOT NULL comment 'query', |
| 120 | # `hit_query` text NOT NULL comment 'hitQuery', |
| 121 | # `answer` text NOT NULL comment 'answer', |
| 122 | # PRIMARY KEY(`id`) |
| 123 | # ) AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT = 'modelcache_query_log'; |
| 124 | # """ |
| 125 | log_table_sql = """CREATE TABLE IF NOT EXISTS modelcache_query_log ( |
| 126 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 127 | gmt_create TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 128 | gmt_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 129 | error_code INTEGER NOT NULL, |