Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
files: '.*\.py$'
fail_fast: true
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-toml
files: \.toml$
- id: check-yaml
files: \.yaml$
- id: check-json
files: \.json$
exclude: ^python\/\.vscode\/.*
- id: end-of-file-fixer
files: \.py$
- id: mixed-line-ending
files: \.py$
- id: debug-statements
files: ^python\/semantic_kernel\/.*\.py$
- id: check-ast
name: Check Valid Python Samples
types: ["python"]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-check-ast
name: Check Valid Python Notebooks
types: ["jupyter"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.6
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- id: ruff-format

- repo: https://github.com/PyCQA/bandit
rev: 1.7.10
hooks:
- id: bandit
name: bandit
files: ^clients/python/semantic-kernel-memory-plugin/.*\.py$
args: ["-c", "clients/python/semantic-kernel-memory-plugin/pyproject.toml"]
additional_dependencies: ["bandit[toml]"]

- repo: https://github.com/google/addlicense
rev: 55a521bf81c24480094950caa3566548fa63875e
hooks:
- id: addlicense
# Update this pattern to match your actual file locations
files: (^|/).*\.py$
args: [ "-c", "Microsoft", "-l" , "mit", "-v" ]
98 changes: 98 additions & 0 deletions clients/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Microsoft Kernel Memory Python Libraries 🐍

This directory contains three Python packages that provide different ways to interact with Microsoft Kernel Memory:

## πŸ“¦ kernel-memory-client

An autogenerated Python client library for interacting with the Kernel Memory API. This client provides low-level access to all Kernel Memory endpoints and operations.

```python
from kernel_memory_client import Client

# Create a client
client = Client(base_url="http://localhost:9001")

# Use the client to interact with Kernel Memory
# ...
```

### Generation Info

The client was autogenerated using the OpenAPI specification (`swagger.json`) and the `openapi-python-client` tool:

```shell
# Install the openapi-python-client tool
pipx install openapi-python-client

# Generate the client
cd clients/python/kernel-memory-cli
openapi-python-client generate --path ../../swagger.json --config openapi-python-generator-config.yml --overwrrite
```

**Note:** License information and code formatting are automatically handled by pre-commit hooks in the repository. Make sure to install and run pre-commit after generating the client to ensure proper formatting and license headers are applied.

**Note:** The following patch was applied to fix the document upload functionality:

```patch
# clients/python/kernel-memory-client/kernel_memory_client/models/upload_document_body.py
@@ -1,11 +1,3 @@
files: Union[Unset, tuple[None, bytes, str]] = UNSET
if not isinstance(self.files, Unset):
- _temp_files = []
- for files_item_data in self.files:
- files_item = files_item_data.to_tuple()
-
- _temp_files.append(files_item)
- files = (None, json.dumps(_temp_files).encode(), "application/json")
-
+ files = files
```

## πŸ–₯️ kernel-memory-cli

A command-line interface for Microsoft Kernel Memory that provides an easy way to interact with Kernel Memory services from the terminal.

```bash
# Install the CLI
pip install kernel-memory-cli

# Configure the CLI
kernel-memory config --base-url http://localhost:9001

# Upload a document
kernel-memory upload mydocument.pdf

# Search for information
kernel-memory search "my query"

# Ask questions about your documents
kernel-memory ask "What does my document say about machine learning?"
```

The CLI offers intuitive commands for configuring connections, uploading documents, searching content, and asking questions about your indexed documents.

## πŸ”Œ semantic-kernel-memory-plugin

A plugin that integrates Kernel Memory with Semantic Kernel, allowing you to use Kernel Memory's document processing and retrieval capabilities within your Semantic Kernel applications.

```python
from semantic_kernel import Kernel
from semantic_kernel_memory_plugin.memory_plugin import MemoryPlugin
from kernel_memory_client import Client, AuthenticatedClient

# Create a Semantic Kernel instance
kernel = Kernel()

# Add the Kernel Memory plugin

memory_client = AuthenticatedClient(
base_url="http://localhost:9001")

memory_plugin = MemoryPlugin(memory_client=memory_client)
kernel.add_plugin(memory_plugin)

# Now use Kernel Memory capabilities in your Semantic Kernel app
# ...
```

This plugin bridges the powerful memory capabilities of Kernel Memory with the flexible AI orchestration of Semantic Kernel.
128 changes: 128 additions & 0 deletions clients/python/kernel-memory-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Kernel Memory CLI

A command line interface for Microsoft Kernel Memory based on python.

## Installation

```bash
pip install kernel-memory-cli
```

## Getting Started

Before using Kernel Memory CLI, you need to configure it:

```bash
kernel-memory config --base-url http://localhost:9001
```

If you need authentication:

```bash
kernel-memory config --base-url http://your-api-url --token your-api-token
```

## Commands

### Configuration

- Set up the CLI:
```bash
kernel-memory config --base-url <url> [--token <token>] [--default-index <index>]
```

- View current configuration:
```bash
kernel-memory show
```

### Working with Documents

- Upload a document:
```bash
kernel-memory upload <file_path> [--index <index>] [--document-id <id>] [--tags <tag1:value1>]
```

- Search document snippets:
```bash
kernel-memory search "your search query" [--index <index>] [--limit 3] [--filter-tags <tag1:value1>]
```

- Ask questions:
```bash
kernel-memory ask "your question" [--index <index>] [--filter-tags <tag1:value1>]
```

- List available indices:
```bash
kernel-memory list
```

![Help](./docs/help.png)
![List](./docs/list.png)

## Advanced Usage Examples

### Custom Processing Pipeline

You can customize the document processing pipeline using the `--steps` option when uploading:

```bash
kernel-memory upload --steps extract --steps partition --steps gen_embeddings --steps save_records mydocs-NASA-news.pdf
```

This command uploads a PDF document about NASA news and processes it through these specific pipeline steps:
1. `extract` - Extracts text content from the PDF
2. `partition` - Divides the content into manageable chunks
3. `gen_embeddings` - Generates vector embeddings for each chunk
4. `save_records` - Saves the processed chunks in the index

![Upload](./docs/upload.png)

### Searching for Information

To search for specific information in your documents:

```bash
kernel-memory search "Tell me the latest news about Artemis (a Nasa project)"
```

This command performs a semantic search in your index and returns the most relevant document snippets related to the Artemis project. Each result shows:
- Document ID
- Relevant content snippet
- Relevance score
- Associated tags (if available)

![Search](./docs/search.png)

### Asking Questions

For more advanced question answering:

```bash
kernel-memory ask "Tell me the latest news about Artemis (a Nasa project)"
```

Unlike the search command that returns raw snippets, the `ask` command:
1. Retrieves the most relevant information about the Artemis project
2. Generates a comprehensive answer synthesizing the information
3. Provides citations to the source documents
4. Creates a cohesive response that directly answers your question

![Ask](./docs/ask.png)

## Environment Variables

You can configure the CLI using environment variables:

- `KM_BASE_URL` - Base URL for the API
- `KM_TOKEN` - Authentication token
- `KM_TOKEN_PREFIX` - Prefix for the token
- `KM_DEFAULT_INDEX` - Default index to use
- `KM_TIMEOUT` - Request timeout in seconds
- `KM_VERIFY_SSL` - Whether to verify SSL certificates
- `KM_CONFIG_PATH` - Path to config file

## License

[MIT License](LICENSE)
Binary file added clients/python/kernel-memory-cli/docs/ask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added clients/python/kernel-memory-cli/docs/help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added clients/python/kernel-memory-cli/docs/list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions clients/python/kernel-memory-cli/kernel_memory_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2025 Microsoft
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading