From 4cfa7bb40ad39bf4dcee6936bda9362af6ad0d75 Mon Sep 17 00:00:00 2001 From: "tuyang.yhj" Date: Tue, 4 Jul 2023 09:45:38 +0800 Subject: [PATCH] WEB API independent --- pilot/memory/chat_history/duckdb_history.py | 11 ++++++----- pilot/openapi/api_v1/api_v1.py | 7 +------ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pilot/memory/chat_history/duckdb_history.py b/pilot/memory/chat_history/duckdb_history.py index e3cf01efc..17bb5eccf 100644 --- a/pilot/memory/chat_history/duckdb_history.py +++ b/pilot/memory/chat_history/duckdb_history.py @@ -36,8 +36,9 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): if not result: # 如果表不存在,则创建新表 self.connect.execute( - "CREATE TABLE chat_history (conv_uid VARCHAR(100) PRIMARY KEY, chat_mode VARCHAR(50), summary VARCHAR(255), user_name VARCHAR(100), messages TEXT)" + "CREATE TABLE chat_history (id integer primary key, conv_uid VARCHAR(100) UNIQUE, chat_mode VARCHAR(50), summary VARCHAR(255), user_name VARCHAR(100), messages TEXT)" ) + self.connect.execute("CREATE SEQUENCE seq_id START 1;") def __get_messages_by_conv_uid(self, conv_uid: str): cursor = self.connect.cursor() @@ -59,7 +60,7 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): try: cursor = self.connect.cursor() cursor.execute( - "INSERT INTO chat_history(conv_uid, chat_mode summary, user_name, messages)VALUES(?,?,?,?,?)", + "INSERT INTO chat_history(id, conv_uid, chat_mode summary, user_name, messages)VALUES(nextval('seq_id'),?,?,?,?,?)", [self.chat_seesion_id, chat_mode, summary, user_name, ""], ) cursor.commit() @@ -81,7 +82,7 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): ) else: cursor.execute( - "INSERT INTO chat_history(conv_uid, chat_mode, summary, user_name, messages)VALUES(?,?,?,?,?)", + "INSERT INTO chat_history(id, conv_uid, chat_mode, summary, user_name, messages)VALUES(nextval('seq_id'),?,?,?,?,?)", [ self.chat_seesion_id, once_message.chat_mode, @@ -115,10 +116,10 @@ class DuckdbHistoryMemory(BaseChatHistoryMemory): cursor = duckdb.connect(duckdb_path).cursor() if user_name: cursor.execute( - "SELECT * FROM chat_history where user_name=? limit 20", [user_name] + "SELECT * FROM chat_history where user_name=? order by id desc limit 20", [user_name] ) else: - cursor.execute("SELECT * FROM chat_history limit 20") + cursor.execute("SELECT * FROM chat_history order by id desc limit 20") # 获取查询结果字段名 fields = [field[0] for field in cursor.description] data = [] diff --git a/pilot/openapi/api_v1/api_v1.py b/pilot/openapi/api_v1/api_v1.py index 343576f46..6ef4e5395 100644 --- a/pilot/openapi/api_v1/api_v1.py +++ b/pilot/openapi/api_v1/api_v1.py @@ -102,12 +102,7 @@ async def read_main(): @router.get("/v1/chat/dialogue/list", response_model=Result[ConversationVo]) -async def dialogue_list(response: Response, user_id: str = None): - # 设置CORS头部信息 - response.headers["Access-Control-Allow-Origin"] = "*" - response.headers["Access-Control-Allow-Methods"] = "GET" - response.headers["Access-Control-Request-Headers"] = "content-type" - +async def dialogue_list( user_id: str = None): dialogues: List = [] datas = DuckdbHistoryMemory.conv_list(user_id)