Skip to content

Commit

Permalink
Merge pull request #4 from AlessandroSpallina/develop
Browse files Browse the repository at this point in the history
Welcome v0.2.1
  • Loading branch information
AlessandroSpallina authored Dec 28, 2023
2 parents df88812 + 7d86dea commit 5bdcadb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
49 changes: 26 additions & 23 deletions dietician.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sqlite_db_path": "sqlite:///cat/data/dietician.db"
}

0 comments on commit 5bdcadb

Please sign in to comment.