From b7d365119c1d578173031984fa924b4a8146b404 Mon Sep 17 00:00:00 2001 From: Deven Patel Date: Tue, 23 Jan 2024 14:27:30 +0530 Subject: [PATCH] [Feature] add `app.delete()` method (#1187) Co-authored-by: Deven Patel --- docs/api-reference/app/delete.mdx | 33 +++++++++++++++++++++++++++++-- embedchain/embedchain.py | 12 +++++++++++ embedchain/vectordb/base.py | 5 +++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/docs/api-reference/app/delete.mdx b/docs/api-reference/app/delete.mdx index 96d79554..d1f2ceda 100644 --- a/docs/api-reference/app/delete.mdx +++ b/docs/api-reference/app/delete.mdx @@ -2,9 +2,33 @@ title: 🗑 delete --- +## Delete Document + +`delete()` method allows you to delete a document previously added to the app. + +### Usage + +```python +from embedchain import App + +app = App() + +forbes_doc_id = app.add("https://www.forbes.com/profile/elon-musk") +wiki_doc_id = app.add("https://en.wikipedia.org/wiki/Elon_Musk") + +app.delete(forbes_doc_id) # deletes the forbes document +``` + + + If you do not have the document id, you can use `app.db.get()` method to get the document and extract the `hash` key from `metadatas` dictionary object, which serves as the document id. + + + +## Delete Chat Session History + `delete_session_chat_history()` method allows you to delete all previous messages in a chat history. -## Usage +### Usage ```python from embedchain import App @@ -16,4 +40,9 @@ app.add("https://www.forbes.com/profile/elon-musk") app.chat("What is the net worth of Elon Musk?") app.delete_session_chat_history() -``` \ No newline at end of file +``` + + + `delete_session_chat_history(session_id="session_1")` method also accepts `session_id` optional param for deleting chat history of a specific session. + It assumes the default session if no `session_id` is provided. + \ No newline at end of file diff --git a/embedchain/embedchain.py b/embedchain/embedchain.py index bb69051c..7cf15d1c 100644 --- a/embedchain/embedchain.py +++ b/embedchain/embedchain.py @@ -674,3 +674,15 @@ class EmbedChain(JSONSerializable): def delete_all_chat_history(self, app_id: str): self.llm.memory.delete(app_id=app_id) self.llm.update_history(app_id=app_id) + + def delete(self, source_id: str): + """ + Deletes the data from the database. + :param source_hash: The hash of the source. + :type source_hash: str + """ + self.db.delete(where={"hash": source_id}) + logging.info(f"Successfully deleted {source_id}") + # Send anonymous telemetry + if self.config.collect_metrics: + self.telemetry.capture(event_name="delete", properties=self._telemetry_props) diff --git a/embedchain/vectordb/base.py b/embedchain/vectordb/base.py index 0fd8652c..a31cf100 100644 --- a/embedchain/vectordb/base.py +++ b/embedchain/vectordb/base.py @@ -75,3 +75,8 @@ class BaseVectorDB(JSONSerializable): :type name: str """ raise NotImplementedError + + def delete(self): + """Delete from database.""" + + raise NotImplementedError