llmjs2 1.1.0 → 1.3.0
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.
- package/CONFIG_README.md +98 -0
- package/README.md +382 -357
- package/cli.js +195 -0
- package/config.yaml +149 -0
- package/docs/BASIC_USAGE.md +296 -0
- package/docs/CLI.md +455 -0
- package/docs/GET_STARTED.md +129 -0
- package/docs/GUARDRAILS_GUIDE.md +734 -0
- package/docs/README.md +47 -0
- package/docs/ROUTER_GUIDE.md +397 -0
- package/docs/SERVER_MODE.md +350 -0
- package/index.js +199 -228
- package/package.json +43 -28
- package/providers/ollama.js +120 -88
- package/providers/openai.js +104 -0
- package/providers/openrouter.js +113 -79
- package/router.js +248 -0
- package/server.js +186 -0
- package/test.js +246 -296
- package/validate-config.js +87 -0
- package/example.js +0 -298
package/CONFIG_README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# llmjs2 Configuration Guide
|
|
2
|
+
|
|
3
|
+
This directory contains sample configuration files for testing the llmjs2 library.
|
|
4
|
+
|
|
5
|
+
## Files
|
|
6
|
+
|
|
7
|
+
- `config.yaml` - Comprehensive sample configuration with models, guardrails, and routing
|
|
8
|
+
- `.env` - Sample environment variables (add your API keys here)
|
|
9
|
+
- `validate-config.js` - Script to validate your configuration
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
1. **Add your API keys** to `.env`:
|
|
14
|
+
```bash
|
|
15
|
+
# Uncomment and add your keys
|
|
16
|
+
OPENAI_API_KEY=your_actual_openai_key
|
|
17
|
+
# OLLAMA_API_KEY and OPEN_ROUTER_API_KEY are already set for testing
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
2. **Validate the configuration**:
|
|
21
|
+
```bash
|
|
22
|
+
node validate-config.js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
3. **Start the server**:
|
|
26
|
+
```bash
|
|
27
|
+
node cli.js --config config.yaml --port 3001
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
4. **Test the API**:
|
|
31
|
+
```bash
|
|
32
|
+
curl -X POST http://localhost:3001/v1/chat/completions \
|
|
33
|
+
-H "Content-Type: application/json" \
|
|
34
|
+
-d '{"messages":[{"role":"user","content":"Hello!"}]}'
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Configuration Structure
|
|
38
|
+
|
|
39
|
+
### Model List
|
|
40
|
+
Defines available models and their providers:
|
|
41
|
+
- `model_name`: Alias for routing (can have multiple providers)
|
|
42
|
+
- `llm_params`: Provider-specific configuration
|
|
43
|
+
- `model`: Full model identifier in format `[provider]/[actual-model-name]` (e.g., `openai/gpt-4`, `ollama/minimax-m2.5:cloud`)
|
|
44
|
+
- `api_key`: API key (supports `os.environ/VAR_NAME` syntax)
|
|
45
|
+
- `api_base`: Optional custom API endpoint
|
|
46
|
+
|
|
47
|
+
**Important**: The `[provider]/` prefix is used internally for routing. When requests are sent to LLM providers, only the `[actual-model-name]` part is used.
|
|
48
|
+
|
|
49
|
+
### Guardrails
|
|
50
|
+
Custom processing logic for requests/responses:
|
|
51
|
+
- `name`: Unique identifier
|
|
52
|
+
- `mode`: `pre_call` (before LLM) or `post_call` (after LLM)
|
|
53
|
+
- `code`: JavaScript function as string
|
|
54
|
+
|
|
55
|
+
### Router Settings
|
|
56
|
+
- `routing_strategy`: `default`, `random`, or `sequential`
|
|
57
|
+
|
|
58
|
+
## Testing Different Features
|
|
59
|
+
|
|
60
|
+
### Load Balancing
|
|
61
|
+
The config includes multiple models with the same `model_name` (like `free-model`) to demonstrate load balancing.
|
|
62
|
+
|
|
63
|
+
### Guardrails
|
|
64
|
+
Try sending requests with inappropriate content to see filtering in action:
|
|
65
|
+
```bash
|
|
66
|
+
curl -X POST http://localhost:3001/v1/chat/completions \
|
|
67
|
+
-H "Content-Type: application/json" \
|
|
68
|
+
-d '{"messages":[{"role":"user","content":"This contains badword"}]}'
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Different Routing Strategies
|
|
72
|
+
Edit `router_settings.routing_strategy` to test:
|
|
73
|
+
- `random`: Random model selection
|
|
74
|
+
- `sequential`: Cycle through models
|
|
75
|
+
- `default`: Load balance same model names
|
|
76
|
+
|
|
77
|
+
## Environment Variables
|
|
78
|
+
|
|
79
|
+
The configuration supports environment variables:
|
|
80
|
+
- `os.environ/VAR_NAME` syntax in YAML
|
|
81
|
+
- `.env` file loaded automatically
|
|
82
|
+
- Standard environment variables override defaults
|
|
83
|
+
|
|
84
|
+
## Troubleshooting
|
|
85
|
+
|
|
86
|
+
- **"Model not found"**: Check that API keys are set in `.env`
|
|
87
|
+
- **"Rate limit exceeded"**: The config includes rate limiting (10 requests/minute)
|
|
88
|
+
- **Server won't start**: Run `node validate-config.js` to check configuration
|
|
89
|
+
- **API errors**: Check server logs for detailed error messages
|
|
90
|
+
|
|
91
|
+
## Production Usage
|
|
92
|
+
|
|
93
|
+
For production:
|
|
94
|
+
1. Remove test API keys from `.env`
|
|
95
|
+
2. Add your actual API keys
|
|
96
|
+
3. Configure proper rate limiting and content filtering
|
|
97
|
+
4. Set up proper logging and monitoring
|
|
98
|
+
5. Use environment-specific config files (dev/prod)
|