scrapex 1.0.0-alpha.1 → 1.0.0-beta.2
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 +164 -5
- package/dist/embeddings/index.cjs +52 -0
- package/dist/embeddings/index.d.cts +3 -0
- package/dist/embeddings/index.d.mts +3 -0
- package/dist/embeddings/index.mjs +4 -0
- package/dist/embeddings-BjNTQSG9.cjs +1455 -0
- package/dist/embeddings-BjNTQSG9.cjs.map +1 -0
- package/dist/embeddings-Bsymy_jA.mjs +1215 -0
- package/dist/embeddings-Bsymy_jA.mjs.map +1 -0
- package/dist/{enhancer-oM4BhYYS.cjs → enhancer-Cs_WyWtJ.cjs} +2 -51
- package/dist/enhancer-Cs_WyWtJ.cjs.map +1 -0
- package/dist/{enhancer-Q6CSc1gA.mjs → enhancer-INx5NlgO.mjs} +2 -45
- package/dist/enhancer-INx5NlgO.mjs.map +1 -0
- package/dist/http-base-CHLf-Tco.cjs +684 -0
- package/dist/http-base-CHLf-Tco.cjs.map +1 -0
- package/dist/http-base-DM7YNo6X.mjs +618 -0
- package/dist/http-base-DM7YNo6X.mjs.map +1 -0
- package/dist/index-Bvseqli-.d.cts +268 -0
- package/dist/index-Bvseqli-.d.cts.map +1 -0
- package/dist/index-CIFjNySr.d.mts +268 -0
- package/dist/index-CIFjNySr.d.mts.map +1 -0
- package/dist/index-D6qfjmZQ.d.mts +401 -0
- package/dist/index-D6qfjmZQ.d.mts.map +1 -0
- package/dist/index-RFSpP5g8.d.cts +401 -0
- package/dist/index-RFSpP5g8.d.cts.map +1 -0
- package/dist/index.cjs +171 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +61 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +129 -6
- package/dist/index.mjs.map +1 -1
- package/dist/llm/index.cjs +252 -233
- package/dist/llm/index.cjs.map +1 -1
- package/dist/llm/index.d.cts +132 -85
- package/dist/llm/index.d.cts.map +1 -1
- package/dist/llm/index.d.mts +132 -85
- package/dist/llm/index.d.mts.map +1 -1
- package/dist/llm/index.mjs +244 -236
- package/dist/llm/index.mjs.map +1 -1
- package/dist/parsers/index.cjs +10 -199
- package/dist/parsers/index.d.cts +2 -133
- package/dist/parsers/index.d.mts +2 -133
- package/dist/parsers/index.mjs +2 -191
- package/dist/parsers-Bneuws8x.cjs +569 -0
- package/dist/parsers-Bneuws8x.cjs.map +1 -0
- package/dist/parsers-DsawHeo0.mjs +482 -0
- package/dist/parsers-DsawHeo0.mjs.map +1 -0
- package/dist/types-BOcHQU9s.d.mts +831 -0
- package/dist/types-BOcHQU9s.d.mts.map +1 -0
- package/dist/types-DutdBpqd.d.cts +831 -0
- package/dist/types-DutdBpqd.d.cts.map +1 -0
- package/package.json +15 -16
- package/dist/enhancer-Q6CSc1gA.mjs.map +0 -1
- package/dist/enhancer-oM4BhYYS.cjs.map +0 -1
- package/dist/parsers/index.cjs.map +0 -1
- package/dist/parsers/index.d.cts.map +0 -1
- package/dist/parsers/index.d.mts.map +0 -1
- package/dist/parsers/index.mjs.map +0 -1
- package/dist/types-CNQZVW36.d.mts +0 -150
- package/dist/types-CNQZVW36.d.mts.map +0 -1
- package/dist/types-D0HYR95H.d.cts +0 -150
- package/dist/types-D0HYR95H.d.cts.map +0 -1
package/dist/llm/index.cjs
CHANGED
|
@@ -1,316 +1,335 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_parsers = require('../parsers-Bneuws8x.cjs');
|
|
2
|
+
const require_http_base = require('../http-base-CHLf-Tco.cjs');
|
|
3
|
+
const require_enhancer = require('../enhancer-Cs_WyWtJ.cjs');
|
|
4
|
+
let zod = require("zod");
|
|
2
5
|
|
|
3
|
-
//#region src/llm/
|
|
4
|
-
const DEFAULT_MODEL$1 = "claude-3-5-haiku-20241022";
|
|
5
|
-
const DEFAULT_MAX_TOKENS$1 = 1024;
|
|
6
|
+
//#region src/llm/http.ts
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* Requires @anthropic-ai/sdk as a peer dependency.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const provider = new AnthropicProvider({ apiKey: 'sk-...' });
|
|
14
|
-
* const result = await scrape(url, { llm: provider, enhance: ['summarize'] });
|
|
15
|
-
* ```
|
|
8
|
+
* HTTP-based LLM Provider using native fetch.
|
|
9
|
+
* Provides a unified interface for any REST-based LLM API.
|
|
16
10
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
11
|
+
/**
|
|
12
|
+
* HTTP-based LLM provider.
|
|
13
|
+
* Works with any REST API using native fetch.
|
|
14
|
+
*/
|
|
15
|
+
var HttpLLMProvider = class extends require_http_base.BaseHttpProvider {
|
|
16
|
+
name;
|
|
17
|
+
requestBuilder;
|
|
18
|
+
responseMapper;
|
|
19
|
+
jsonMode;
|
|
20
|
+
constructor(config) {
|
|
21
|
+
super(config);
|
|
22
|
+
this.name = "http-llm";
|
|
23
|
+
this.jsonMode = config.jsonMode ?? false;
|
|
24
|
+
this.requestBuilder = config.requestBuilder ?? ((prompt, opts) => {
|
|
25
|
+
const messages = [];
|
|
26
|
+
if (opts.systemPrompt) messages.push({
|
|
27
|
+
role: "system",
|
|
28
|
+
content: opts.systemPrompt
|
|
30
29
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
messages.push({
|
|
31
|
+
role: "user",
|
|
32
|
+
content: prompt
|
|
33
|
+
});
|
|
34
|
+
const request = {
|
|
35
|
+
model: this.model,
|
|
36
|
+
messages
|
|
37
|
+
};
|
|
38
|
+
if (opts.temperature !== void 0) request.temperature = opts.temperature;
|
|
39
|
+
if (opts.maxTokens !== void 0) request.max_tokens = opts.maxTokens;
|
|
40
|
+
return request;
|
|
41
|
+
});
|
|
42
|
+
this.responseMapper = config.responseMapper ?? ((response) => {
|
|
43
|
+
const resp = response;
|
|
44
|
+
if (Array.isArray(resp.choices) && resp.choices.length > 0) {
|
|
45
|
+
const choice = resp.choices[0];
|
|
46
|
+
if (choice.message?.content) return choice.message.content;
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(resp.content)) {
|
|
49
|
+
const textBlock = resp.content.find((c) => c.type === "text");
|
|
50
|
+
if (textBlock?.text) return textBlock.text;
|
|
51
|
+
}
|
|
52
|
+
throw new require_http_base.ScrapeError("Unable to parse LLM response. Provide a custom responseMapper.", "VALIDATION_ERROR");
|
|
53
|
+
});
|
|
34
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Generate a text completion.
|
|
57
|
+
*/
|
|
35
58
|
async complete(prompt, options = {}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
temperature: options.temperature
|
|
46
|
-
})).content[0];
|
|
47
|
-
if (content?.type === "text" && content.text) return content.text;
|
|
48
|
-
throw new require_enhancer.ScrapeError("Unexpected or empty response from Anthropic", "LLM_ERROR");
|
|
49
|
-
} catch (error) {
|
|
50
|
-
if (error instanceof require_enhancer.ScrapeError) throw error;
|
|
51
|
-
throw new require_enhancer.ScrapeError(`Anthropic API error: ${error instanceof Error ? error.message : String(error)}`, "LLM_ERROR", void 0, error instanceof Error ? error : void 0);
|
|
52
|
-
}
|
|
59
|
+
let body = this.requestBuilder(prompt, options);
|
|
60
|
+
if (this.jsonMode && typeof body === "object" && body !== null) body = {
|
|
61
|
+
...body,
|
|
62
|
+
response_format: { type: "json_object" }
|
|
63
|
+
};
|
|
64
|
+
const { data } = await this.fetch(this.baseUrl, { body });
|
|
65
|
+
const content = this.responseMapper(data);
|
|
66
|
+
if (!content) throw new require_http_base.ScrapeError("Empty response from LLM", "LLM_ERROR");
|
|
67
|
+
return content;
|
|
53
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Generate a structured JSON completion with Zod validation.
|
|
71
|
+
*/
|
|
54
72
|
async completeJSON(prompt, schema, options = {}) {
|
|
55
73
|
const jsonPrompt = `${prompt}
|
|
56
74
|
|
|
57
75
|
Respond ONLY with valid JSON matching this schema:
|
|
58
|
-
${JSON.stringify(zodToJsonSchema
|
|
76
|
+
${JSON.stringify(zodToJsonSchema(schema), null, 2)}
|
|
59
77
|
|
|
60
78
|
Do not include any explanation or markdown formatting. Just the JSON object.`;
|
|
61
|
-
const
|
|
79
|
+
const useJsonMode = this.jsonMode;
|
|
80
|
+
let body = this.requestBuilder(jsonPrompt, {
|
|
62
81
|
...options,
|
|
63
82
|
systemPrompt: options.systemPrompt ?? "You are a helpful assistant that responds only with valid JSON."
|
|
64
83
|
});
|
|
84
|
+
if (useJsonMode && typeof body === "object" && body !== null) body = {
|
|
85
|
+
...body,
|
|
86
|
+
response_format: { type: "json_object" }
|
|
87
|
+
};
|
|
88
|
+
const { data } = await this.fetch(this.baseUrl, { body });
|
|
89
|
+
const content = this.responseMapper(data);
|
|
90
|
+
if (!content) throw new require_http_base.ScrapeError("Empty response from LLM", "LLM_ERROR");
|
|
65
91
|
try {
|
|
66
|
-
const
|
|
67
|
-
|
|
92
|
+
const trimmed = content.trim();
|
|
93
|
+
try {
|
|
94
|
+
return schema.parse(JSON.parse(trimmed));
|
|
95
|
+
} catch {}
|
|
96
|
+
const jsonMatch = content.match(/[[{][\s\S]*[\]}]/);
|
|
97
|
+
if (!jsonMatch) throw new Error("No JSON found in response");
|
|
68
98
|
const parsed = JSON.parse(jsonMatch[0]);
|
|
69
99
|
return schema.parse(parsed);
|
|
70
100
|
} catch (error) {
|
|
71
|
-
throw new
|
|
101
|
+
throw new require_http_base.ScrapeError(`Failed to parse LLM response as JSON: ${error instanceof Error ? error.message : String(error)}`, "VALIDATION_ERROR", void 0, error instanceof Error ? error : void 0);
|
|
72
102
|
}
|
|
73
103
|
}
|
|
74
104
|
};
|
|
75
105
|
/**
|
|
76
|
-
* Convert a Zod schema to a
|
|
77
|
-
*
|
|
106
|
+
* Convert a Zod schema to a JSON Schema representation.
|
|
107
|
+
* Uses Zod's built-in toJSONSchema method (Zod 4+).
|
|
108
|
+
* Used for prompting LLMs to return structured data.
|
|
78
109
|
*/
|
|
79
|
-
function zodToJsonSchema
|
|
110
|
+
function zodToJsonSchema(schema) {
|
|
111
|
+
if (typeof zod.z.toJSONSchema === "function") {
|
|
112
|
+
const { $schema, ...rest } = zod.z.toJSONSchema(schema);
|
|
113
|
+
return rest;
|
|
114
|
+
}
|
|
80
115
|
const def = schema._def;
|
|
81
|
-
switch (def.
|
|
82
|
-
case "
|
|
116
|
+
switch (def.type) {
|
|
117
|
+
case "object": {
|
|
83
118
|
const shape = schema.shape;
|
|
84
119
|
const properties = {};
|
|
85
|
-
|
|
120
|
+
const required = [];
|
|
121
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
122
|
+
properties[key] = zodToJsonSchema(value);
|
|
123
|
+
if (value._def.type !== "optional") required.push(key);
|
|
124
|
+
}
|
|
86
125
|
return {
|
|
87
126
|
type: "object",
|
|
88
|
-
properties
|
|
127
|
+
properties,
|
|
128
|
+
required
|
|
89
129
|
};
|
|
90
130
|
}
|
|
91
|
-
case "
|
|
131
|
+
case "array": return {
|
|
92
132
|
type: "array",
|
|
93
|
-
items: zodToJsonSchema
|
|
94
|
-
};
|
|
95
|
-
case "ZodString": return { type: "string" };
|
|
96
|
-
case "ZodNumber": return { type: "number" };
|
|
97
|
-
case "ZodBoolean": return { type: "boolean" };
|
|
98
|
-
case "ZodEnum": return {
|
|
99
|
-
type: "string",
|
|
100
|
-
enum: def.values
|
|
133
|
+
items: zodToJsonSchema(def.element)
|
|
101
134
|
};
|
|
135
|
+
case "string": return { type: "string" };
|
|
136
|
+
case "number": return { type: "number" };
|
|
137
|
+
case "boolean": return { type: "boolean" };
|
|
138
|
+
case "enum": {
|
|
139
|
+
const enumDef = def;
|
|
140
|
+
return {
|
|
141
|
+
type: "string",
|
|
142
|
+
enum: Object.values(enumDef.entries)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
case "optional": return zodToJsonSchema(def.innerType);
|
|
102
146
|
default: return { type: "string" };
|
|
103
147
|
}
|
|
104
148
|
}
|
|
105
149
|
|
|
106
150
|
//#endregion
|
|
107
|
-
//#region src/llm/
|
|
108
|
-
const DEFAULT_MODEL = "gpt-4o-mini";
|
|
109
|
-
const DEFAULT_MAX_TOKENS = 1024;
|
|
110
|
-
const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
151
|
+
//#region src/llm/presets.ts
|
|
111
152
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*
|
|
117
|
-
* - LM Studio (http://localhost:1234/v1)
|
|
118
|
-
* - LocalAI
|
|
119
|
-
* - vLLM
|
|
120
|
-
* - Any OpenAI-compatible API
|
|
121
|
-
*
|
|
122
|
-
* Requires `openai` as a peer dependency.
|
|
153
|
+
* Preset factory functions for common LLM providers.
|
|
154
|
+
* All presets use the HttpLLMProvider with appropriate configuration.
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Create an OpenAI LLM provider.
|
|
123
158
|
*
|
|
124
159
|
* @example
|
|
125
160
|
* ```ts
|
|
126
|
-
*
|
|
127
|
-
* const
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
161
|
+
* const provider = createOpenAI({ apiKey: 'sk-...' });
|
|
162
|
+
* const result = await scrape(url, { llm: provider, enhance: ['summarize'] });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
function createOpenAI(options) {
|
|
166
|
+
const apiKey = options?.apiKey ?? process.env.OPENAI_API_KEY;
|
|
167
|
+
if (!apiKey) throw new Error("OpenAI API key required. Set OPENAI_API_KEY env var or pass apiKey option.");
|
|
168
|
+
return new HttpLLMProvider({
|
|
169
|
+
baseUrl: options?.baseUrl ?? "https://api.openai.com/v1/chat/completions",
|
|
170
|
+
model: options?.model ?? "gpt-4o-mini",
|
|
171
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
172
|
+
jsonMode: true
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Create an Anthropic Claude LLM provider.
|
|
135
177
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* apiKey: 'lm-studio'
|
|
141
|
-
* });
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const provider = createAnthropic({ apiKey: 'sk-...' });
|
|
181
|
+
* const result = await scrape(url, { llm: provider, enhance: ['summarize'] });
|
|
142
182
|
* ```
|
|
143
183
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
model;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
} catch {
|
|
160
|
-
throw new require_enhancer.ScrapeError("openai package is required for OpenAI provider. Install with: npm install openai", "LLM_ERROR");
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
async complete(prompt, options = {}) {
|
|
164
|
-
try {
|
|
165
|
-
const client = this.client;
|
|
166
|
-
const messages = [];
|
|
167
|
-
if (options.systemPrompt) messages.push({
|
|
168
|
-
role: "system",
|
|
169
|
-
content: options.systemPrompt
|
|
170
|
-
});
|
|
171
|
-
messages.push({
|
|
172
|
-
role: "user",
|
|
173
|
-
content: prompt
|
|
174
|
-
});
|
|
175
|
-
const content = (await client.chat.completions.create({
|
|
176
|
-
model: this.model,
|
|
177
|
-
max_tokens: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
178
|
-
messages,
|
|
179
|
-
temperature: options.temperature
|
|
180
|
-
})).choices[0]?.message?.content;
|
|
181
|
-
if (!content) throw new require_enhancer.ScrapeError("Empty response from OpenAI", "LLM_ERROR");
|
|
182
|
-
return content;
|
|
183
|
-
} catch (error) {
|
|
184
|
-
if (error instanceof require_enhancer.ScrapeError) throw error;
|
|
185
|
-
throw new require_enhancer.ScrapeError(`OpenAI API error: ${error instanceof Error ? error.message : String(error)}`, "LLM_ERROR", void 0, error instanceof Error ? error : void 0);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
async completeJSON(prompt, schema, options = {}) {
|
|
189
|
-
const client = this.client;
|
|
190
|
-
try {
|
|
191
|
-
const messages = [{
|
|
192
|
-
role: "system",
|
|
193
|
-
content: options.systemPrompt ?? "You are a helpful assistant that extracts information from content."
|
|
194
|
-
}, {
|
|
184
|
+
function createAnthropic(options) {
|
|
185
|
+
const apiKey = options?.apiKey ?? process.env.ANTHROPIC_API_KEY;
|
|
186
|
+
if (!apiKey) throw new Error("Anthropic API key required. Set ANTHROPIC_API_KEY env var or pass apiKey option.");
|
|
187
|
+
const model = options?.model ?? "claude-3-5-haiku-20241022";
|
|
188
|
+
return new HttpLLMProvider({
|
|
189
|
+
baseUrl: "https://api.anthropic.com/v1/messages",
|
|
190
|
+
model,
|
|
191
|
+
headers: {
|
|
192
|
+
"x-api-key": apiKey,
|
|
193
|
+
"anthropic-version": "2023-06-01"
|
|
194
|
+
},
|
|
195
|
+
requestBuilder: (prompt, opts) => ({
|
|
196
|
+
model,
|
|
197
|
+
max_tokens: opts.maxTokens ?? 1024,
|
|
198
|
+
messages: [{
|
|
195
199
|
role: "user",
|
|
196
200
|
content: prompt
|
|
197
|
-
}]
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
response_format: { type: "json_object" }
|
|
204
|
-
})).choices[0]?.message?.content;
|
|
205
|
-
if (!content) throw new require_enhancer.ScrapeError("Empty response from OpenAI", "LLM_ERROR");
|
|
206
|
-
const parsed = JSON.parse(content);
|
|
207
|
-
return schema.parse(parsed);
|
|
208
|
-
} catch (error) {
|
|
209
|
-
if (error instanceof require_enhancer.ScrapeError) throw error;
|
|
210
|
-
const jsonPrompt = `${prompt}
|
|
211
|
-
|
|
212
|
-
Respond ONLY with valid JSON matching this schema:
|
|
213
|
-
${JSON.stringify(zodToJsonSchema(schema), null, 2)}
|
|
214
|
-
|
|
215
|
-
Do not include any explanation or markdown formatting. Just the JSON object.`;
|
|
216
|
-
const response = await this.complete(jsonPrompt, {
|
|
217
|
-
...options,
|
|
218
|
-
systemPrompt: "You respond only with valid JSON."
|
|
219
|
-
});
|
|
220
|
-
try {
|
|
221
|
-
const jsonMatch = response.match(/\{[\s\S]*\}/);
|
|
222
|
-
if (!jsonMatch) throw new Error("No JSON object found in response");
|
|
223
|
-
const parsed = JSON.parse(jsonMatch[0]);
|
|
224
|
-
return schema.parse(parsed);
|
|
225
|
-
} catch (parseError) {
|
|
226
|
-
throw new require_enhancer.ScrapeError(`Failed to parse LLM response as JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`, "VALIDATION_ERROR", void 0, parseError instanceof Error ? parseError : void 0);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
/**
|
|
232
|
-
* Convert a Zod schema to JSON Schema for structured outputs
|
|
233
|
-
*/
|
|
234
|
-
function zodToJsonSchema(schema) {
|
|
235
|
-
const def = schema._def;
|
|
236
|
-
switch (def.typeName) {
|
|
237
|
-
case "ZodObject": {
|
|
238
|
-
const shape = schema.shape;
|
|
239
|
-
const properties = {};
|
|
240
|
-
const required = [];
|
|
241
|
-
for (const [key, value] of Object.entries(shape)) {
|
|
242
|
-
properties[key] = zodToJsonSchema(value);
|
|
243
|
-
if (value._def.typeName !== "ZodOptional") required.push(key);
|
|
244
|
-
}
|
|
245
|
-
return {
|
|
246
|
-
type: "object",
|
|
247
|
-
properties,
|
|
248
|
-
required
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
case "ZodArray": return {
|
|
252
|
-
type: "array",
|
|
253
|
-
items: zodToJsonSchema(def.type)
|
|
254
|
-
};
|
|
255
|
-
case "ZodString": return { type: "string" };
|
|
256
|
-
case "ZodNumber": return { type: "number" };
|
|
257
|
-
case "ZodBoolean": return { type: "boolean" };
|
|
258
|
-
case "ZodEnum": return {
|
|
259
|
-
type: "string",
|
|
260
|
-
enum: def.values
|
|
261
|
-
};
|
|
262
|
-
case "ZodOptional": return zodToJsonSchema(def.innerType);
|
|
263
|
-
default: return { type: "string" };
|
|
264
|
-
}
|
|
201
|
+
}],
|
|
202
|
+
...opts.systemPrompt && { system: opts.systemPrompt },
|
|
203
|
+
...opts.temperature !== void 0 && { temperature: opts.temperature }
|
|
204
|
+
}),
|
|
205
|
+
responseMapper: (res) => res.content.find((item) => item.type === "text")?.text ?? ""
|
|
206
|
+
});
|
|
265
207
|
}
|
|
266
208
|
/**
|
|
267
|
-
* Create
|
|
209
|
+
* Create a Groq LLM provider.
|
|
210
|
+
* Groq provides fast inference for open-source models.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* const provider = createGroq({ model: 'llama-3.1-70b-versatile' });
|
|
215
|
+
* ```
|
|
268
216
|
*/
|
|
269
|
-
function
|
|
270
|
-
|
|
217
|
+
function createGroq(options) {
|
|
218
|
+
const apiKey = options?.apiKey ?? process.env.GROQ_API_KEY;
|
|
219
|
+
if (!apiKey) throw new Error("Groq API key required. Set GROQ_API_KEY env var or pass apiKey option.");
|
|
220
|
+
return new HttpLLMProvider({
|
|
221
|
+
baseUrl: "https://api.groq.com/openai/v1/chat/completions",
|
|
222
|
+
model: options?.model ?? "llama-3.1-70b-versatile",
|
|
223
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
224
|
+
jsonMode: true
|
|
225
|
+
});
|
|
271
226
|
}
|
|
272
227
|
/**
|
|
273
|
-
* Create an Ollama provider
|
|
228
|
+
* Create an Ollama LLM provider for local models.
|
|
274
229
|
*
|
|
275
230
|
* @example
|
|
276
231
|
* ```ts
|
|
277
232
|
* const provider = createOllama({ model: 'llama3.2' });
|
|
278
233
|
* ```
|
|
279
234
|
*/
|
|
280
|
-
function createOllama(
|
|
281
|
-
return new
|
|
282
|
-
baseUrl:
|
|
283
|
-
model:
|
|
284
|
-
|
|
235
|
+
function createOllama(options) {
|
|
236
|
+
return new HttpLLMProvider({
|
|
237
|
+
baseUrl: options.baseUrl ?? "http://localhost:11434/v1/chat/completions",
|
|
238
|
+
model: options.model,
|
|
239
|
+
requireHttps: false,
|
|
240
|
+
allowPrivate: true
|
|
285
241
|
});
|
|
286
242
|
}
|
|
287
243
|
/**
|
|
288
|
-
* Create an LM Studio provider
|
|
244
|
+
* Create an LM Studio LLM provider for local models.
|
|
289
245
|
*
|
|
290
246
|
* @example
|
|
291
247
|
* ```ts
|
|
292
248
|
* const provider = createLMStudio({ model: 'local-model' });
|
|
293
249
|
* ```
|
|
294
250
|
*/
|
|
295
|
-
function createLMStudio(
|
|
296
|
-
return new
|
|
297
|
-
baseUrl:
|
|
298
|
-
model:
|
|
299
|
-
|
|
251
|
+
function createLMStudio(options) {
|
|
252
|
+
return new HttpLLMProvider({
|
|
253
|
+
baseUrl: options.baseUrl ?? "http://localhost:1234/v1/chat/completions",
|
|
254
|
+
model: options.model,
|
|
255
|
+
requireHttps: false,
|
|
256
|
+
allowPrivate: true
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Create a Together AI LLM provider.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* const provider = createTogether({ model: 'meta-llama/Llama-3.2-3B-Instruct-Turbo' });
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
function createTogether(options) {
|
|
268
|
+
const apiKey = options?.apiKey ?? process.env.TOGETHER_API_KEY;
|
|
269
|
+
if (!apiKey) throw new Error("Together API key required. Set TOGETHER_API_KEY env var or pass apiKey option.");
|
|
270
|
+
return new HttpLLMProvider({
|
|
271
|
+
baseUrl: "https://api.together.xyz/v1/chat/completions",
|
|
272
|
+
model: options?.model ?? "meta-llama/Llama-3.2-3B-Instruct-Turbo",
|
|
273
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
274
|
+
jsonMode: true
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Create an OpenRouter LLM provider.
|
|
279
|
+
* OpenRouter provides access to many models through a unified API.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* const provider = createOpenRouter({
|
|
284
|
+
* model: 'anthropic/claude-3.5-sonnet',
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
function createOpenRouter(options) {
|
|
289
|
+
const apiKey = options.apiKey ?? process.env.OPENROUTER_API_KEY;
|
|
290
|
+
if (!apiKey) throw new Error("OpenRouter API key required. Set OPENROUTER_API_KEY env var or pass apiKey option.");
|
|
291
|
+
const headers = { Authorization: `Bearer ${apiKey}` };
|
|
292
|
+
if (options.siteUrl) headers["HTTP-Referer"] = options.siteUrl;
|
|
293
|
+
if (options.siteName) headers["X-Title"] = options.siteName;
|
|
294
|
+
return new HttpLLMProvider({
|
|
295
|
+
baseUrl: "https://openrouter.ai/api/v1/chat/completions",
|
|
296
|
+
model: options.model,
|
|
297
|
+
headers
|
|
300
298
|
});
|
|
301
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* Create a generic HTTP LLM provider.
|
|
302
|
+
* Use this for any OpenAI-compatible API.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* const provider = createHttpLLM({
|
|
307
|
+
* baseUrl: 'https://my-api.com/v1/chat/completions',
|
|
308
|
+
* model: 'my-model',
|
|
309
|
+
* headers: { Authorization: 'Bearer ...' },
|
|
310
|
+
* });
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
function createHttpLLM(config) {
|
|
314
|
+
return new HttpLLMProvider(config);
|
|
315
|
+
}
|
|
302
316
|
|
|
303
317
|
//#endregion
|
|
304
|
-
exports.AnthropicProvider = AnthropicProvider;
|
|
305
318
|
exports.ClassifySchema = require_enhancer.ClassifySchema;
|
|
306
319
|
exports.EntitiesSchema = require_enhancer.EntitiesSchema;
|
|
307
|
-
exports.
|
|
320
|
+
exports.HttpLLMProvider = HttpLLMProvider;
|
|
308
321
|
exports.SummarySchema = require_enhancer.SummarySchema;
|
|
309
322
|
exports.TagsSchema = require_enhancer.TagsSchema;
|
|
310
323
|
exports.ask = require_enhancer.ask;
|
|
324
|
+
exports.createAnthropic = createAnthropic;
|
|
325
|
+
exports.createGroq = createGroq;
|
|
326
|
+
exports.createHttpLLM = createHttpLLM;
|
|
311
327
|
exports.createLMStudio = createLMStudio;
|
|
312
328
|
exports.createOllama = createOllama;
|
|
313
329
|
exports.createOpenAI = createOpenAI;
|
|
330
|
+
exports.createOpenRouter = createOpenRouter;
|
|
331
|
+
exports.createTogether = createTogether;
|
|
314
332
|
exports.enhance = require_enhancer.enhance;
|
|
315
333
|
exports.extract = require_enhancer.extract;
|
|
334
|
+
exports.zodToJsonSchema = zodToJsonSchema;
|
|
316
335
|
//# sourceMappingURL=index.cjs.map
|