diff --git a/core/cat/rabbit_hole.py b/core/cat/rabbit_hole.py index cd663aaf2..cad18d805 100644 --- a/core/cat/rabbit_hole.py +++ b/core/cat/rabbit_hole.py @@ -155,6 +155,37 @@ def ingest_file( source=filename ) + def ingest_string( + self, + stray, + text: str + ): + """Load a string in the Cat's declarative memory. + + The method convert the string in Langchain `Document`. Then, it stores the `Document` in the Cat's memory. + + Parameters + ---------- + text : string + The text to store in the Cat's memory + + See Also + ---------- + before_rabbithole_stores_documents + """ + + docs = self.string_to_docs( + stray, + text, + chunk_size=len(text), + chunk_overlap=0 + ) + self.store_documents( + stray, + docs, + ) + + def file_to_docs( self, stray, @@ -293,7 +324,7 @@ def string_to_docs( return docs - def store_documents(self, stray, docs: List[Document], source: str) -> None: + def store_documents(self, stray, docs: List[Document], source: str=None) -> None: """Add documents to the Cat's declarative memory. This method loops a list of Langchain `Document` and adds some metadata. Namely, the source filename and the diff --git a/core/cat/routes/upload.py b/core/cat/routes/upload.py index 5ae23ade7..92a84f8fb 100644 --- a/core/cat/routes/upload.py +++ b/core/cat/routes/upload.py @@ -54,6 +54,28 @@ async def upload_file( "info": "File is being ingested asynchronously", } +# receive chunk via http endpoint +@router.post("/chunk") +async def upload_chunk( + request: Request, + background_tasks: BackgroundTasks, + chunk: str = Body( + description="A chunk to upload in the cat memory", + examples={"Example of chunk with length greater than 20"}, + min_length=20 + ), + stray = Depends(session), +) -> Dict: + """Upload a single chunk, The chunk will be vectorized and stored into documents memory.""" + + # upload chunk to long term memory, in the background + background_tasks.add_task( + stray.rabbit_hole.ingest_string, stray, chunk, + ) + + # reply to client + return {"info": "Chunk is being ingested asynchronously"} + @router.post("/web") async def upload_url(