-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: align knowledge base CRUD API contract with kb_name, canonical payload fields, and matching OpenAPI types #9000
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bde3bc4
fix(kb): align CRUD API contract end-to-end
lxfight c975ceb
fix: restore _model_dict references, add KnowledgeBaseCreateRequest, …
lxfight 4d5045e
fix: address knowledge base contract follow-ups
lxfight de70eaa
fix: preserve knowledge base create error shape
lxfight 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||
| from astrbot.core.core_lifecycle import AstrBotCoreLifecycle | ||||||
| from astrbot.core.provider.provider import EmbeddingProvider, RerankProvider | ||||||
| from astrbot.core.utils.astrbot_path import get_astrbot_temp_path | ||||||
| from astrbot.dashboard.schemas import KnowledgeBaseRequest | ||||||
| from astrbot.dashboard.utils import generate_tsne_visualization | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -29,6 +30,19 @@ def __init__(self, core_lifecycle: AstrBotCoreLifecycle) -> None: | |||||
| def _payload(data: object) -> dict[str, Any]: | ||||||
| return data if isinstance(data, dict) else {} | ||||||
|
|
||||||
| @staticmethod | ||||||
|
sourcery-ai[bot] marked this conversation as resolved.
|
||||||
| def _canonical_kb_payload(data: object) -> dict[str, Any]: | ||||||
| """Normalize knowledge base create/update payloads. | ||||||
|
|
||||||
| Uses KnowledgeBaseRequest to handle the legacy ``name`` → | ||||||
| ``kb_name`` migration while preserving operational fields | ||||||
| like ``kb_id``. | ||||||
| """ | ||||||
| raw = KnowledgeBaseService._payload(data) | ||||||
|
Contributor
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. Modifying the input
Suggested change
|
||||||
| canonical = KnowledgeBaseRequest(**raw).canonical_payload() | ||||||
| raw.update(canonical) | ||||||
| return raw | ||||||
|
|
||||||
| def get_kb_manager(self): | ||||||
| return self.core_lifecycle.kb_manager | ||||||
|
|
||||||
|
|
@@ -293,7 +307,7 @@ async def list_kbs_from_dashboard_query(self, *, page, page_size) -> dict[str, A | |||||
|
|
||||||
| async def create_kb(self, data: object) -> tuple[dict[str, Any], str]: | ||||||
| kb_manager = self.get_kb_manager() | ||||||
| payload = self._payload(data) | ||||||
| payload = self._canonical_kb_payload(data) | ||||||
| kb_name = payload.get("kb_name") | ||||||
| if not kb_name: | ||||||
| raise KnowledgeBaseServiceError("知识库名称不能为空") | ||||||
|
|
@@ -363,7 +377,7 @@ async def get_kb_from_dashboard_query(self, kb_id: str | None) -> dict[str, Any] | |||||
| return await self.get_kb(kb_id) | ||||||
|
|
||||||
| async def update_kb(self, data: object) -> tuple[dict[str, Any], str]: | ||||||
| payload = self._payload(data) | ||||||
| payload = self._canonical_kb_payload(data) | ||||||
| kb_id = payload.get("kb_id") | ||||||
| if not kb_id: | ||||||
| raise KnowledgeBaseServiceError("缺少参数 kb_id") | ||||||
|
|
@@ -380,28 +394,20 @@ async def update_kb(self, data: object) -> tuple[dict[str, Any], str]: | |||||
| "top_k_sparse", | ||||||
| "top_m_final", | ||||||
| ] | ||||||
| if all(payload.get(key) is None for key in update_keys): | ||||||
| provided_updates = {key: payload[key] for key in update_keys if key in payload} | ||||||
| if not provided_updates: | ||||||
| raise KnowledgeBaseServiceError("至少需要提供一个更新字段") | ||||||
|
|
||||||
| current_kb = await self.get_kb_manager().get_kb(kb_id) | ||||||
| kb_name = payload.get("kb_name") | ||||||
| if kb_name is None: | ||||||
| if not current_kb: | ||||||
| raise KnowledgeBaseServiceError("知识库不存在") | ||||||
| kb_name = current_kb.kb.kb_name | ||||||
| if not current_kb: | ||||||
| raise KnowledgeBaseServiceError("知识库不存在") | ||||||
| current = current_kb.kb | ||||||
| update_data = {key: getattr(current, key, None) for key in update_keys} | ||||||
| update_data.update(provided_updates) | ||||||
|
|
||||||
| kb_helper = await self.get_kb_manager().update_kb( | ||||||
| kb_id=kb_id, | ||||||
| kb_name=kb_name, | ||||||
| description=payload.get("description"), | ||||||
| emoji=payload.get("emoji"), | ||||||
| embedding_provider_id=payload.get("embedding_provider_id"), | ||||||
| rerank_provider_id=payload.get("rerank_provider_id"), | ||||||
| chunk_size=payload.get("chunk_size"), | ||||||
| chunk_overlap=payload.get("chunk_overlap"), | ||||||
| top_k_dense=payload.get("top_k_dense"), | ||||||
| top_k_sparse=payload.get("top_k_sparse"), | ||||||
| top_m_final=payload.get("top_m_final"), | ||||||
| **update_data, | ||||||
| ) | ||||||
| if not kb_helper: | ||||||
| raise KnowledgeBaseServiceError("知识库不存在") | ||||||
|
|
@@ -762,11 +768,11 @@ async def retrieve(self, data: object) -> dict[str, Any]: | |||||
|
|
||||||
| if not query: | ||||||
| raise KnowledgeBaseServiceError("缺少参数 query") | ||||||
| kb_manager = self.get_kb_manager() | ||||||
| if not kb_names or not isinstance(kb_names, list): | ||||||
| raise KnowledgeBaseServiceError("缺少参数 kb_names 或格式错误") | ||||||
|
|
||||||
| top_k = payload.get("top_k", 5) | ||||||
| kb_manager = self.get_kb_manager() | ||||||
| results = await kb_manager.retrieve( | ||||||
| query=query, | ||||||
| kb_names=kb_names, | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.