WEB API independent

This commit is contained in:
tuyang.yhj 2023-07-04 09:45:38 +08:00
parent 02b51291b7
commit 4cfa7bb40a
2 changed files with 7 additions and 11 deletions

View File

@ -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 = []

View File

@ -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)