nrlewis / autodebater

MIT License
0 stars 0 forks source link

dialogue history storage and runtime fetch #8

Open nrlewis opened 4 months ago

nrlewis commented 4 months ago

The current chat history for each participant is stored in memory as a chat_history array.

Configure the DialogueHistory to be stored into a db backend, like SQLite using strategy pattern

Setup the participants to fetch entire history from the db backend, converting to specific format on the fly:

# participant
def respond() # only need to pass in pointer to db? 
      chat_history = [convert(msg) for msg in dialogue_history]
      response =  llm.call(chat_history)
      return response

from copilot:

# src/autodebater/storage.py
from abc import ABC, abstractmethod

class DialogueMessageStorage(ABC):

    @abstractmethod
    def save_message(self, message):
        pass

    @abstractmethod
    def get_messages(self, debate_id):
        pass

    @abstractmethod
    def generate_debate_id(self):
        pass
# src/autodebater/sqlite_storage.py
import sqlite3
from src.autodebater.storage import DialogueMessageStorage

class SQLiteDialogueMessageStorage(DialogueMessageStorage):

    def __init__(self, db_path=':memory:'):
        self.connection = sqlite3.connect(db_path)
        self._create_table()

    def _create_table(self):
        with self.connection:
            self.connection.execute('''CREATE TABLE IF NOT EXISTS dialogue_messages (
                id INTEGER PRIMARY KEY,
                name TEXT,
                role TEXT,
                message TEXT,
                debate_id TEXT,
                stance TEXT,
                judgement REAL,
                timestamp TEXT
            )''')

    def save_message(self, message):
        with self.connection:
            self.connection.execute('''INSERT INTO dialogue_messages (
                name, role, message, debate_id, stance, judgement, timestamp
            ) VALUES (?, ?, ?, ?, ?, ?, ?)''', (
                message['name'],
                message['role'],
                message['message'],
                message['debate_id'],
                message['stance'],
                message['judgement'],
                message['timestamp']
            ))

    def get_messages(self, debate_id):
        cursor = self.connection.execute('SELECT * FROM dialogue_messages WHERE debate_id=?', (debate_id,))
        return cursor.fetchall()

    def generate_debate_id(self):
        cursor = self.connection.execute('SELECT MAX(id) FROM dialogue_messages')
        max_id = cursor.fetchone()[0]
        return str((max_id or 0) + 1)

Django ORM:

# src/autodebater/django_storage.py
from src.autodebater.storage import DialogueMessageStorage
from autodebater_api.models import DialogueMessage

class DjangoORMDialogueMessageStorage(DialogueMessageStorage):

    def save_message(self, message):
        DialogueMessage.objects.create(**message)

    def get_messages(self, debate_id):
        return DialogueMessage.objects.filter(debate_id=debate_id).values()

    def generate_debate_id(self):
        latest_debate = DialogueMessage.objects.order_by('debate_id').last()
        return str((latest_debate.debate_id if latest_debate else 0) + 1)