-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support edit metadata #414
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ead7edf
Support change metada
56af7e8
merge main
3466b3a
change [add/delete/update/reset]metadata api's parameter from doc_id …
6c4d44a
update serving api to allow adding and deleting metadata more freely
9671381
Modify delete_metadata_item api: Add keys keywords to delete by key.
219d476
lint
wzh1994 c06e572
fix bug
wzh1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import os | ||
import json | ||
from typing import List, Optional, Dict | ||
from typing import List, Optional, Dict, Union | ||
from pydantic import BaseModel, Field | ||
|
||
from starlette.responses import RedirectResponse | ||
|
@@ -213,5 +213,146 @@ def delete_files_from_group(self, request: FileGroupRequest): | |
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
class AddMetadataRequest(BaseModel): | ||
doc_ids: List[str] | ||
kv_pair: Dict[str, Union[bool, int, float, str, list]] | ||
|
||
@app.post("/add_metadata") | ||
def add_metadata(self, add_metadata_request: AddMetadataRequest): | ||
doc_ids = add_metadata_request.doc_ids | ||
kv_pair = add_metadata_request.kv_pair | ||
try: | ||
docs = self._manager.get_docs(doc_ids) | ||
if not docs: | ||
return BaseResponse(code=400, msg="Failed, no doc found") | ||
doc_meta = {} | ||
for doc in docs: | ||
meta_dict = json.loads(doc.meta) if doc.meta else {} | ||
for k, v in kv_pair.items(): | ||
if k not in meta_dict or not meta_dict[k]: | ||
meta_dict[k] = v | ||
elif isinstance(meta_dict[k], list): | ||
meta_dict[k].extend(v) if isinstance(v, list) else meta_dict[k].append(v) | ||
else: | ||
meta_dict[k] = ([meta_dict[k]] + v) if isinstance(v, list) else [meta_dict[k], v] | ||
doc_meta[doc.doc_id] = meta_dict | ||
self._manager.set_docs_new_meta(doc_meta) | ||
return BaseResponse(data=None) | ||
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
class DeleteMetadataRequest(BaseModel): | ||
doc_ids: List[str] | ||
keys: Optional[List[str]] = Field(None) | ||
kv_pair: Optional[Dict[str, Union[bool, int, float, str, list]]] = Field(None) | ||
|
||
def _inplace_del_meta(self, meta_dict, kv_pair: Dict[str, Union[None, bool, int, float, str, list]]): | ||
# alert: meta_dict is not a deepcopy | ||
for k, v in kv_pair.items(): | ||
if k not in meta_dict: | ||
continue | ||
if v is None: | ||
meta_dict.pop(k, None) | ||
elif isinstance(meta_dict[k], list): | ||
if isinstance(v, (bool, int, float, str)): | ||
v = [v] | ||
# delete v exists in meta_dict[k] | ||
meta_dict[k] = list(set(meta_dict[k]) - set(v)) | ||
else: | ||
# old meta[k] not a list, use v as condition to delete the key | ||
if meta_dict[k] == v: | ||
meta_dict.pop(k, None) | ||
|
||
@app.post("/delete_metadata_item") | ||
def delete_metadata_item(self, del_metadata_request: DeleteMetadataRequest): | ||
doc_ids = del_metadata_request.doc_ids | ||
kv_pair = del_metadata_request.kv_pair | ||
keys = del_metadata_request.keys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果我想meta_dict[k].remove(v),该怎么办 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已更新delete_metadata_item 接口,解决此需求 |
||
try: | ||
if keys is not None: | ||
# convert keys to kv_pair | ||
if kv_pair: | ||
kv_pair.update({k: None for k in keys}) | ||
else: | ||
kv_pair = {k: None for k in keys} | ||
if not kv_pair: | ||
# clear metadata | ||
self._manager.set_docs_new_meta({doc_id: {} for doc_id in doc_ids}) | ||
else: | ||
docs = self._manager.get_docs(doc_ids) | ||
if not docs: | ||
return BaseResponse(code=400, msg="Failed, no doc found") | ||
doc_meta = {} | ||
for doc in docs: | ||
meta_dict = json.loads(doc.meta) if doc.meta else {} | ||
self._inplace_del_meta(meta_dict, kv_pair) | ||
doc_meta[doc.doc_id] = meta_dict | ||
self._manager.set_docs_new_meta(doc_meta) | ||
return BaseResponse(data=None) | ||
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
class UpdateMetadataRequest(BaseModel): | ||
doc_ids: List[str] | ||
kv_pair: Dict[str, Union[bool, int, float, str, list]] | ||
|
||
@app.post("/update_or_create_metadata_keys") | ||
def update_or_create_metadata_keys(self, update_metadata_request: UpdateMetadataRequest): | ||
doc_ids = update_metadata_request.doc_ids | ||
kv_pair = update_metadata_request.kv_pair | ||
try: | ||
docs = self._manager.get_docs(doc_ids) | ||
if not docs: | ||
return BaseResponse(code=400, msg="Failed, no doc found") | ||
for doc in docs: | ||
doc_meta = {} | ||
meta_dict = json.loads(doc.meta) if doc.meta else {} | ||
for k, v in kv_pair.items(): | ||
meta_dict[k] = v | ||
doc_meta[doc.doc_id] = meta_dict | ||
self._manager.set_docs_new_meta(doc_meta) | ||
return BaseResponse(data=None) | ||
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
class ResetMetadataRequest(BaseModel): | ||
doc_ids: List[str] | ||
new_meta: Dict[str, Union[bool, int, float, str, list]] | ||
|
||
@app.post("/reset_metadata") | ||
def reset_metadata(self, reset_metadata_request: ResetMetadataRequest): | ||
doc_ids = reset_metadata_request.doc_ids | ||
new_meta = reset_metadata_request.new_meta | ||
try: | ||
docs = self._manager.get_docs(doc_ids) | ||
if not docs: | ||
return BaseResponse(code=400, msg="Failed, no doc found") | ||
self._manager.set_docs_new_meta({doc.doc_id: new_meta for doc in docs}) | ||
return BaseResponse(data=None) | ||
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
class QueryMetadataRequest(BaseModel): | ||
doc_id: str | ||
key: Optional[str] = None | ||
|
||
@app.post("/query_metadata") | ||
def query_metadata(self, query_metadata_request: QueryMetadataRequest): | ||
doc_id = query_metadata_request.doc_id | ||
key = query_metadata_request.key | ||
try: | ||
docs = self._manager.get_docs(doc_id) | ||
if not docs: | ||
return BaseResponse(data=None) | ||
doc = docs[0] | ||
meta_dict = json.loads(doc.meta) if doc.meta else {} | ||
if not key: | ||
return BaseResponse(data=meta_dict) | ||
if key not in meta_dict: | ||
return BaseResponse(code=400, msg=f"Failed, key {key} does not exist") | ||
return BaseResponse(data=meta_dict[key]) | ||
except Exception as e: | ||
return BaseResponse(code=500, msg=str(e), data=None) | ||
|
||
def __repr__(self): | ||
return lazyllm.make_repr("Module", "DocManager") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此处不合理,假设我有两个标签a和b先后add,第一次add的时候会赋值成a,然后第二次add时就报错了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我建议metadata加的时候要简单指定一下是不是list(取一个适合业务的名字,比如vector_values=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
出于简化设计和易于使用的考量。改为了这样的设计:
前提约束:metadata 内容是扁平的,即 {key: val} val 中不会出现嵌套的dict。
增加metadata时, kv_pair类型约束为: Dict[str, Union[bool, int, float, str, list]]