Skip to content

Commit

Permalink
Added delete option to vectordb
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrus2281 committed Sep 4, 2024
1 parent 5af2fc3 commit caf061b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
9 changes: 8 additions & 1 deletion docs/components/langchain_vector_store_embedding_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ component_config:
<freeform-object>
},
...
]
],
id: [
<string>,
...
],
action: <string>
}
```
| Field | Required | Description |
| --- | --- | --- |
| texts | True | |
| metadatas | False | |
| id | False | The ID of the text to add to the index. required for 'delete' action |
| action | False | The action to perform on the index from one of 'add', 'delete' |


## Component Output Schema
Expand Down
2 changes: 1 addition & 1 deletion docs/components/langchain_vector_store_embedding_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ component_config:
| embedding_component_path | True | | The embedding library path - e.g. 'langchain_community.embeddings' |
| embedding_component_name | True | | The embedding model to use - e.g. BedrockEmbeddings |
| embedding_component_config | True | | Model specific configuration for the embedding model. See documentation for valid parameter names. |
| max_results | True | | The maximum number of results to return |
| max_results | True | 3 | The maximum number of results to return |
| combine_context_from_same_source | False | True | Set to False if you don't want to combine all the context from the same source. Default is True |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@
"type": "object",
},
},
"id": {
"type": "array",
"items": {
"type": "string",
},
"description": "The ID of the text to add to the index. required for 'delete' action",
},
"action": {
"type": "string",
"default": "add",
"description": "The action to perform on the index from one of 'add', 'delete'",
},
},
"required": ["texts"],
},
Expand Down Expand Up @@ -116,12 +128,36 @@ def invoke(self, message, data):

# Get the metadatas if they exist
metadatas = data.get("metadatas", None)
args = [texts]
if metadatas is not None:
if not isinstance(metadatas, list):
metadatas = [metadatas]
args.append(metadatas)

# Get the ids if they exist
ids = data.get("id", None)
if ids is not None:
if not isinstance(ids, list):
ids = [ids]

action = data.get("action", "add")

match action:
case "add":
return self.add_data(texts, metadatas, ids)
case "delete":
return self.delete_data(ids)
case _:
raise ValueError("Invalid action: {}".format(action))

def add_data(self, texts, metadatas=None, ids=None):
# Add the texts to the vector store
self.vector_store.add_texts(*args)
args = [texts]
if metadatas is not None:
args.append(metadatas)
self.vector_store.add_texts(*args, ids=ids)
return {"result": "OK"}

def delete_data(self, ids):
if not ids:
raise ValueError("No IDs provided to delete")
self.vector_store.delete(ids)
return {"result": "OK"}

0 comments on commit caf061b

Please sign in to comment.