-
-
Notifications
You must be signed in to change notification settings - Fork 52
Added Kimi K2 provider tests Fixes #40 #192
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
16 commits
Select commit
Hold shift + click to select a range
a221274
Add CLI interface for cortex command - Fixes #11
Sahilbhatane 8c8d39d
Add multi-step installation coordinator - Fixes #8
Sahilbhatane 91cdde8
Test file update for CLI
Sahilbhatane 161436c
Add Kimi provider and integration tests issue #40
Sahilbhatane 437d730
Add Kimi provider and integration tests issue cortexlinux#40
Sahilbhatane 406fe52
feat: Implement Kimi K2 API integration - Fixes #40
Sahilbhatane 1ff6fc1
feat: Implement Kimi K2 API integration - Fixes [#40](https://github.…
Sahilbhatane 8bad628
feat: Implement Kimi K2 API integration - Fixes (#40)
Sahilbhatane b631506
feat: Implement Kimi K2 API integration - Fixes #40
Sahilbhatane 78915fc
test fix and suggestion fixs
Sahilbhatane 8cfa7af
Test fix
Sahilbhatane fb4e654
test: fix CLI/interpreter tests and Windows compat
Sahilbhatane 75c0768
ci/docs: align workflow + Kimi doc
Sahilbhatane 0453ab0
ci: install pytest-timeout in workflow
Sahilbhatane a51a2c7
ci: ignore docker integration tests; add pytest-timeout
Sahilbhatane b059c82
test: isolate env-dependent provider selection
Sahilbhatane 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Contribution Guide | ||
|
|
||
| Thank you for your interest in contributing to **Cortex**. This document explains the | ||
| project workflow, coding standards, and review expectations so that every pull | ||
| request is straightforward to review and merge. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| 1. **Fork and clone the repository.** | ||
| 2. **Create a feature branch** from `main` using a descriptive name, for example | ||
| `issue-40-kimi-k2`. | ||
| 3. **Install dependencies** in a virtual environment: | ||
| ```bash | ||
| python -m venv .venv | ||
| source .venv/bin/activate # Windows: .venv\Scripts\activate | ||
| pip install --upgrade pip | ||
| pip install -r LLM/requirements.txt | ||
| pip install -r src/requirements.txt | ||
| pip install -e . | ||
| ``` | ||
| 4. **Run the full test suite** (`python test/run_all_tests.py`) to ensure your | ||
| environment is healthy before you start coding. | ||
|
|
||
| ## Coding Standards | ||
|
|
||
| - **Type hints and docstrings** are required for all public functions, classes, | ||
| and modules. CodeRabbit enforces an 80% docstring coverage threshold. | ||
| - **Formatting** follows `black` (line length 100) and `isort` ordering. Please run: | ||
| ```bash | ||
| black . | ||
| isort . | ||
| ``` | ||
| - **Linting** uses `ruff`. Address warnings locally before opening a pull request. | ||
| - **Logging and messages** must use the structured status labels (`[INFO]`, `[PLAN]`, | ||
| `[EXEC]`, `[SUCCESS]`, `[ERROR]`, etc.) to provide a consistent CLI experience. | ||
| - **Secrets** such as API keys must never be hard-coded or committed. | ||
| - **Dependency changes** must update both `LLM/requirements.txt` and any related | ||
| documentation (`README.md`, `test.md`). | ||
|
|
||
| ## Tests | ||
|
|
||
| - Unit tests live under `test/` and should be added or updated alongside code | ||
| changes. | ||
| - Integration tests live under `test/integration/` and are designed to run inside | ||
| Docker. Use the helper utilities in `test/integration/docker_utils.py` to keep | ||
| the tests concise and reliable. | ||
| - Ensure that every new feature or regression fix includes corresponding test | ||
| coverage. Submissions without meaningful tests will be sent back for revision. | ||
| - Before requesting review, run: | ||
| ```bash | ||
| python test/run_all_tests.py | ||
| ``` | ||
| Optionally, include `CORTEX_PROVIDER=fake` to avoid contacting external APIs. | ||
|
|
||
| ## Pull Request Checklist | ||
|
|
||
| - Provide a **clear title** that references the issue being addressed. | ||
| - Include a **summary** of the change, **testing notes**, and **risk assessment**. | ||
| - Confirm that **CI passes** and that **docstring coverage** meets the required threshold. | ||
| - Link the pull request to the relevant GitHub issue (`Fixes #<issue-number>`). | ||
| - Be responsive to review feedback and keep discussions on-topic. | ||
|
|
||
| We appreciate your time and effort—welcome aboard! |
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 |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| # Issue #40: Kimi K2 API Integration | ||
|
|
||
| **Issue Link:** [cortexlinux/cortex#40](https://github.com/cortexlinux/cortex/issues/40) | ||
| **PR Link:** [cortexlinux/cortex#192](https://github.com/cortexlinux/cortex/pull/192) | ||
| **Bounty:** $150 | ||
| **Status:** ✅ Implemented | ||
| **Date Completed:** December 2, 2025 | ||
|
|
||
| ## Summary | ||
|
|
||
| Successfully integrated Moonshot AI's Kimi K2 model as a new LLM provider for Cortex, expanding the platform's multi-LLM capabilities. This implementation allows users to leverage Kimi K2 for natural language command interpretation as an alternative to OpenAI GPT-4o and Anthropic Claude 3.5. | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| ### 1. Core Integration (LLM/interpreter.py) | ||
|
|
||
| **Added:** | ||
| - `KIMI` enum value to `APIProvider` | ||
| - `_call_kimi()` method for Kimi K2 HTTP API integration | ||
| - Kimi-specific initialization in `_initialize_client()` | ||
| - Default model detection for Kimi K2 (`kimi-k2-turbo-preview`) | ||
|
|
||
| **Features:** | ||
| - Full HTTP-based API integration using `requests` library | ||
| - Configurable base URL via `KIMI_API_BASE_URL` environment variable (defaults to `https://api.moonshot.ai`) | ||
| - Configurable model via `KIMI_DEFAULT_MODEL` environment variable | ||
| - Proper error handling with descriptive exceptions | ||
| - Request timeout set to 60 seconds | ||
| - JSON response parsing with validation | ||
|
|
||
| **Security:** | ||
| - Bearer token authentication | ||
| - Proper SSL/TLS via HTTPS | ||
| - Input validation and sanitization | ||
| - Error messages don't leak sensitive information | ||
|
|
||
| ### 2. CLI Support (cortex/cli.py) | ||
|
|
||
| **Updated Methods:** | ||
| - `_get_provider()`: Added Kimi detection via `KIMI_API_KEY` | ||
| - `_get_api_key(provider)`: Added Kimi API key mapping | ||
| - Updated install workflow to support fake provider for testing | ||
|
|
||
| **Environment Variables:** | ||
| - `KIMI_API_KEY`: Required for Kimi K2 authentication | ||
| - `CORTEX_PROVIDER`: Optional override (supports `openai`, `claude`, `kimi`, `fake`) | ||
| - `KIMI_API_BASE_URL`: Optional base URL override | ||
| - `KIMI_DEFAULT_MODEL`: Optional model override (default: `kimi-k2-turbo-preview`) | ||
|
|
||
| ### 3. Dependencies (LLM/requirements.txt) | ||
|
|
||
| **Updated:** | ||
| - Added `requests>=2.32.4` (addresses CVE-2024-35195, CVE-2024-37891, CVE-2023-32681) | ||
| - Security-focused version constraint ensures patched vulnerabilities | ||
|
|
||
| ### 4. Testing | ||
|
|
||
| **Added Tests:** | ||
| - `test_get_provider_kimi`: Provider detection | ||
| - `test_get_api_key_kimi`: API key retrieval | ||
| - `test_initialization_kimi`: Kimi initialization | ||
| - `test_call_kimi_success`: Successful API call | ||
| - `test_call_kimi_failure`: Error handling | ||
| - `test_call_fake_with_env_commands`: Fake provider testing | ||
|
|
||
| **Test Coverage:** | ||
| - Unit tests: ✅ 143 tests passing | ||
| - Integration tests: ✅ 5 Docker-based tests (skipped without Docker) | ||
| - All existing tests remain passing | ||
| - No regressions introduced | ||
|
|
||
| ### 5. Documentation | ||
|
|
||
| **Updated Files:** | ||
| - `README.md`: Added Kimi K2 to supported providers table, usage examples | ||
| - `cortex/cli.py`: Updated help text with Kimi environment variables | ||
| - `docs/ISSUE_40_KIMI_K2_IMPLEMENTATION.md`: This summary document | ||
|
|
||
| ## Configuration Examples | ||
|
|
||
| ### Getting a Valid API Key | ||
|
|
||
| 1. Visit [Moonshot AI Platform](https://platform.moonshot.ai/) | ||
| 2. Sign up or log in to your account | ||
| 3. Navigate to [API Keys Console](https://platform.moonshot.ai/console/api-keys) | ||
| 4. Click "Create API Key" and copy the key | ||
| 5. The key format should start with `sk-` | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```bash | ||
| # Set Kimi API key (get from Moonshot Console) | ||
| export KIMI_API_KEY="sk-your-actual-key-here" | ||
|
|
||
| # Install with Kimi K2 (auto-detected) | ||
| cortex install docker | ||
|
|
||
| # Explicit provider override | ||
| export CORTEX_PROVIDER=kimi | ||
| cortex install "nginx with ssl" | ||
| ``` | ||
|
|
||
| ### Advanced Configuration | ||
|
|
||
| ```bash | ||
| # Custom model (options: kimi-k2-turbo-preview, kimi-k2-0905-preview, kimi-k2-thinking, kimi-k2-thinking-turbo) | ||
| export KIMI_DEFAULT_MODEL="kimi-k2-0905-preview" | ||
|
|
||
| # Custom base URL (default: https://api.moonshot.ai) | ||
| export KIMI_API_BASE_URL="https://api.moonshot.ai" | ||
|
|
||
| # Dry run mode | ||
| cortex install postgresql --dry-run | ||
| ``` | ||
|
|
||
| ### Testing Without API Costs | ||
|
|
||
| ```bash | ||
| # Use fake provider for testing | ||
| export CORTEX_PROVIDER=fake | ||
| export CORTEX_FAKE_COMMANDS='{"commands": ["echo Step 1", "echo Step 2"]}' | ||
| cortex install docker --dry-run | ||
| ``` | ||
|
|
||
| ## API Request Format | ||
|
|
||
| The Kimi K2 integration uses the OpenAI-compatible chat completions endpoint: | ||
|
|
||
| ```json | ||
| POST https://api.moonshot.ai/v1/chat/completions | ||
|
|
||
| Headers: | ||
| Authorization: Bearer {KIMI_API_KEY} | ||
| Content-Type: application/json | ||
|
|
||
| Body: | ||
| { | ||
| "model": "kimi-k2-turbo-preview", | ||
| "messages": [ | ||
| {"role": "system", "content": "System prompt..."}, | ||
| {"role": "user", "content": "User request..."} | ||
| ], | ||
| "temperature": 0.3, | ||
| "max_tokens": 1000 | ||
| } | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| The implementation includes comprehensive error handling: | ||
|
|
||
| 1. **Missing Dependencies:** Clear error if `requests` package not installed | ||
| 2. **API Failures:** Runtime errors with descriptive messages | ||
| 3. **Empty Responses:** Validation that API returns valid choices | ||
| 4. **Network Issues:** Timeout protection (60s) | ||
| 5. **Authentication Errors:** HTTP status code validation via `raise_for_status()` | ||
|
|
||
| ## Code Quality Improvements | ||
|
|
||
| Based on CodeRabbit feedback, the following improvements were made: | ||
|
|
||
| 1. ✅ **Security:** Updated `requests>=2.32.4` to address known CVEs | ||
| 2. ✅ **Model Defaults:** Updated OpenAI default to `gpt-4o` (current best practice) | ||
| 3. ✅ **Test Organization:** Removed duplicate test files (`cortex/test_cli.py`, `cortex/test_coordinator.py`) | ||
| 4. ✅ **Import Fixes:** Added missing imports (`unittest`, `Mock`, `patch`, `SimpleNamespace`) | ||
| 5. ✅ **Method Signatures:** Updated `_get_api_key(provider)` to accept provider parameter | ||
| 6. ✅ **Provider Exclusions:** Removed Groq provider as per requirements (only Kimi K2 added) | ||
| 7. ✅ **Setup.py Fix:** Corrected syntax errors in package configuration | ||
|
|
||
| ## Performance Considerations | ||
|
|
||
| - **HTTP Request Timeout:** 60 seconds prevents hanging on slow connections | ||
| - **Connection Reuse:** `requests` library handles connection pooling automatically | ||
| - **Error Recovery:** Fast-fail on API errors with informative messages | ||
| - **Memory Efficiency:** JSON parsing directly from response without intermediate storage | ||
|
|
||
| ## Future Enhancements | ||
|
|
||
| Potential improvements for future iterations: | ||
|
|
||
| 1. **Streaming Support:** Add streaming response support for real-time feedback | ||
| 2. **Retry Logic:** Implement exponential backoff for transient failures | ||
| 3. **Rate Limiting:** Add rate limit awareness and queuing | ||
| 4. **Batch Operations:** Support multiple requests in parallel | ||
| 5. **Model Selection:** UI/CLI option to select specific Kimi models | ||
| 6. **Caching:** Cache common responses to reduce API costs | ||
|
|
||
| ## Testing Results | ||
|
|
||
| ```text | ||
| Ran 143 tests in 10.136s | ||
|
|
||
| OK (skipped=5) | ||
| ``` | ||
|
|
||
| All tests pass successfully: | ||
| - ✅ 138 tests passed | ||
| - ⏭️ 5 integration tests skipped (require Docker) | ||
| - ❌ 0 failures | ||
| - ❌ 0 errors | ||
|
|
||
| ## Migration Notes | ||
|
|
||
| For users upgrading: | ||
|
|
||
| 1. **Backward Compatible:** Existing OpenAI and Claude configurations continue to work | ||
| 2. **New Dependency:** `pip install requests>=2.32.4` required | ||
| 3. **Environment Variables:** Optional - no breaking changes to existing setups | ||
| 4. **Default Behavior:** No change - OpenAI remains default if multiple keys present | ||
|
|
||
| ## Related Issues | ||
|
|
||
| - **Issue #16:** Integration test suite (optional, addressed in PR #192) | ||
| - **Issue #11:** CLI improvements (referenced in commits) | ||
| - **Issue #8:** Multi-step coordinator (referenced in commits) | ||
|
|
||
| ## Contributors | ||
|
|
||
| - @Sahilbhatane - Primary implementation | ||
| - @mikejmorgan-ai - Code review and issue management | ||
| - @dhvll - Code review | ||
| - @coderabbitai - Automated code review and suggestions | ||
|
|
||
| ## Lessons Learned | ||
|
|
||
| 1. **API Documentation:** Kimi K2 follows OpenAI-compatible format, simplifying integration | ||
| 2. **Security First:** Always use latest patched dependencies (`requests>=2.32.4`) | ||
| 3. **Test Coverage:** Comprehensive testing prevents regressions | ||
| 4. **Error Messages:** Descriptive errors improve user experience | ||
| 5. **Environment Variables:** Flexible configuration reduces hard-coded values | ||
|
|
||
| ## References | ||
|
|
||
| - **Kimi K2 Documentation:** [Moonshot AI Docs](https://platform.moonshot.ai/docs) | ||
| - **Original PR:** [cortexlinux/cortex#192](https://github.com/cortexlinux/cortex/pull/192) | ||
| - **Issue Discussion:** [cortexlinux/cortex#40](https://github.com/cortexlinux/cortex/issues/40) | ||
| - **CVE Fixes:** CVE-2024-35195, CVE-2024-37891, CVE-2023-32681 |
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.