Skip to content

Implement kb management#1

Merged
dubh3124 merged 3 commits into
mainfrom
implement_kb_management
Aug 29, 2025
Merged

Implement kb management#1
dubh3124 merged 3 commits into
mainfrom
implement_kb_management

Conversation

@dubh3124

@dubh3124 dubh3124 commented Aug 29, 2025

Copy link
Copy Markdown
Owner

This pull request introduces Knowledge Base (KB) management to the OpenWebUI Python SDK and CLI, bringing powerful new features for handling files, directories, and knowledge bases. It also refactors API response handling for greater reliability, and test coverage.


🚀 What's New

1. Knowledge Base SDK & CLI

  • New SDK Module: openwebui.api.knowledge.KnowledgeBaseAPI for KB creation, file upload, batch directory upload (with .kbignore support), listing, updating, and deletion.
  • CLI Commands (owui kb):
    • kb create <name> --description <desc>
    • kb list-kbs
    • kb list-files <kb-id> [--search "query"]
    • kb upload-file <file> --kb-id <id>
    • kb upload-dir <dir> --kb-id <id>
    • kb update-file <file-id> <new-file>
    • kb delete-file <file-id>
    • kb delete-all-files <kb-id> [--yes]
  • .kbignore Support: Respect .kbignore rules on directory upload, using pathspec for gitignore-style pattern matching.

2. Refactored API Response Handling

  • New utility: openwebui/utils/api_utils.py:handle_api_response
    • Centralizes error handling, parses responses, provides robust fallback for raw content.
    • Used in both folders and chats APIs for consistent and predictable error handling.

3. Docs & Usage

  • README.md:
    • Updated with new KB-related CLI commands and SDK usage examples.
    • Includes directory upload instructions, .kbignore handling, and batch management.

4. Test Coverage

  • Unit Tests: tests/sdk/test_sdk_knowledge.py
    • Full coverage for KB creation, file upload, directory upload, listing, and deletion.
  • CLI Tests: tests/cli/test_cli_knowledge.py
    • CLI flows for all KB operations.
  • Integration Tests: tests/integration/test_kb_sdk_integration.py
    • End-to-end workflow with real server, including setup and teardown.
  • Fixtures: Updated tests/conftest.py to provide KB creation & cleanup.

5. Project Metadata

  • New dependencies: tqdm, pathspec for progress bars and ignore file support.

🔧 Refactoring & Improvements

  • Folders & Chats APIs: Now use a shared response handler for all calls, eliminating duplicated error logic.
  • Code Style: Minor cleanups, improved type hints and docstrings.

@dubh3124 dubh3124 requested a review from Copilot August 29, 2025 07:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces comprehensive Knowledge Base (KB) management capabilities to the OpenWebUI Python SDK and CLI, while also refactoring API response handling for better reliability and consistency.

  • Knowledge Base SDK module with full CRUD operations for KBs and files, including batch directory uploads with .kbignore support
  • New CLI commands under owui kb for creating, listing, uploading to, and managing knowledge bases and their files
  • Centralized API response handling utility that eliminates duplicated error logic across different API modules

Reviewed Changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
openwebui/api/knowledge.py Implements comprehensive KnowledgeBaseAPI with file upload, directory batch upload, and KB management
openwebui/cli/main.py Adds complete kb command group with create, list, upload, update, and delete operations
openwebui/utils/api_utils.py Centralizes API response handling and error parsing across all API modules
openwebui/utils/kbignore_parser.py Implements .kbignore file parsing using pathspec for gitignore-style pattern matching
tests/sdk/test_sdk_knowledge.py Comprehensive unit tests for the KnowledgeBaseAPI functionality
tests/cli/test_cli_knowledge.py CLI tests covering all knowledge base command scenarios
tests/integration/test_kb_sdk_integration.py End-to-end integration tests for KB workflows
pyproject.toml Adds new dependencies tqdm and pathspec for progress bars and ignore file support

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

# Attempt to decode as JSON if content-type suggests it
if "application/json" in response.headers.get("content-type", ""):
return json.loads(response.content.decode("utf-8", errors="ignore"))
return json.loads(response.content.decode("utf-8", errors="ignore")) # Fallback to raw string

Copilot AI Aug 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says 'Fallback to raw string' but the code is still attempting JSON parsing. This could raise a JSONDecodeError that isn't handled, contradicting the fallback intention.

Suggested change
return json.loads(response.content.decode("utf-8", errors="ignore")) # Fallback to raw string
return response.content.decode("utf-8", errors="ignore") # Fallback to raw string

Copilot uses AI. Check for mistakes.
try:
# Filter out any unexpected keys that are not part of KnowledgeResponse
# See TODO #1 above for context
kb_data = {k: v for k, v in kb_data.items() if k in models.KnowledgeResponse.__dict__.keys()}

Copilot AI Aug 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using __dict__.keys() on a class may not include all valid model attributes, especially for generated models that might use descriptors or properties. This could filter out valid fields. Consider using a more robust method to determine valid attributes.

Suggested change
kb_data = {k: v for k, v in kb_data.items() if k in models.KnowledgeResponse.__dict__.keys()}
kb_data = {k: v for k, v in kb_data.items() if k in models.KnowledgeResponse.__annotations__.keys()}

Copilot uses AI. Check for mistakes.
Comment thread tests/conftest.py
# This is where the RuntimeError usually happens if the loop is considered closed too soon.
# Adding a small delay can sometimes help with race conditions, but ideal fix
# is to ensure the cleanup is ordered correctly by pytest_asyncio.
await asyncio.sleep(1)

Copilot AI Aug 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a sleep before closing the client is a workaround that suggests a race condition in the cleanup logic. This could mask timing-related issues and make tests unreliable. Consider addressing the root cause instead of using artificial delays.

Suggested change
await asyncio.sleep(1)

Copilot uses AI. Check for mistakes.
Comment on lines +448 to +449
import asyncio # Assuming asyncio is available

Copilot AI Aug 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports should be moved to the top of the file with other imports rather than being imported inside a function. This follows Python best practices and makes dependencies clear.

Suggested change
import asyncio # Assuming asyncio is available

Copilot uses AI. Check for mistakes.
@dubh3124 dubh3124 merged commit 86313fd into main Aug 29, 2025
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants