From a206a76379acb9b20fdfdae60d5f41e44156d3f3 Mon Sep 17 00:00:00 2001 From: AlessandroSpallina Date: Thu, 28 Dec 2023 01:13:14 +0100 Subject: [PATCH 1/2] Moved sqlite db file outside the plugin directory, path is customizable in plugin settings --- dietician.py | 49 ++++++++++++++++++++++++++----------------------- settings.json | 3 +++ 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 settings.json diff --git a/dietician.py b/dietician.py index a450120..efd3652 100644 --- a/dietician.py +++ b/dietician.py @@ -1,24 +1,15 @@ -import sqlite3 -import os import hashlib from typing import List from cat.log import log -from cat.mad_hatter.decorators import tool, hook -from langchain.indexes import SQLRecordManager, index +from cat.mad_hatter.decorators import hook, plugin +from pydantic import BaseModel, Field from langchain.docstore.document import Document -from langchain.vectorstores import Qdrant -from cat.mad_hatter.decorators import hook +from sqlalchemy import ForeignKey, String, create_engine +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, Session -from typing import List -from typing import Optional -from sqlalchemy import ForeignKey, MetaData -from sqlalchemy import String -from sqlalchemy.orm import DeclarativeBase -from sqlalchemy.orm import Mapped -from sqlalchemy.orm import mapped_column -from sqlalchemy.orm import relationship -from sqlalchemy import create_engine -from sqlalchemy.orm import Session + +# sqlalchemy sqlite engine +engine = None class Base(DeclarativeBase): @@ -50,9 +41,26 @@ def __repr__(self) -> str: return f'Chunk(chunk_count={self.chunk_count!r})' -engine = create_engine(f"sqlite:///cat/plugins/ccat-dietician/dietician.db") +class PluginSettings(BaseModel): + sqlite_db_path: str = Field( + default="sqlite:///cat/data/dietician.db", + title="Sqlite filepath. Change it only if you know what you are doing and if so, then always reboot the cat otherwise the old file will be used.", + ) + + +@plugin +def settings_model(): + return PluginSettings -Base.metadata.create_all(engine, checkfirst=True) + +@hook(priority=10) +def after_cat_bootstrap(cat): + global engine + + db_filepath = cat.mad_hatter.get_plugin().load_settings()["sqlite_db_path"] + log.warning(f"Dietician is using a sqlite file located here: {db_filepath}. You can change the path in the plugin settings, after that remember to reboot the cat!") + engine = create_engine(db_filepath) + Base.metadata.create_all(engine, checkfirst=True) @hook(priority=10) @@ -66,14 +74,9 @@ def before_rabbithole_splits_text(doc, cat): return doc -# Hook called when a list of Document is going to be inserted in memory from the rabbit hole. -# Here you can edit/summarize the documents before inserting them in memory -# Should return a list of documents (each is a langchain Document) @hook(priority=10) def before_rabbithole_stores_documents(docs: List[Document], cat) -> List[Document]: cat.working_memory['ccat-dietician']['chunk_count'] = len(docs) - - #document = session.query(DietDocument).filter_by(hash=hash).first() with Session(engine) as session: try: diff --git a/settings.json b/settings.json new file mode 100644 index 0000000..cc64f89 --- /dev/null +++ b/settings.json @@ -0,0 +1,3 @@ +{ + "sqlite_db_path": "sqlite:///cat/data/dietician.db" +} \ No newline at end of file From 7d86deaf9b5e92313d329b00a2938ffc0e2a5470 Mon Sep 17 00:00:00 2001 From: AlessandroSpallina Date: Thu, 28 Dec 2023 01:30:42 +0100 Subject: [PATCH 2/2] Bump version to v0.2.1 --- plugin.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.json b/plugin.json index c2092ad..aafdd44 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "name": "Dietician", - "version": "0.2.0", - "description": "Preventing CheshireCatAI to ingest the same file multiple times.", + "version": "0.2.1", + "description": "Prevent multiple unnecessary ingestions of the same file if it's unchanged. Support declarative memory update when a document changes; simply re-upload the document. Dietician is your friend!", "author_name": "Alessandro Spallina", "author_url": "https://www.linkedin.com/in/alessandro-spallina/", "plugin_url": "https://github.com/AlessandroSpallina/ccat-dietician",