6. SQLite#

This is a tutorial on using SQLite.

See SQLContextStorage class for storing you users’ contexts in SQL databases.

Chatsky uses sqlalchemy and aiosqlite libraries for asynchronous access to SQLite DB.

Note that protocol separator for windows differs from one for linux.

[1]:
# installing dependencies
%pip install -q chatsky[sqlite]
Note: you may need to restart the kernel to use updated packages.
[2]:
import pathlib
from platform import system

from chatsky.context_storages import context_storage_factory

from chatsky import Pipeline
from chatsky.utils.testing.common import (
    check_happy_path,
    is_interactive_mode,
)
from chatsky.utils.testing.toy_script import TOY_SCRIPT_KWARGS, HAPPY_PATH
[3]:
pathlib.Path("dbs").mkdir(exist_ok=True)
db_file = pathlib.Path("dbs/sqlite.db")
db_file.touch(exist_ok=True)

separator = "///" if system() == "Windows" else "////"
db_uri = f"sqlite+aiosqlite:{separator}{db_file.absolute()}"
db = context_storage_factory(db_uri)


pipeline = Pipeline(**TOY_SCRIPT_KWARGS, context_storage=db)
[4]:
if __name__ == "__main__":
    check_happy_path(pipeline, HAPPY_PATH, printout=True)
    if is_interactive_mode():
        pipeline.run()
USER: text='Hi'
BOT : text='Hi, how are you?'
USER: text='i'm fine, how are you?'
BOT : text='Good. What do you want to talk about?'
USER: text='Let's talk about music.'
BOT : text='Sorry, I can not talk about music now.'
USER: text='Ok, goodbye.'
BOT : text='bye'
USER: text='Hi'
BOT : text='Hi, how are you?'