mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-29 20:05:25 +08:00
feat: add list kbengine and parser api (#89)
This commit is contained in:
@@ -82,6 +82,66 @@ async def get_query_vars(self) -> dict[str, Any]:
|
||||
query_vars = await event_context.get_query_vars()
|
||||
```
|
||||
|
||||
### List Knowledge Bases Configured for the Current Pipeline
|
||||
|
||||
Get the knowledge bases configured in the pipeline used by the current request.
|
||||
|
||||
```python
|
||||
async def list_pipeline_knowledge_bases(self) -> list[dict[str, Any]]:
|
||||
"""List knowledge bases configured for the current pipeline"""
|
||||
|
||||
# Usage example
|
||||
knowledge_bases = await event_context.list_pipeline_knowledge_bases()
|
||||
|
||||
# Return example
|
||||
[
|
||||
{
|
||||
"uuid": "kb_uuid",
|
||||
"name": "Product Docs",
|
||||
"description": "Internal product documentation",
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
This only returns knowledge bases bound in the current pipeline's `Local Agent` configuration. If the pipeline has no knowledge base configured, or is not using `Local Agent`, an empty list is returned.
|
||||
|
||||
### Retrieve Knowledge from the Current Pipeline Scope
|
||||
|
||||
Run retrieval against the knowledge bases that are allowed for the current request's pipeline.
|
||||
|
||||
```python
|
||||
async def retrieve_knowledge(
|
||||
self,
|
||||
kb_id: str,
|
||||
query_text: str,
|
||||
top_k: int = 5,
|
||||
filters: dict[str, Any] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Retrieve relevant documents from a knowledge base"""
|
||||
|
||||
# Usage example
|
||||
knowledge_bases = await event_context.list_pipeline_knowledge_bases()
|
||||
results = await event_context.retrieve_knowledge(
|
||||
kb_id=knowledge_bases[0]["uuid"],
|
||||
query_text="How do I configure the enterprise knowledge base?",
|
||||
top_k=3,
|
||||
filters={"document_type": {"$eq": "manual"}},
|
||||
)
|
||||
|
||||
# Return example
|
||||
[
|
||||
{
|
||||
"id": "chunk_0",
|
||||
"content": [{"type": "text", "text": "Knowledge base configuration guide"}],
|
||||
"metadata": {"document_id": "doc_001"},
|
||||
"distance": 0.12,
|
||||
"score": 0.88,
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
`kb_id` must come from `list_pipeline_knowledge_bases` and must belong to a knowledge base configured for the current pipeline. Otherwise, LangBot returns an error.
|
||||
|
||||
## LangBot API
|
||||
|
||||
These APIs can be called in any plugin component. Access methods:
|
||||
@@ -213,6 +273,19 @@ llm_message = await self.plugin.invoke_llm(
|
||||
)
|
||||
```
|
||||
|
||||
### List Available Parsers
|
||||
|
||||
List Parser plugins currently available on the host, optionally filtered by MIME type.
|
||||
|
||||
```python
|
||||
async def list_parsers(self, mime_type: str | None = None) -> list[dict[str, Any]]:
|
||||
"""List available Parser plugins"""
|
||||
|
||||
# Usage example
|
||||
parsers = await self.plugin.list_parsers(mime_type="application/pdf")
|
||||
# Each item includes plugin_id, plugin_author, plugin_name, name, description, supported_mime_types
|
||||
```
|
||||
|
||||
### Set Plugin Persistent Data
|
||||
|
||||
Persistently store plugin data. Data stored through this interface can only be accessed by this plugin. Values need to be converted to bytes manually.
|
||||
@@ -495,4 +568,4 @@ async def get_knowledge_file_stream(self, storage_path: str) -> bytes:
|
||||
|
||||
# Usage example
|
||||
file_bytes = await self.plugin.get_knowledge_file_stream(context.file_object.storage_path)
|
||||
```
|
||||
```
|
||||
|
||||
@@ -176,12 +176,22 @@ async def ingest(self, context: IngestionContext) -> IngestionResult:
|
||||
|
||||
## Cross-Plugin Parser Invocation
|
||||
|
||||
KnowledgeEngine plugins can invoke parsers provided by other plugins via `self.plugin.invoke_parser`:
|
||||
Before invoking a parser from another plugin, you can use `self.plugin.list_parsers` to discover the parsers currently available on the host:
|
||||
|
||||
```python
|
||||
parsers = await self.plugin.list_parsers(mime_type="application/pdf")
|
||||
# Each item includes plugin_id, plugin_author, plugin_name, name, description, supported_mime_types
|
||||
```
|
||||
|
||||
If the list is empty, no connected Parser plugin currently supports that MIME type.
|
||||
|
||||
After obtaining `plugin_author` and `plugin_name`, you can call `self.plugin.invoke_parser`:
|
||||
|
||||
```python
|
||||
parser = parsers[0]
|
||||
result = await self.plugin.invoke_parser(
|
||||
plugin_author="author_name",
|
||||
plugin_name="plugin_name",
|
||||
plugin_author=parser["plugin_author"],
|
||||
plugin_name=parser["plugin_name"],
|
||||
storage_path=context.file_object.storage_path,
|
||||
mime_type=context.file_object.metadata.mime_type,
|
||||
filename=context.file_object.metadata.filename,
|
||||
|
||||
Reference in New Issue
Block a user