llm-strings 0.0.1
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/README.md +354 -0
- package/dist/index.cjs +631 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +92 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.js +597 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
# llm-strings
|
|
2
|
+
|
|
3
|
+
**Connection strings for LLMs. Like database URLs, but for AI.**
|
|
4
|
+
|
|
5
|
+
```ini
|
|
6
|
+
llm://api.openai.com/gpt-5.2?temp=0.7&max=2000
|
|
7
|
+
llm://my-app:sk-key-123@api.anthropic.com/claude-sonnet-4-5?cache=5m
|
|
8
|
+
llm://bedrock-runtime.us-east-1.amazonaws.com/anthropic.claude-sonnet-4-5-20250929-v1:0?temp=0.5
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Every LLM provider invented their own parameter names. `max_tokens` vs `maxOutputTokens` vs `maxTokens`. `top_p` vs `topP` vs `p`. `stop` vs `stop_sequences` vs `stopSequences`. You write the config once, then rewrite it for every provider.
|
|
12
|
+
|
|
13
|
+
**llm-strings** gives you a single, portable format. Parse it, normalize it to any provider's API, and validate it — all in one library with zero dependencies.
|
|
14
|
+
|
|
15
|
+
Based on the [LLM Connection Strings](https://danlevy.net/llm-connection-strings/) specification.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install llm-strings
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { parse, normalize, validate, build } from "llm-strings";
|
|
27
|
+
|
|
28
|
+
// Parse a connection string into structured config
|
|
29
|
+
const config = parse("llm://api.openai.com/gpt-5.2?temp=0.7&max=2000");
|
|
30
|
+
// → { host: "api.openai.com", model: "gpt-5.2", params: { temp: "0.7", max: "2000" } }
|
|
31
|
+
|
|
32
|
+
// Normalize aliases and map to the provider's actual API param names
|
|
33
|
+
const { config: normalized, provider } = normalize(config);
|
|
34
|
+
// → params: { temperature: "0.7", max_tokens: "2000" }, provider: "openai"
|
|
35
|
+
|
|
36
|
+
// Validate against provider specs (returns [] if valid)
|
|
37
|
+
const issues = validate("llm://api.openai.com/gpt-5.2?temp=3.0");
|
|
38
|
+
// → [{ param: "temperature", message: '"temperature" must be <= 2, got 3', severity: "error" }]
|
|
39
|
+
|
|
40
|
+
// Build a connection string from a config object
|
|
41
|
+
const str = build({ host: "api.openai.com", model: "gpt-5.2", params: { temperature: "0.7" } });
|
|
42
|
+
// → "llm://api.openai.com/gpt-5.2?temperature=0.7"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Why Connection Strings?
|
|
46
|
+
|
|
47
|
+
You already use them for databases: `postgres://user:pass@host/db`. They're compact, portable, and easy to pass through environment variables. LLM configs deserve the same treatment.
|
|
48
|
+
|
|
49
|
+
**Store your entire model config in one env var:**
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
LLM_URL="llm://my-app:sk-proj-abc123@api.openai.com/gpt-5.2?temp=0.7&max=2000"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Switch providers by changing a string, not refactoring code:**
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Monday: OpenAI
|
|
59
|
+
LLM_URL="llm://api.openai.com/gpt-5.2?temp=0.7&max=2000"
|
|
60
|
+
|
|
61
|
+
# Tuesday: Anthropic
|
|
62
|
+
LLM_URL="llm://api.anthropic.com/claude-sonnet-4-5?temp=0.7&max=2000"
|
|
63
|
+
|
|
64
|
+
# Wednesday: Bedrock in production
|
|
65
|
+
LLM_URL="llm://bedrock-runtime.us-east-1.amazonaws.com/anthropic.claude-sonnet-4-5-20250929-v1:0?temp=0.7&max=2000"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Your code stays the same. `normalize()` handles the parameter translation.
|
|
69
|
+
|
|
70
|
+
## Benefits
|
|
71
|
+
|
|
72
|
+
- **One format, every provider** — Write `temp=0.7&max=2000` once. Normalization maps it to `temperature`, `max_tokens`, `maxOutputTokens`, `maxTokens`, or whatever your provider calls it.
|
|
73
|
+
- **Catch mistakes early** — `validate()` checks types, ranges, and provider-specific rules before you burn tokens on a bad request.
|
|
74
|
+
- **Zero dependencies** — Pure TypeScript. No runtime baggage.
|
|
75
|
+
- **Portable config** — Fits in an env var, a CLI flag, a config file, or a database column.
|
|
76
|
+
- **Shorthand aliases** — Use `temp`, `max`, `topp`, `freq`, `pres` — they all expand to the right thing.
|
|
77
|
+
|
|
78
|
+
## Format
|
|
79
|
+
|
|
80
|
+
```ini
|
|
81
|
+
llm://[label[:apiKey]@]host/model[?params]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Part | Required | Description | Example |
|
|
85
|
+
| ---------- | -------- | ----------------------------------------- | ------------------------------ |
|
|
86
|
+
| `label` | No | App name or identifier | `my-app` |
|
|
87
|
+
| `apiKey` | No | API key (in the password position) | `sk-proj-abc123` |
|
|
88
|
+
| `host` | Yes | Provider's API hostname | `api.openai.com` |
|
|
89
|
+
| `model` | Yes | Model name or ID | `gpt-5.2` |
|
|
90
|
+
| `params` | No | Key-value config (query string) | `temp=0.7&max=2000` |
|
|
91
|
+
|
|
92
|
+
## Examples
|
|
93
|
+
|
|
94
|
+
### Switching between providers
|
|
95
|
+
|
|
96
|
+
Write portable params, let `normalize()` translate them:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { parse, normalize } from "llm-strings";
|
|
100
|
+
|
|
101
|
+
// Same logical config, different providers
|
|
102
|
+
const strings = [
|
|
103
|
+
"llm://api.openai.com/gpt-5.2?temp=0.7&max=2000&top_p=0.9",
|
|
104
|
+
"llm://api.anthropic.com/claude-sonnet-4-5?temp=0.7&max=2000&top_p=0.9",
|
|
105
|
+
"llm://generativelanguage.googleapis.com/gemini-3-flash-preview?temp=0.7&max=2000&top_p=0.9",
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
for (const str of strings) {
|
|
109
|
+
const { config, provider } = normalize(parse(str));
|
|
110
|
+
console.log(`${provider}:`, config.params);
|
|
111
|
+
}
|
|
112
|
+
// openai: { temperature: "0.7", max_tokens: "2000", top_p: "0.9" }
|
|
113
|
+
// anthropic: { temperature: "0.7", max_tokens: "2000", top_p: "0.9" }
|
|
114
|
+
// google: { temperature: "0.7", maxOutputTokens: "2000", topP: "0.9" }
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Validating before calling the API
|
|
118
|
+
|
|
119
|
+
Catch bad config before it hits the network:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { validate } from "llm-strings";
|
|
123
|
+
|
|
124
|
+
// Anthropic doesn't allow temperature + top_p together
|
|
125
|
+
const issues = validate(
|
|
126
|
+
"llm://api.anthropic.com/claude-sonnet-4-5?temp=0.7&top_p=0.9"
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
for (const issue of issues) {
|
|
130
|
+
console.error(`[${issue.severity}] ${issue.param}: ${issue.message}`);
|
|
131
|
+
}
|
|
132
|
+
// [error] temperature: Cannot specify both "temperature" and "top_p" for Anthropic models.
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
// OpenAI reasoning models have different rules
|
|
137
|
+
const issues = validate("llm://api.openai.com/o3?temp=0.7&max=2000");
|
|
138
|
+
// [error] temperature: "temperature" is not supported by OpenAI reasoning model "o3".
|
|
139
|
+
// Use "reasoning_effort" instead of temperature for controlling output.
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Environment-driven config
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { parse, normalize } from "llm-strings";
|
|
146
|
+
|
|
147
|
+
// One env var holds the entire config
|
|
148
|
+
const { config, provider } = normalize(parse(process.env.LLM_URL!));
|
|
149
|
+
|
|
150
|
+
// Use the normalized params directly in your API call
|
|
151
|
+
const response = await fetch(`https://${config.host}/v1/chat/completions`, {
|
|
152
|
+
method: "POST",
|
|
153
|
+
headers: {
|
|
154
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
155
|
+
"Content-Type": "application/json",
|
|
156
|
+
},
|
|
157
|
+
body: JSON.stringify({
|
|
158
|
+
model: config.model,
|
|
159
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
160
|
+
...Object.fromEntries(
|
|
161
|
+
Object.entries(config.params).map(([k, v]) => [k, isNaN(+v) ? v : +v])
|
|
162
|
+
),
|
|
163
|
+
}),
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Prompt caching (Anthropic & Bedrock)
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { parse, normalize } from "llm-strings";
|
|
171
|
+
|
|
172
|
+
// cache=true → cache_control=ephemeral
|
|
173
|
+
const { config } = normalize(
|
|
174
|
+
parse("llm://api.anthropic.com/claude-sonnet-4-5?max=4096&cache=true")
|
|
175
|
+
);
|
|
176
|
+
// → params: { max_tokens: "4096", cache_control: "ephemeral" }
|
|
177
|
+
|
|
178
|
+
// cache=5m → cache_control=ephemeral + cache_ttl=5m
|
|
179
|
+
const { config: withTtl } = normalize(
|
|
180
|
+
parse("llm://api.anthropic.com/claude-sonnet-4-5?max=4096&cache=5m")
|
|
181
|
+
);
|
|
182
|
+
// → params: { max_tokens: "4096", cache_control: "ephemeral", cache_ttl: "5m" }
|
|
183
|
+
|
|
184
|
+
// Works on Bedrock too (Claude and Nova models)
|
|
185
|
+
const { config: bedrock } = normalize(
|
|
186
|
+
parse(
|
|
187
|
+
"llm://bedrock-runtime.us-east-1.amazonaws.com/anthropic.claude-sonnet-4-5-20250929-v1:0?cache=1h"
|
|
188
|
+
)
|
|
189
|
+
);
|
|
190
|
+
// → params: { cache_control: "ephemeral", cache_ttl: "1h" }
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Debugging normalization
|
|
194
|
+
|
|
195
|
+
Use verbose mode to see exactly what was transformed:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { parse, normalize } from "llm-strings";
|
|
199
|
+
|
|
200
|
+
const { changes } = normalize(
|
|
201
|
+
parse(
|
|
202
|
+
"llm://generativelanguage.googleapis.com/gemini-3-flash-preview?temp=0.7&max=2000&topp=0.9"
|
|
203
|
+
),
|
|
204
|
+
{ verbose: true }
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
for (const c of changes) {
|
|
208
|
+
console.log(`${c.from} → ${c.to} (${c.reason})`);
|
|
209
|
+
}
|
|
210
|
+
// temp → temperature (alias: "temp" → "temperature")
|
|
211
|
+
// max → max_tokens (alias: "max" → "max_tokens")
|
|
212
|
+
// max_tokens → maxOutputTokens (google uses "maxOutputTokens" instead of "max_tokens")
|
|
213
|
+
// topp → top_p (alias: "topp" → "top_p")
|
|
214
|
+
// top_p → topP (google uses "topP" instead of "top_p")
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Building connection strings programmatically
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
import { build } from "llm-strings";
|
|
221
|
+
|
|
222
|
+
const url = build({
|
|
223
|
+
host: "api.openai.com",
|
|
224
|
+
model: "gpt-5.2",
|
|
225
|
+
label: "my-app",
|
|
226
|
+
apiKey: "sk-proj-abc123",
|
|
227
|
+
params: { temperature: "0.7", max_tokens: "2000", stream: "true" },
|
|
228
|
+
});
|
|
229
|
+
// → "llm://my-app:sk-proj-abc123@api.openai.com/gpt-5.2?temperature=0.7&max_tokens=2000&stream=true"
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### AWS Bedrock with cross-region inference
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { parse, normalize, detectBedrockModelFamily } from "llm-strings";
|
|
236
|
+
|
|
237
|
+
const config = parse(
|
|
238
|
+
"llm://bedrock-runtime.us-east-1.amazonaws.com/us.anthropic.claude-sonnet-4-5-20250929-v1:0?temp=0.5&max=4096"
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
detectBedrockModelFamily(config.model);
|
|
242
|
+
// → "anthropic"
|
|
243
|
+
|
|
244
|
+
const { config: normalized } = normalize(config);
|
|
245
|
+
// → params: { temperature: "0.5", maxTokens: "4096" }
|
|
246
|
+
// (Bedrock Converse API uses camelCase)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Gateway providers (OpenRouter, Vercel)
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { parse, normalize, validate } from "llm-strings";
|
|
253
|
+
|
|
254
|
+
// OpenRouter proxies to any provider
|
|
255
|
+
const { config } = normalize(
|
|
256
|
+
parse("llm://openrouter.ai/anthropic/claude-sonnet-4-5?temp=0.7&max=2000")
|
|
257
|
+
);
|
|
258
|
+
// → params: { temperature: "0.7", max_tokens: "2000" }
|
|
259
|
+
|
|
260
|
+
// Reasoning model restrictions apply even through gateways
|
|
261
|
+
const issues = validate("llm://openrouter.ai/openai/o3?temp=0.7");
|
|
262
|
+
// → [{ param: "temperature", severity: "error",
|
|
263
|
+
// message: "...not supported by OpenAI reasoning model..." }]
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Supported Providers
|
|
267
|
+
|
|
268
|
+
| Provider | Host Pattern | Param Style |
|
|
269
|
+
| ----------- | ---------------------------------------- | ----------- |
|
|
270
|
+
| OpenAI | `api.openai.com` | snake_case |
|
|
271
|
+
| Anthropic | `api.anthropic.com` | snake_case |
|
|
272
|
+
| Google | `generativelanguage.googleapis.com` | camelCase |
|
|
273
|
+
| Mistral | `api.mistral.ai` | snake_case |
|
|
274
|
+
| Cohere | `api.cohere.com` | snake_case |
|
|
275
|
+
| AWS Bedrock | `bedrock-runtime.{region}.amazonaws.com` | camelCase |
|
|
276
|
+
| OpenRouter | `openrouter.ai` | snake_case |
|
|
277
|
+
| Vercel AI | `gateway.ai.vercel.sh` | snake_case |
|
|
278
|
+
|
|
279
|
+
Gateways like OpenRouter and Vercel route to any upstream provider. Bedrock hosts models from multiple families (Anthropic, Meta, Amazon, Mistral, Cohere, AI21) with cross-region inference support. Each provider's parameter names differ — normalization handles the translation automatically.
|
|
280
|
+
|
|
281
|
+
## Shorthand Aliases
|
|
282
|
+
|
|
283
|
+
Use these shortcuts in your connection strings — they expand automatically during normalization:
|
|
284
|
+
|
|
285
|
+
| Shorthand | Canonical |
|
|
286
|
+
| ------------------------------------------ | -------------------- |
|
|
287
|
+
| `temp` | `temperature` |
|
|
288
|
+
| `max`, `max_out`, `maxTokens` | `max_tokens` |
|
|
289
|
+
| `topp`, `topP`, `nucleus` | `top_p` |
|
|
290
|
+
| `topk`, `topK` | `top_k` |
|
|
291
|
+
| `freq`, `freq_penalty` | `frequency_penalty` |
|
|
292
|
+
| `pres`, `pres_penalty` | `presence_penalty` |
|
|
293
|
+
| `stop_sequences`, `stopSequences` | `stop` |
|
|
294
|
+
| `reasoning`, `reasoning_effort` | `effort` |
|
|
295
|
+
| `cache_control`, `cacheControl` | `cache` |
|
|
296
|
+
|
|
297
|
+
## API Reference
|
|
298
|
+
|
|
299
|
+
### `parse(connectionString): LlmConnectionConfig`
|
|
300
|
+
|
|
301
|
+
Parses an `llm://` connection string into its component parts. Throws if the scheme is not `llm://`.
|
|
302
|
+
|
|
303
|
+
### `build(config): string`
|
|
304
|
+
|
|
305
|
+
Reconstructs a connection string from a config object. Inverse of `parse()`.
|
|
306
|
+
|
|
307
|
+
### `normalize(config, options?): NormalizeResult`
|
|
308
|
+
|
|
309
|
+
Normalizes parameters for the target provider:
|
|
310
|
+
|
|
311
|
+
1. Expands shorthand aliases (`temp` → `temperature`)
|
|
312
|
+
2. Maps to provider-specific param names (`max_tokens` → `maxOutputTokens` for Google)
|
|
313
|
+
3. Normalizes cache values (`cache=true` → `cache_control=ephemeral`)
|
|
314
|
+
4. Adjusts for reasoning models (`max_tokens` → `max_completion_tokens` for o1/o3/o4)
|
|
315
|
+
|
|
316
|
+
Pass `{ verbose: true }` to get a detailed `changes` array documenting each transformation.
|
|
317
|
+
|
|
318
|
+
### `validate(connectionString): ValidationIssue[]`
|
|
319
|
+
|
|
320
|
+
Parses, normalizes, and validates a connection string against provider-specific rules. Returns `[]` if everything is valid. Checks:
|
|
321
|
+
|
|
322
|
+
- Type correctness (number, boolean, string enums)
|
|
323
|
+
- Value ranges (e.g., temperature 0–2 for OpenAI, 0–1 for Anthropic)
|
|
324
|
+
- Mutual exclusions (`temperature` + `top_p` on Anthropic)
|
|
325
|
+
- Reasoning model restrictions (no `temperature` on o1/o3/o4)
|
|
326
|
+
- Bedrock model family constraints (`topK` only for Claude/Cohere/Mistral)
|
|
327
|
+
|
|
328
|
+
### `detectProvider(host): Provider | undefined`
|
|
329
|
+
|
|
330
|
+
Identifies the provider from a hostname string.
|
|
331
|
+
|
|
332
|
+
### `detectBedrockModelFamily(model): BedrockModelFamily | undefined`
|
|
333
|
+
|
|
334
|
+
Identifies the model family (anthropic, meta, amazon, mistral, cohere, ai21) from a Bedrock model ID. Handles cross-region (`us.`, `eu.`, `apac.`) and global inference profiles.
|
|
335
|
+
|
|
336
|
+
## TypeScript
|
|
337
|
+
|
|
338
|
+
Full type definitions ship with the package:
|
|
339
|
+
|
|
340
|
+
```ts
|
|
341
|
+
import type {
|
|
342
|
+
LlmConnectionConfig,
|
|
343
|
+
NormalizeResult,
|
|
344
|
+
NormalizeChange,
|
|
345
|
+
NormalizeOptions,
|
|
346
|
+
ValidationIssue,
|
|
347
|
+
Provider,
|
|
348
|
+
BedrockModelFamily,
|
|
349
|
+
} from "llm-strings";
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## License
|
|
353
|
+
|
|
354
|
+
MIT
|