kryten-llm 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. kryten_llm-0.1.1/INSTALL.md +212 -0
  2. kryten_llm-0.1.1/LICENSE +21 -0
  3. kryten_llm-0.1.1/PKG-INFO +271 -0
  4. kryten_llm-0.1.1/README.md +240 -0
  5. kryten_llm-0.1.1/kryten_llm/__init__.py +22 -0
  6. kryten_llm-0.1.1/kryten_llm/__main__.py +137 -0
  7. kryten_llm-0.1.1/kryten_llm/components/__init__.py +24 -0
  8. kryten_llm-0.1.1/kryten_llm/components/config_reloader.py +286 -0
  9. kryten_llm-0.1.1/kryten_llm/components/context_manager.py +184 -0
  10. kryten_llm-0.1.1/kryten_llm/components/formatter.py +383 -0
  11. kryten_llm-0.1.1/kryten_llm/components/health_monitor.py +266 -0
  12. kryten_llm-0.1.1/kryten_llm/components/heartbeat.py +122 -0
  13. kryten_llm-0.1.1/kryten_llm/components/listener.py +79 -0
  14. kryten_llm-0.1.1/kryten_llm/components/llm_manager.py +349 -0
  15. kryten_llm-0.1.1/kryten_llm/components/prompt_builder.py +148 -0
  16. kryten_llm-0.1.1/kryten_llm/components/rate_limiter.py +478 -0
  17. kryten_llm-0.1.1/kryten_llm/components/response_logger.py +105 -0
  18. kryten_llm-0.1.1/kryten_llm/components/spam_detector.py +388 -0
  19. kryten_llm-0.1.1/kryten_llm/components/trigger_engine.py +278 -0
  20. kryten_llm-0.1.1/kryten_llm/components/validator.py +269 -0
  21. kryten_llm-0.1.1/kryten_llm/config.py +93 -0
  22. kryten_llm-0.1.1/kryten_llm/models/__init__.py +25 -0
  23. kryten_llm-0.1.1/kryten_llm/models/config.py +473 -0
  24. kryten_llm-0.1.1/kryten_llm/models/events.py +16 -0
  25. kryten_llm-0.1.1/kryten_llm/models/phase3.py +59 -0
  26. kryten_llm-0.1.1/kryten_llm/service.py +619 -0
  27. kryten_llm-0.1.1/kryten_llm/utils/__init__.py +0 -0
  28. kryten_llm-0.1.1/pyproject.toml +78 -0
  29. kryten_llm-0.1.1/systemd/README.md +57 -0
  30. kryten_llm-0.1.1/systemd/kryten-llm.service +28 -0
@@ -0,0 +1,212 @@
1
+ # Installation Guide
2
+
3
+ ## Prerequisites
4
+
5
+ - Python 3.10 or higher
6
+ - Poetry (Python package manager)
7
+ - NATS server (running and accessible)
8
+ - Git
9
+
10
+ ## Quick Start
11
+
12
+ ### 1. Clone the Repository
13
+
14
+ ```bash
15
+ git clone https://github.com/grobertson/kryten-llm.git
16
+ cd kryten-llm
17
+ ```
18
+
19
+ ### 2. Install Dependencies
20
+
21
+ ```bash
22
+ poetry install
23
+ ```
24
+
25
+ ### 3. Configure the Service
26
+
27
+ Copy the example configuration:
28
+
29
+ ```bash
30
+ cp config.example.json config.json
31
+ ```
32
+
33
+ Edit `config.json` with your settings:
34
+
35
+ ```json
36
+ {
37
+ "nats_url": "nats://localhost:4222",
38
+ "nats_subject_prefix": "cytube",
39
+ "service_name": "kryten-llm"
40
+ }
41
+ ```
42
+
43
+ ### 4. Run the Service
44
+
45
+ **Development mode:**
46
+
47
+ ```bash
48
+ poetry run kryten-llm --config config.json --log-level DEBUG
49
+ ```
50
+
51
+ **Using startup script (PowerShell):**
52
+
53
+ ```powershell
54
+ .\start-llm.ps1
55
+ ```
56
+
57
+ **Using startup script (Bash):**
58
+
59
+ ```bash
60
+ ./start-llm.sh
61
+ ```
62
+
63
+ ## Production Installation
64
+
65
+ ### System User Setup
66
+
67
+ Create a dedicated system user:
68
+
69
+ ```bash
70
+ sudo useradd -r -s /bin/false -d /opt/kryten-llm kryten
71
+ ```
72
+
73
+ ### Installation Directory
74
+
75
+ ```bash
76
+ sudo mkdir -p /opt/kryten-llm
77
+ sudo chown kryten:kryten /opt/kryten-llm
78
+ ```
79
+
80
+ ### Configuration Directory
81
+
82
+ ```bash
83
+ sudo mkdir -p /etc/kryten/llm
84
+ sudo chown kryten:kryten /etc/kryten/llm
85
+ sudo cp config.example.json /etc/kryten/llm/config.json
86
+ sudo chown kryten:kryten /etc/kryten/llm/config.json
87
+ sudo chmod 600 /etc/kryten/llm/config.json
88
+ ```
89
+
90
+ Edit the configuration:
91
+
92
+ ```bash
93
+ sudo nano /etc/kryten/llm/config.json
94
+ ```
95
+
96
+ ### Log Directory
97
+
98
+ ```bash
99
+ sudo mkdir -p /var/log/kryten-llm
100
+ sudo chown kryten:kryten /var/log/kryten-llm
101
+ ```
102
+
103
+ ### Install Application
104
+
105
+ ```bash
106
+ cd /opt/kryten-llm
107
+ sudo -u kryten git clone https://github.com/grobertson/kryten-llm.git .
108
+ sudo -u kryten poetry install --no-dev
109
+ ```
110
+
111
+ ### Systemd Service
112
+
113
+ Install the systemd service:
114
+
115
+ ```bash
116
+ sudo cp systemd/kryten-llm.service /etc/systemd/system/
117
+ sudo systemctl daemon-reload
118
+ sudo systemctl enable kryten-llm
119
+ sudo systemctl start kryten-llm
120
+ ```
121
+
122
+ Check service status:
123
+
124
+ ```bash
125
+ sudo systemctl status kryten-llm
126
+ sudo journalctl -u kryten-llm -f
127
+ ```
128
+
129
+ ## Updating
130
+
131
+ ### Development
132
+
133
+ ```bash
134
+ git pull
135
+ poetry install
136
+ ```
137
+
138
+ ### Production
139
+
140
+ ```bash
141
+ cd /opt/kryten-llm
142
+ sudo systemctl stop kryten-llm
143
+ sudo -u kryten git pull
144
+ sudo -u kryten poetry install --no-dev
145
+ sudo systemctl start kryten-llm
146
+ ```
147
+
148
+ ## Troubleshooting
149
+
150
+ ### Check NATS Connection
151
+
152
+ Ensure NATS server is running:
153
+
154
+ ```bash
155
+ # Check if NATS is listening
156
+ netstat -tuln | grep 4222
157
+ ```
158
+
159
+ ### View Logs
160
+
161
+ ```bash
162
+ # Systemd service logs
163
+ sudo journalctl -u kryten-llm -f
164
+
165
+ # Check for errors
166
+ sudo journalctl -u kryten-llm --since "1 hour ago" | grep -i error
167
+ ```
168
+
169
+ ### Test Configuration
170
+
171
+ ```bash
172
+ # Validate JSON syntax
173
+ python -m json.tool config.json
174
+
175
+ # Test connection manually
176
+ poetry run kryten-llm --config config.json --log-level DEBUG
177
+ ```
178
+
179
+ ### Permissions Issues
180
+
181
+ Ensure correct ownership:
182
+
183
+ ```bash
184
+ sudo chown -R kryten:kryten /opt/kryten-llm
185
+ sudo chown -R kryten:kryten /etc/kryten/llm
186
+ sudo chown -R kryten:kryten /var/log/kryten-llm
187
+ ```
188
+
189
+ ## Uninstalling
190
+
191
+ ### Stop and Disable Service
192
+
193
+ ```bash
194
+ sudo systemctl stop kryten-llm
195
+ sudo systemctl disable kryten-llm
196
+ sudo rm /etc/systemd/system/kryten-llm.service
197
+ sudo systemctl daemon-reload
198
+ ```
199
+
200
+ ### Remove Files
201
+
202
+ ```bash
203
+ sudo rm -rf /opt/kryten-llm
204
+ sudo rm -rf /etc/kryten/llm
205
+ sudo rm -rf /var/log/kryten-llm
206
+ ```
207
+
208
+ ### Remove User
209
+
210
+ ```bash
211
+ sudo userdel kryten
212
+ ```
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kryten Robot Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,271 @@
1
+ Metadata-Version: 2.4
2
+ Name: kryten-llm
3
+ Version: 0.1.1
4
+ Summary: Kryten LLM integration service - provides AI chat responses and interactions
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: cytube,moderation,chat,nats,microservices
8
+ Author: Kryten Robot Team
9
+ Requires-Python: >=3.10,<4.0
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Topic :: Communications :: Chat
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Dist: aiohttp (>=3.13.2,<4.0.0)
22
+ Requires-Dist: emoji (>=2.15.0,<3.0.0)
23
+ Requires-Dist: kryten-py (>=0.6.0,<0.7.0)
24
+ Requires-Dist: pydantic (>=2.12.5,<3.0.0)
25
+ Requires-Dist: pydantic-settings (>=2.12.0,<3.0.0)
26
+ Project-URL: Documentation, https://github.com/grobertson/kryten-llm/blob/main/README.md
27
+ Project-URL: Homepage, https://github.com/grobertson/kryten-llm
28
+ Project-URL: Repository, https://github.com/grobertson/kryten-llm
29
+ Description-Content-Type: text/markdown
30
+
31
+ # Kryten LLM
32
+
33
+ AI-powered chat bot service for CyTube, part of the Kryten ecosystem.
34
+
35
+ ## Features
36
+
37
+ - **Trigger-Based Responses**: Configurable trigger words with probabilities
38
+ - **Direct Mentions**: Responds when mentioned by name
39
+ - **LLM Integration**: Multiple LLM provider support (OpenAI, OpenRouter, local)
40
+ - **Rate Limiting**: Per-user, per-trigger, and global rate limits
41
+ - **Spam Detection**: Automatic spam detection with exponential backoff penalties
42
+ - **Context Awareness**: Tracks chat history and video context
43
+ - **Hot-Reload**: Reload configuration without restart (SIGHUP)
44
+ - **Service Discovery**: Publishes heartbeats and responds to discovery polls
45
+ - **Dry-Run Mode**: Test responses without sending to chat
46
+
47
+ ## Requirements
48
+
49
+ - Python 3.10+
50
+ - Poetry
51
+ - NATS server
52
+ - kryten-py library
53
+
54
+ ## Installation
55
+
56
+ ### Using Poetry
57
+
58
+ ```bash
59
+ # Install dependencies
60
+ poetry install
61
+
62
+ # Copy example configuration
63
+ cp config.example.json config.json
64
+
65
+ # Edit configuration with your settings
66
+ # See config.example.json for all options
67
+ ```
68
+
69
+ ### Using pip
70
+
71
+ ```bash
72
+ pip install kryten-llm
73
+ ```
74
+
75
+ ## Configuration
76
+
77
+ Configuration is stored in a JSON file. See `config.example.json` for a complete example.
78
+
79
+ ### Key Sections
80
+
81
+ | Section | Description |
82
+ |---------|-------------|
83
+ | `nats` | NATS connection settings |
84
+ | `channels` | CyTube channel connections |
85
+ | `personality` | Bot character and behavior |
86
+ | `llm_providers` | LLM API configurations |
87
+ | `triggers` | Trigger words with patterns and probabilities |
88
+ | `rate_limits` | Rate limiting rules |
89
+ | `spam_detection` | Spam detection settings |
90
+ | `testing` | Dry-run and logging options |
91
+
92
+ ### Environment Variables
93
+
94
+ Override configuration with environment variables:
95
+
96
+ ```bash
97
+ export OPENROUTER_API_KEY="your-api-key"
98
+ export KRYTEN_LLM_DRY_RUN="true"
99
+ ```
100
+
101
+ ## Usage
102
+
103
+ ### Running the Service
104
+
105
+ ```bash
106
+ # Using Poetry
107
+ poetry run kryten-llm --config config.json
108
+
109
+ # Direct Python execution
110
+ python -m kryten_llm --config config.json
111
+ ```
112
+
113
+ ### Command Line Options
114
+
115
+ | Option | Description |
116
+ |--------|-------------|
117
+ | `--config PATH` | Path to configuration file (default: `config.json`) |
118
+ | `--log-level LEVEL` | Logging level: DEBUG, INFO, WARNING, ERROR |
119
+ | `--dry-run` | Generate responses but don't send to chat |
120
+ | `--validate-config` | Validate configuration and exit |
121
+
122
+ ### Validation Mode
123
+
124
+ Validate your configuration without starting the service:
125
+
126
+ ```bash
127
+ poetry run kryten-llm --config config.json --validate-config
128
+ ```
129
+
130
+ ### Dry-Run Mode
131
+
132
+ Test responses without sending to chat:
133
+
134
+ ```bash
135
+ poetry run kryten-llm --config config.json --dry-run
136
+ ```
137
+
138
+ ## Hot-Reload (POSIX)
139
+
140
+ Reload configuration without restarting the service:
141
+
142
+ ```bash
143
+ # Send SIGHUP to reload configuration
144
+ kill -HUP $(pgrep -f kryten_llm)
145
+ ```
146
+
147
+ Safe changes that can be hot-reloaded:
148
+ - Triggers (patterns, probabilities, enabled status)
149
+ - Rate limits
150
+ - Spam detection settings
151
+ - Personality configuration
152
+ - LLM provider settings
153
+
154
+ Unsafe changes (require restart):
155
+ - NATS connection settings
156
+ - Channel configuration
157
+ - Service name
158
+
159
+ ## Production Deployment
160
+
161
+ ### Systemd Service
162
+
163
+ Install the systemd service file:
164
+
165
+ ```bash
166
+ # Copy service file
167
+ sudo cp kryten-llm.service /etc/systemd/system/
168
+
169
+ # Create config directory
170
+ sudo mkdir -p /etc/kryten-llm
171
+ sudo cp config.json /etc/kryten-llm/
172
+
173
+ # Create log directory
174
+ sudo mkdir -p /var/log/kryten-llm
175
+ sudo chown kryten:kryten /var/log/kryten-llm
176
+
177
+ # Enable and start service
178
+ sudo systemctl daemon-reload
179
+ sudo systemctl enable kryten-llm
180
+ sudo systemctl start kryten-llm
181
+ ```
182
+
183
+ ### Service Management
184
+
185
+ ```bash
186
+ # Check status
187
+ sudo systemctl status kryten-llm
188
+
189
+ # View logs
190
+ sudo journalctl -u kryten-llm -f
191
+
192
+ # Reload configuration
193
+ sudo systemctl reload kryten-llm
194
+
195
+ # Restart service
196
+ sudo systemctl restart kryten-llm
197
+ ```
198
+
199
+ ## Development
200
+
201
+ ### Running Tests
202
+
203
+ ```bash
204
+ # Run all tests
205
+ poetry run pytest
206
+
207
+ # Run with coverage
208
+ poetry run pytest --cov=kryten_llm
209
+
210
+ # Run specific test file
211
+ poetry run pytest tests/test_trigger_engine.py -v
212
+ ```
213
+
214
+ ### Code Quality
215
+
216
+ ```bash
217
+ # Linting
218
+ poetry run ruff check .
219
+
220
+ # Formatting
221
+ poetry run black .
222
+
223
+ # Type checking
224
+ poetry run mypy kryten_llm
225
+ ```
226
+
227
+ ## Architecture
228
+
229
+ The service processes messages through a pipeline:
230
+
231
+ ```
232
+ Chat Message
233
+
234
+ MessageListener (parse/filter)
235
+
236
+ TriggerEngine (detect triggers/mentions)
237
+
238
+ RateLimiter (check rate limits)
239
+
240
+ SpamDetector (check spam)
241
+
242
+ ContextManager (gather context)
243
+
244
+ PromptBuilder (build LLM prompt)
245
+
246
+ LLMManager (call LLM API)
247
+
248
+ ResponseValidator (validate response)
249
+
250
+ ResponseFormatter (format for chat)
251
+
252
+ Send to Chat
253
+ ```
254
+
255
+ ## Service Discovery
256
+
257
+ The service publishes:
258
+
259
+ - **Startup event**: When service starts
260
+ - **Heartbeat**: Every 30s with health status
261
+ - **Shutdown event**: When service stops gracefully
262
+
263
+ Subscribe to service events:
264
+ - `kryten.lifecycle.llm.startup`
265
+ - `kryten.heartbeat.llm`
266
+ - `kryten.lifecycle.llm.shutdown`
267
+
268
+ ## License
269
+
270
+ MIT License - see [LICENSE](LICENSE) for details.
271
+