Compare commits

..

No commits in common. "30cad14c43c76b969492c4d168c1a5426f5df41a" and "68dbf72a670a4fed5a863bc43c43df9678d80d1e" have entirely different histories.

4 changed files with 13 additions and 13 deletions

View file

@ -24,7 +24,7 @@ from database import BasicDatabase
OPENROUTER_X_TITLE = "TG/VK Chat Bot" OPENROUTER_X_TITLE = "TG/VK Chat Bot"
OPENROUTER_HTTP_REFERER = "https://ultracoder.org" OPENROUTER_HTTP_REFERER = "https://ultracoder.org"
GROUP_CHAT_MAX_MESSAGES = 40 GROUP_CHAT_MAX_MESSAGES = 20
PRIVATE_CHAT_MAX_MESSAGES = 40 PRIVATE_CHAT_MAX_MESSAGES = 40
MAX_OUTPUT_TOKENS = 500 MAX_OUTPUT_TOKENS = 500

View file

@ -121,8 +121,8 @@ class BasicDatabase:
def context_get_messages(self, bot_id: int, chat_id: int) -> list[dict]: def context_get_messages(self, bot_id: int, chat_id: int) -> list[dict]:
self.cursor.execute(""" self.cursor.execute("""
SELECT role, text, image FROM contexts SELECT role, text, image FROM contexts
WHERE bot_id = ? AND chat_id = ? WHERE bot_id = ? AND chat_id = ? AND message_id IS NOT NULL
ORDER BY id ORDER BY message_id
""", bot_id, chat_id) """, bot_id, chat_id)
return self._to_dict(self.cursor.fetchall()) return self._to_dict(self.cursor.fetchall())
@ -133,8 +133,8 @@ class BasicDatabase:
def context_get_last_assistant_message_id(self, bot_id: int, chat_id: int) -> Optional[int]: def context_get_last_assistant_message_id(self, bot_id: int, chat_id: int) -> Optional[int]:
return self.cursor.execute(""" return self.cursor.execute("""
SELECT message_id FROM contexts SELECT message_id FROM contexts
WHERE bot_id = ? AND chat_id = ? AND role = 'assistant' WHERE bot_id = ? AND chat_id = ? AND role = 'assistant' AND message_id IS NOT NULL
ORDER BY id DESC ORDER BY message_id DESC
LIMIT 1 LIMIT 1
""", bot_id, chat_id).fetchval() """, bot_id, chat_id).fetchval()
@ -167,12 +167,16 @@ class BasicDatabase:
def _context_trim(self, bot_id: int, chat_id: int, max_messages: int): def _context_trim(self, bot_id: int, chat_id: int, max_messages: int):
current_count = self.context_get_count(bot_id, chat_id) current_count = self.context_get_count(bot_id, chat_id)
while current_count >= max_messages: while current_count >= max_messages:
oldest_message_id = self.cursor.execute( oldest_message_id = self.cursor.execute("""
"SELECT id FROM contexts WHERE bot_id = ? AND chat_id = ? ORDER BY id LIMIT 1", SELECT message_id FROM contexts
bot_id, chat_id).fetchval() WHERE bot_id = ? AND chat_id = ? AND message_id IS NOT NULL
ORDER BY message_id ASC
LIMIT 1
""", bot_id, chat_id).fetchval()
if oldest_message_id: if oldest_message_id:
self.cursor.execute("DELETE FROM contexts WHERE id = ?", oldest_message_id) self.cursor.execute("DELETE FROM contexts WHERE bot_id = ? AND chat_id = ? AND message_id = ?",
bot_id, chat_id, oldest_message_id)
current_count -= 1 current_count -= 1
else: else:
break break

View file

@ -44,14 +44,12 @@ class TgDatabase(database.BasicDatabase):
self.cursor.execute(""" self.cursor.execute("""
CREATE TABLE IF NOT EXISTS contexts ( CREATE TABLE IF NOT EXISTS contexts (
id BIGINT NOT NULL auto_increment,
bot_id BIGINT NOT NULL, bot_id BIGINT NOT NULL,
chat_id BIGINT NOT NULL, chat_id BIGINT NOT NULL,
message_id BIGINT, message_id BIGINT,
role VARCHAR(16) NOT NULL, role VARCHAR(16) NOT NULL,
text VARCHAR(4000), text VARCHAR(4000),
image MEDIUMBLOB, image MEDIUMBLOB,
PRIMARY KEY (id),
CONSTRAINT fk_contexts_chats FOREIGN KEY (bot_id, chat_id) REFERENCES chats (bot_id, chat_id) ON UPDATE CASCADE ON DELETE CASCADE) CONSTRAINT fk_contexts_chats FOREIGN KEY (bot_id, chat_id) REFERENCES chats (bot_id, chat_id) ON UPDATE CASCADE ON DELETE CASCADE)
""") """)

View file

@ -47,14 +47,12 @@ class VkDatabase(database.BasicDatabase):
self.cursor.execute(""" self.cursor.execute("""
CREATE TABLE IF NOT EXISTS contexts ( CREATE TABLE IF NOT EXISTS contexts (
id BIGINT NOT NULL auto_increment,
bot_id BIGINT NOT NULL, bot_id BIGINT NOT NULL,
chat_id BIGINT NOT NULL, chat_id BIGINT NOT NULL,
message_id BIGINT, message_id BIGINT,
role VARCHAR(16) NOT NULL, role VARCHAR(16) NOT NULL,
text VARCHAR(4000), text VARCHAR(4000),
image MEDIUMBLOB, image MEDIUMBLOB,
PRIMARY KEY (id),
CONSTRAINT fk_contexts_chats FOREIGN KEY (bot_id, chat_id) REFERENCES chats (bot_id, chat_id) ON UPDATE CASCADE ON DELETE CASCADE) CONSTRAINT fk_contexts_chats FOREIGN KEY (bot_id, chat_id) REFERENCES chats (bot_id, chat_id) ON UPDATE CASCADE ON DELETE CASCADE)
""") """)