@ljoukov/llm 0.1.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/LICENSE +21 -0
- package/README.md +195 -0
- package/dist/index.cjs +3470 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +252 -0
- package/dist/index.d.ts +252 -0
- package/dist/index.js +3412 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Liudmila Joukovskaya
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# @ljoukov/llm
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ljoukov/llm)
|
|
4
|
+
[](https://www.npmjs.com/package/@ljoukov/llm)
|
|
5
|
+
[](https://github.com/ljoukov/llm/actions/workflows/ci.yml)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
Unified TypeScript wrapper over:
|
|
9
|
+
|
|
10
|
+
- **OpenAI Responses API** (`openai`)
|
|
11
|
+
- **Google Gemini via Vertex AI** (`@google/genai`)
|
|
12
|
+
- **ChatGPT subscription models** via `chatgpt-*` model ids (requires `CHATGPT_AUTH_JSON_B64`)
|
|
13
|
+
|
|
14
|
+
Designed around a single streaming API that yields:
|
|
15
|
+
|
|
16
|
+
- response text deltas
|
|
17
|
+
- thought (reasoning summary) deltas
|
|
18
|
+
- usage token counts + estimated USD cost
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm i @ljoukov/llm
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Environment variables
|
|
27
|
+
|
|
28
|
+
This package reads a `.env.local` file in `process.cwd()` (Node.js) using the same rules as Spark, and falls back to
|
|
29
|
+
plain environment variables.
|
|
30
|
+
|
|
31
|
+
### OpenAI
|
|
32
|
+
|
|
33
|
+
- `OPENAI_API_KEY`
|
|
34
|
+
|
|
35
|
+
### Gemini (Vertex AI)
|
|
36
|
+
|
|
37
|
+
- `GOOGLE_SERVICE_ACCOUNT_JSON` (the contents of a service account JSON key file, not a file path)
|
|
38
|
+
|
|
39
|
+
For local dev it is usually easiest to store the JSON on one line:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
jq -c . < path/to/service-account.json
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### ChatGPT subscription models
|
|
46
|
+
|
|
47
|
+
- `CHATGPT_AUTH_JSON_B64`
|
|
48
|
+
|
|
49
|
+
This is a base64url-encoded JSON blob containing the ChatGPT OAuth tokens + account id (Spark-compatible).
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
### Basic (non-streaming)
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { generateText } from "@ljoukov/llm";
|
|
57
|
+
|
|
58
|
+
const result = await generateText({
|
|
59
|
+
model: "gpt-5.2",
|
|
60
|
+
prompt: "Write one sentence about TypeScript.",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log(result.text);
|
|
64
|
+
console.log(result.usage, result.costUsd);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Streaming (response + thoughts + usage)
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { streamText } from "@ljoukov/llm";
|
|
71
|
+
|
|
72
|
+
const call = streamText({
|
|
73
|
+
model: "gpt-5.2",
|
|
74
|
+
prompt: "Explain what a hash function is in one paragraph.",
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
for await (const event of call.events) {
|
|
78
|
+
if (event.type === "delta" && event.channel === "thought") {
|
|
79
|
+
process.stderr.write(event.text);
|
|
80
|
+
}
|
|
81
|
+
if (event.type === "delta" && event.channel === "response") {
|
|
82
|
+
process.stdout.write(event.text);
|
|
83
|
+
}
|
|
84
|
+
if (event.type === "usage") {
|
|
85
|
+
console.log("\n\nusage:", event.usage, "costUsd:", event.costUsd);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const result = await call.result;
|
|
90
|
+
console.log("\nmodelVersion:", result.modelVersion);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Gemini
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { generateText } from "@ljoukov/llm";
|
|
97
|
+
|
|
98
|
+
const result = await generateText({
|
|
99
|
+
model: "gemini-2.5-pro",
|
|
100
|
+
prompt: "Return exactly: OK",
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
console.log(result.text);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### ChatGPT subscription models
|
|
107
|
+
|
|
108
|
+
Use a `chatgpt-` prefix:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { generateText } from "@ljoukov/llm";
|
|
112
|
+
|
|
113
|
+
const result = await generateText({
|
|
114
|
+
model: "chatgpt-gpt-5.1-codex-mini",
|
|
115
|
+
prompt: "Return exactly: OK",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
console.log(result.text);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## JSON outputs
|
|
122
|
+
|
|
123
|
+
`generateJson()` validates the output with Zod and returns the parsed value.
|
|
124
|
+
|
|
125
|
+
- OpenAI API models use structured outputs (`json_schema`) when possible.
|
|
126
|
+
- Gemini uses `responseJsonSchema`.
|
|
127
|
+
- `chatgpt-*` models fall back to best-effort JSON parsing (no strict schema mode).
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { generateJson } from "@ljoukov/llm";
|
|
131
|
+
import { z } from "zod";
|
|
132
|
+
|
|
133
|
+
const schema = z.object({
|
|
134
|
+
ok: z.boolean(),
|
|
135
|
+
message: z.string(),
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const { value } = await generateJson({
|
|
139
|
+
model: "gpt-5.2",
|
|
140
|
+
prompt: "Return a JSON object with ok=true and message='hello'.",
|
|
141
|
+
schema,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
console.log(value.ok, value.message);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Tools
|
|
148
|
+
|
|
149
|
+
This library supports two kinds of tools:
|
|
150
|
+
|
|
151
|
+
- Model tools (server-side): `web-search` and `code-execution`
|
|
152
|
+
- Your tools (JS/TS code): use `runToolLoop()` and `tool()`
|
|
153
|
+
|
|
154
|
+
### Model tools (web search / code execution)
|
|
155
|
+
|
|
156
|
+
These tools run on the provider side.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { generateText } from "@ljoukov/llm";
|
|
160
|
+
|
|
161
|
+
const result = await generateText({
|
|
162
|
+
model: "gpt-5.2",
|
|
163
|
+
prompt: "Find 3 relevant sources about X and summarize them.",
|
|
164
|
+
tools: [{ type: "web-search", mode: "live" }, { type: "code-execution" }],
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
console.log(result.text);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Your tools (function calling)
|
|
171
|
+
|
|
172
|
+
`runToolLoop()` runs a simple function-calling loop until the model returns a final answer or the step limit is hit.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { runToolLoop, tool } from "@ljoukov/llm";
|
|
176
|
+
import { z } from "zod";
|
|
177
|
+
|
|
178
|
+
const result = await runToolLoop({
|
|
179
|
+
model: "gpt-5.2",
|
|
180
|
+
prompt: "What is 12 * 9? Use the tool.",
|
|
181
|
+
tools: {
|
|
182
|
+
multiply: tool({
|
|
183
|
+
description: "Multiply two integers.",
|
|
184
|
+
inputSchema: z.object({ a: z.number(), b: z.number() }),
|
|
185
|
+
execute: ({ a, b }) => ({ value: a * b }),
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
console.log(result.text);
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|