@node-llm/core 0.3.0 → 0.4.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 +176 -25
- package/dist/chat/Chat.d.ts +26 -1
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +83 -1
- package/dist/chat/ChatOptions.d.ts +5 -0
- package/dist/chat/ChatOptions.d.ts.map +1 -1
- package/dist/chat/ChatResponse.d.ts +5 -0
- package/dist/chat/ChatResponse.d.ts.map +1 -1
- package/dist/chat/ChatResponse.js +12 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/embedding/Embedding.d.ts +17 -0
- package/dist/embedding/Embedding.d.ts.map +1 -0
- package/dist/embedding/Embedding.js +24 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.d.ts +11 -1
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +55 -28
- package/dist/providers/Embedding.d.ts +20 -0
- package/dist/providers/Embedding.d.ts.map +1 -0
- package/dist/providers/Embedding.js +1 -0
- package/dist/providers/Provider.d.ts +11 -4
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/gemini/Capabilities.d.ts +30 -0
- package/dist/providers/gemini/Capabilities.d.ts.map +1 -0
- package/dist/providers/gemini/Capabilities.js +148 -0
- package/dist/providers/gemini/Chat.d.ts +8 -0
- package/dist/providers/gemini/Chat.d.ts.map +1 -0
- package/dist/providers/gemini/Chat.js +69 -0
- package/dist/providers/gemini/ChatUtils.d.ts +9 -0
- package/dist/providers/gemini/ChatUtils.d.ts.map +1 -0
- package/dist/providers/gemini/ChatUtils.js +83 -0
- package/dist/providers/gemini/Embeddings.d.ts +8 -0
- package/dist/providers/gemini/Embeddings.d.ts.map +1 -0
- package/dist/providers/gemini/Embeddings.js +44 -0
- package/dist/providers/gemini/Errors.d.ts +2 -0
- package/dist/providers/gemini/Errors.d.ts.map +1 -0
- package/dist/providers/gemini/Errors.js +34 -0
- package/dist/providers/gemini/GeminiProvider.d.ts +34 -0
- package/dist/providers/gemini/GeminiProvider.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiProvider.js +55 -0
- package/dist/providers/gemini/Image.d.ts +8 -0
- package/dist/providers/gemini/Image.d.ts.map +1 -0
- package/dist/providers/gemini/Image.js +47 -0
- package/dist/providers/gemini/Models.d.ts +8 -0
- package/dist/providers/gemini/Models.d.ts.map +1 -0
- package/dist/providers/gemini/Models.js +38 -0
- package/dist/providers/gemini/Streaming.d.ts +8 -0
- package/dist/providers/gemini/Streaming.d.ts.map +1 -0
- package/dist/providers/gemini/Streaming.js +70 -0
- package/dist/providers/gemini/Transcription.d.ts +9 -0
- package/dist/providers/gemini/Transcription.d.ts.map +1 -0
- package/dist/providers/gemini/Transcription.js +63 -0
- package/dist/providers/gemini/index.d.ts +11 -0
- package/dist/providers/gemini/index.d.ts.map +1 -0
- package/dist/providers/gemini/index.js +24 -0
- package/dist/providers/gemini/types.d.ts +118 -0
- package/dist/providers/gemini/types.d.ts.map +1 -0
- package/dist/providers/gemini/types.js +1 -0
- package/dist/providers/openai/Capabilities.d.ts +7 -2
- package/dist/providers/openai/Capabilities.d.ts.map +1 -1
- package/dist/providers/openai/Capabilities.js +52 -214
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +3 -0
- package/dist/providers/openai/Embedding.d.ts +8 -0
- package/dist/providers/openai/Embedding.d.ts.map +1 -0
- package/dist/providers/openai/Embedding.js +48 -0
- package/dist/providers/openai/ModelDefinitions.d.ts +25 -0
- package/dist/providers/openai/ModelDefinitions.d.ts.map +1 -0
- package/dist/providers/openai/ModelDefinitions.js +211 -0
- package/dist/providers/openai/Moderation.d.ts.map +1 -1
- package/dist/providers/openai/Moderation.js +3 -2
- package/dist/providers/openai/OpenAIProvider.d.ts +7 -0
- package/dist/providers/openai/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/openai/OpenAIProvider.js +10 -0
- package/dist/providers/openai/Transcription.d.ts.map +1 -1
- package/dist/providers/openai/Transcription.js +5 -4
- package/dist/schema/Schema.d.ts +20 -0
- package/dist/schema/Schema.d.ts.map +1 -0
- package/dist/schema/Schema.js +22 -0
- package/dist/schema/to-json-schema.d.ts +3 -0
- package/dist/schema/to-json-schema.d.ts.map +1 -0
- package/dist/schema/to-json-schema.js +10 -0
- package/dist/utils/Binary.d.ts +12 -0
- package/dist/utils/Binary.d.ts.map +1 -0
- package/dist/utils/Binary.js +71 -0
- package/package.json +3 -2
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
export const OPENAI_MODELS = {
|
|
2
|
+
gpt5: {
|
|
3
|
+
pattern: /^gpt-5/,
|
|
4
|
+
contextWindow: 128_000,
|
|
5
|
+
maxOutputTokens: 400_000,
|
|
6
|
+
pricing: { input: 1.25, output: 10.0, cached_input: 0.125 },
|
|
7
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
8
|
+
type: "chat"
|
|
9
|
+
},
|
|
10
|
+
gpt5_mini: {
|
|
11
|
+
pattern: /^gpt-5-mini/,
|
|
12
|
+
contextWindow: 128_000,
|
|
13
|
+
maxOutputTokens: 400_000,
|
|
14
|
+
pricing: { input: 0.25, output: 2.0, cached_input: 0.025 },
|
|
15
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
16
|
+
type: "chat"
|
|
17
|
+
},
|
|
18
|
+
gpt5_nano: {
|
|
19
|
+
pattern: /^gpt-5-nano/,
|
|
20
|
+
contextWindow: 128_000,
|
|
21
|
+
maxOutputTokens: 400_000,
|
|
22
|
+
pricing: { input: 0.05, output: 0.4, cached_input: 0.005 },
|
|
23
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
24
|
+
type: "chat"
|
|
25
|
+
},
|
|
26
|
+
gpt41: {
|
|
27
|
+
pattern: /^gpt-4\.1(?!-(?:mini|nano))/,
|
|
28
|
+
contextWindow: 1_047_576,
|
|
29
|
+
maxOutputTokens: 32_768,
|
|
30
|
+
pricing: { input: 2.0, output: 8.0, cached_input: 0.5 },
|
|
31
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
32
|
+
type: "chat"
|
|
33
|
+
},
|
|
34
|
+
gpt41_mini: {
|
|
35
|
+
pattern: /^gpt-4\.1-mini/,
|
|
36
|
+
contextWindow: 1_047_576,
|
|
37
|
+
maxOutputTokens: 32_768,
|
|
38
|
+
pricing: { input: 0.4, output: 1.6, cached_input: 0.1 },
|
|
39
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
40
|
+
type: "chat"
|
|
41
|
+
},
|
|
42
|
+
gpt41_nano: {
|
|
43
|
+
pattern: /^gpt-4\.1-nano/,
|
|
44
|
+
contextWindow: 1_047_576,
|
|
45
|
+
maxOutputTokens: 32_768,
|
|
46
|
+
pricing: { input: 0.1, output: 0.4 },
|
|
47
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
48
|
+
type: "chat"
|
|
49
|
+
},
|
|
50
|
+
chatgpt4o: {
|
|
51
|
+
pattern: /^chatgpt-4o/,
|
|
52
|
+
contextWindow: 128_000,
|
|
53
|
+
maxOutputTokens: 16_384,
|
|
54
|
+
pricing: { input: 5.0, output: 15.0 },
|
|
55
|
+
features: { vision: true, tools: false, structuredOutput: true },
|
|
56
|
+
type: "chat"
|
|
57
|
+
},
|
|
58
|
+
gpt4: {
|
|
59
|
+
pattern: /^gpt-4(?:-\d{6})?$/,
|
|
60
|
+
contextWindow: 8_192,
|
|
61
|
+
maxOutputTokens: 8_192,
|
|
62
|
+
pricing: { input: 10.0, output: 30.0 },
|
|
63
|
+
features: { vision: true, tools: true, structuredOutput: false },
|
|
64
|
+
type: "chat"
|
|
65
|
+
},
|
|
66
|
+
gpt4_turbo: {
|
|
67
|
+
pattern: /^gpt-4(?:\.5)?-(?:\d{6}-)?(preview|turbo)/,
|
|
68
|
+
contextWindow: 128_000,
|
|
69
|
+
maxOutputTokens: 4_096,
|
|
70
|
+
pricing: { input: 10.0, output: 30.0 },
|
|
71
|
+
features: { vision: true, tools: true, structuredOutput: false },
|
|
72
|
+
type: "chat"
|
|
73
|
+
},
|
|
74
|
+
gpt45: {
|
|
75
|
+
pattern: /^gpt-4\.5/, // Assuming pattern based on name, wasn't explicit in MODEL_PATTERNS but listed in type
|
|
76
|
+
contextWindow: 128_000, // Guessing based on gpt-4-turbo
|
|
77
|
+
maxOutputTokens: 4_096,
|
|
78
|
+
pricing: { input: 75.0, output: 150.0 },
|
|
79
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
80
|
+
type: "chat"
|
|
81
|
+
},
|
|
82
|
+
gpt35_turbo: {
|
|
83
|
+
pattern: /^gpt-3\.5-turbo/,
|
|
84
|
+
contextWindow: 16_385,
|
|
85
|
+
maxOutputTokens: 4_096,
|
|
86
|
+
pricing: { input: 0.5, output: 1.5 },
|
|
87
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
88
|
+
type: "chat"
|
|
89
|
+
},
|
|
90
|
+
gpt4o: {
|
|
91
|
+
pattern: /^gpt-4o(?!-(?:mini|audio|realtime|transcribe|tts|search))/,
|
|
92
|
+
contextWindow: 128_000,
|
|
93
|
+
maxOutputTokens: 16_384,
|
|
94
|
+
pricing: { input: 2.5, output: 10.0 },
|
|
95
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
96
|
+
type: "chat"
|
|
97
|
+
},
|
|
98
|
+
gpt4o_audio: {
|
|
99
|
+
pattern: /^gpt-4o-(?:audio)/,
|
|
100
|
+
contextWindow: 128_000,
|
|
101
|
+
maxOutputTokens: 16_384, // Assuming same as gpt4o
|
|
102
|
+
pricing: { input: 2.5, output: 10.0, audio_input: 40.0, audio_output: 80.0 },
|
|
103
|
+
features: { vision: false, tools: false, structuredOutput: false }, // Check features
|
|
104
|
+
type: "audio"
|
|
105
|
+
},
|
|
106
|
+
gpt4o_mini: {
|
|
107
|
+
pattern: /^gpt-4o-mini(?!-(?:audio|realtime|transcribe|tts|search))/,
|
|
108
|
+
contextWindow: 128_000,
|
|
109
|
+
maxOutputTokens: 16_384,
|
|
110
|
+
pricing: { input: 0.15, output: 0.6 },
|
|
111
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
112
|
+
type: "chat"
|
|
113
|
+
},
|
|
114
|
+
o1: {
|
|
115
|
+
pattern: /^o1(?!-(?:mini|pro))/,
|
|
116
|
+
contextWindow: 200_000,
|
|
117
|
+
maxOutputTokens: 100_000,
|
|
118
|
+
pricing: { input: 15.0, output: 60.0 },
|
|
119
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
120
|
+
type: "chat"
|
|
121
|
+
},
|
|
122
|
+
o1_mini: {
|
|
123
|
+
pattern: /^o1-mini/,
|
|
124
|
+
contextWindow: 128_000,
|
|
125
|
+
maxOutputTokens: 65_536,
|
|
126
|
+
pricing: { input: 1.1, output: 4.4 },
|
|
127
|
+
features: { vision: false, tools: false, structuredOutput: false }, // Check features
|
|
128
|
+
type: "chat"
|
|
129
|
+
},
|
|
130
|
+
o1_pro: {
|
|
131
|
+
pattern: /^o1-pro/,
|
|
132
|
+
contextWindow: 200_000,
|
|
133
|
+
maxOutputTokens: 100_000,
|
|
134
|
+
pricing: { input: 150.0, output: 600.0 },
|
|
135
|
+
features: { vision: true, tools: true, structuredOutput: true },
|
|
136
|
+
type: "chat"
|
|
137
|
+
},
|
|
138
|
+
o3_mini: {
|
|
139
|
+
pattern: /^o3-mini/,
|
|
140
|
+
contextWindow: 200_000,
|
|
141
|
+
maxOutputTokens: 100_000,
|
|
142
|
+
pricing: { input: 1.1, output: 4.4 },
|
|
143
|
+
features: { vision: false, tools: true, structuredOutput: true },
|
|
144
|
+
type: "chat"
|
|
145
|
+
},
|
|
146
|
+
embedding3_small: {
|
|
147
|
+
pattern: /^text-embedding-3-small/,
|
|
148
|
+
contextWindow: null,
|
|
149
|
+
maxOutputTokens: null,
|
|
150
|
+
pricing: { price: 0.02 },
|
|
151
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
152
|
+
type: "embedding"
|
|
153
|
+
},
|
|
154
|
+
embedding3_large: {
|
|
155
|
+
pattern: /^text-embedding-3-large/,
|
|
156
|
+
contextWindow: null,
|
|
157
|
+
maxOutputTokens: null,
|
|
158
|
+
pricing: { price: 0.13 },
|
|
159
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
160
|
+
type: "embedding"
|
|
161
|
+
},
|
|
162
|
+
embedding_ada: {
|
|
163
|
+
pattern: /^text-embedding-ada/,
|
|
164
|
+
contextWindow: null,
|
|
165
|
+
maxOutputTokens: null,
|
|
166
|
+
pricing: { price: 0.10 },
|
|
167
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
168
|
+
type: "embedding"
|
|
169
|
+
},
|
|
170
|
+
moderation: {
|
|
171
|
+
pattern: /^(?:omni|text)-moderation/,
|
|
172
|
+
contextWindow: null,
|
|
173
|
+
maxOutputTokens: null,
|
|
174
|
+
pricing: { price: 0.0 },
|
|
175
|
+
features: { vision: true, tools: false, structuredOutput: false },
|
|
176
|
+
type: "moderation"
|
|
177
|
+
},
|
|
178
|
+
dall_e: {
|
|
179
|
+
pattern: /^dall-e/,
|
|
180
|
+
contextWindow: null,
|
|
181
|
+
maxOutputTokens: null,
|
|
182
|
+
pricing: {}, // Variable
|
|
183
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
184
|
+
type: "image"
|
|
185
|
+
},
|
|
186
|
+
whisper: {
|
|
187
|
+
pattern: /^whisper/,
|
|
188
|
+
contextWindow: null,
|
|
189
|
+
maxOutputTokens: null,
|
|
190
|
+
pricing: { price: 0.006 },
|
|
191
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
192
|
+
type: "audio"
|
|
193
|
+
},
|
|
194
|
+
tts1: {
|
|
195
|
+
pattern: /^tts-1(?!-hd)/,
|
|
196
|
+
contextWindow: null,
|
|
197
|
+
maxOutputTokens: null,
|
|
198
|
+
pricing: { price: 15.0 },
|
|
199
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
200
|
+
type: "audio"
|
|
201
|
+
},
|
|
202
|
+
// Default fallback
|
|
203
|
+
other: {
|
|
204
|
+
pattern: /.*/,
|
|
205
|
+
contextWindow: 4_096,
|
|
206
|
+
maxOutputTokens: 16_384,
|
|
207
|
+
pricing: { input: 0.5, output: 1.5 },
|
|
208
|
+
features: { vision: false, tools: false, structuredOutput: false },
|
|
209
|
+
type: "chat"
|
|
210
|
+
}
|
|
211
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Moderation.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Moderation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Moderation.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Moderation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAIvE,qBAAa,gBAAgB;IACf,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAmBvE"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { handleOpenAIError } from "./Errors.js";
|
|
2
|
+
import { DEFAULT_MODELS } from "../../constants.js";
|
|
2
3
|
export class OpenAIModeration {
|
|
3
4
|
baseUrl;
|
|
4
5
|
apiKey;
|
|
@@ -15,11 +16,11 @@ export class OpenAIModeration {
|
|
|
15
16
|
},
|
|
16
17
|
body: JSON.stringify({
|
|
17
18
|
input: request.input,
|
|
18
|
-
model: request.model ||
|
|
19
|
+
model: request.model || DEFAULT_MODELS.MODERATION,
|
|
19
20
|
}),
|
|
20
21
|
});
|
|
21
22
|
if (!response.ok) {
|
|
22
|
-
await handleOpenAIError(response, request.model ||
|
|
23
|
+
await handleOpenAIError(response, request.model || DEFAULT_MODELS.MODERATION);
|
|
23
24
|
}
|
|
24
25
|
return (await response.json());
|
|
25
26
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Provider, ChatRequest, ChatResponse, ModelInfo, ChatChunk, ImageRequest, ImageResponse, ModerationRequest, ModerationResponse } from "../Provider.js";
|
|
2
2
|
import { TranscriptionRequest, TranscriptionResponse } from "../Provider.js";
|
|
3
|
+
import { EmbeddingRequest, EmbeddingResponse } from "../Embedding.js";
|
|
3
4
|
export interface OpenAIProviderOptions {
|
|
4
5
|
apiKey: string;
|
|
5
6
|
baseUrl?: string;
|
|
@@ -13,10 +14,15 @@ export declare class OpenAIProvider implements Provider {
|
|
|
13
14
|
private readonly imageHandler;
|
|
14
15
|
private readonly transcriptionHandler;
|
|
15
16
|
private readonly moderationHandler;
|
|
17
|
+
private readonly embeddingHandler;
|
|
16
18
|
capabilities: {
|
|
17
19
|
supportsVision: (model: string) => boolean;
|
|
18
20
|
supportsTools: (model: string) => boolean;
|
|
19
21
|
supportsStructuredOutput: (model: string) => boolean;
|
|
22
|
+
supportsEmbeddings: (model: string) => boolean;
|
|
23
|
+
supportsImageGeneration: (model: string) => boolean;
|
|
24
|
+
supportsTranscription: (model: string) => boolean;
|
|
25
|
+
supportsModeration: (model: string) => boolean;
|
|
20
26
|
getContextWindow: (model: string) => number | null;
|
|
21
27
|
};
|
|
22
28
|
constructor(options: OpenAIProviderOptions);
|
|
@@ -26,5 +32,6 @@ export declare class OpenAIProvider implements Provider {
|
|
|
26
32
|
paint(request: ImageRequest): Promise<ImageResponse>;
|
|
27
33
|
transcribe(request: TranscriptionRequest): Promise<TranscriptionResponse>;
|
|
28
34
|
moderate(request: ModerationRequest): Promise<ModerationResponse>;
|
|
35
|
+
embed(request: EmbeddingRequest): Promise<EmbeddingResponse>;
|
|
29
36
|
}
|
|
30
37
|
//# sourceMappingURL=OpenAIProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OpenAIProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/OpenAIProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"OpenAIProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/OpenAIProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAS/J,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,cAAe,YAAW,QAAQ;IAqBjC,OAAO,CAAC,QAAQ,CAAC,OAAO;IApBpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAC3C,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAsB;IAC3D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IAE5C,YAAY;gCACO,MAAM;+BACP,MAAM;0CACK,MAAM;oCACZ,MAAM;yCACD,MAAM;uCACR,MAAM;oCACT,MAAM;kCACR,MAAM;MAChC;gBAE2B,OAAO,EAAE,qBAAqB;IAWrD,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhD,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;IAIxD,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAIlC,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAIpD,UAAU,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAIzE,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIjE,KAAK,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAGnE"}
|
|
@@ -5,6 +5,7 @@ import { OpenAIModels } from "./Models.js";
|
|
|
5
5
|
import { OpenAIImage } from "./Image.js";
|
|
6
6
|
import { OpenAITranscription } from "./Transcription.js";
|
|
7
7
|
import { OpenAIModeration } from "./Moderation.js";
|
|
8
|
+
import { OpenAIEmbedding } from "./Embedding.js";
|
|
8
9
|
export class OpenAIProvider {
|
|
9
10
|
options;
|
|
10
11
|
baseUrl;
|
|
@@ -14,10 +15,15 @@ export class OpenAIProvider {
|
|
|
14
15
|
imageHandler;
|
|
15
16
|
transcriptionHandler;
|
|
16
17
|
moderationHandler;
|
|
18
|
+
embeddingHandler;
|
|
17
19
|
capabilities = {
|
|
18
20
|
supportsVision: (model) => Capabilities.supportsVision(model),
|
|
19
21
|
supportsTools: (model) => Capabilities.supportsTools(model),
|
|
20
22
|
supportsStructuredOutput: (model) => Capabilities.supportsStructuredOutput(model),
|
|
23
|
+
supportsEmbeddings: (model) => Capabilities.supportsEmbeddings(model),
|
|
24
|
+
supportsImageGeneration: (model) => Capabilities.supportsImageGeneration(model),
|
|
25
|
+
supportsTranscription: (model) => Capabilities.supportsTranscription(model),
|
|
26
|
+
supportsModeration: (model) => Capabilities.supportsModeration(model),
|
|
21
27
|
getContextWindow: (model) => Capabilities.getContextWindow(model),
|
|
22
28
|
};
|
|
23
29
|
constructor(options) {
|
|
@@ -29,6 +35,7 @@ export class OpenAIProvider {
|
|
|
29
35
|
this.imageHandler = new OpenAIImage(this.baseUrl, options.apiKey);
|
|
30
36
|
this.transcriptionHandler = new OpenAITranscription(this.baseUrl, options.apiKey);
|
|
31
37
|
this.moderationHandler = new OpenAIModeration(this.baseUrl, options.apiKey);
|
|
38
|
+
this.embeddingHandler = new OpenAIEmbedding(this.baseUrl, options.apiKey);
|
|
32
39
|
}
|
|
33
40
|
async chat(request) {
|
|
34
41
|
return this.chatHandler.execute(request);
|
|
@@ -48,4 +55,7 @@ export class OpenAIProvider {
|
|
|
48
55
|
async moderate(request) {
|
|
49
56
|
return this.moderationHandler.execute(request);
|
|
50
57
|
}
|
|
58
|
+
async embed(request) {
|
|
59
|
+
return this.embeddingHandler.execute(request);
|
|
60
|
+
}
|
|
51
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transcription.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Transcription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Transcription.d.ts","sourceRoot":"","sources":["../../../src/providers/openai/Transcription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAK7E,qBAAa,mBAAmB;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC;YAU9D,oBAAoB;YA4CpB,iBAAiB;CAwHhC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { handleOpenAIError } from "./Errors.js";
|
|
2
2
|
import { AudioUtils } from "../../utils/audio.js";
|
|
3
|
+
import { DEFAULT_MODELS } from "../../constants.js";
|
|
3
4
|
export class OpenAITranscription {
|
|
4
5
|
baseUrl;
|
|
5
6
|
apiKey;
|
|
@@ -8,7 +9,7 @@ export class OpenAITranscription {
|
|
|
8
9
|
this.apiKey = apiKey;
|
|
9
10
|
}
|
|
10
11
|
async execute(request) {
|
|
11
|
-
const model = request.model ||
|
|
12
|
+
const model = request.model || DEFAULT_MODELS.TRANSCRIPTION;
|
|
12
13
|
if (model.startsWith("gpt-4o")) {
|
|
13
14
|
return this.transcribeViaChat(request);
|
|
14
15
|
}
|
|
@@ -20,7 +21,7 @@ export class OpenAITranscription {
|
|
|
20
21
|
const mimeType = fileName.endsWith(".wav") ? "audio/wav" : "audio/mpeg";
|
|
21
22
|
const file = new File([data], fileName, { type: mimeType });
|
|
22
23
|
formData.append("file", file);
|
|
23
|
-
formData.append("model", request.model ||
|
|
24
|
+
formData.append("model", request.model || DEFAULT_MODELS.TRANSCRIPTION);
|
|
24
25
|
formData.append("response_format", "verbose_json");
|
|
25
26
|
if (request.prompt) {
|
|
26
27
|
formData.append("prompt", request.prompt);
|
|
@@ -36,12 +37,12 @@ export class OpenAITranscription {
|
|
|
36
37
|
body: formData,
|
|
37
38
|
});
|
|
38
39
|
if (!response.ok) {
|
|
39
|
-
await handleOpenAIError(response, request.model ||
|
|
40
|
+
await handleOpenAIError(response, request.model || DEFAULT_MODELS.TRANSCRIPTION);
|
|
40
41
|
}
|
|
41
42
|
const json = (await response.json());
|
|
42
43
|
return {
|
|
43
44
|
text: json.text,
|
|
44
|
-
model: json.model || request.model ||
|
|
45
|
+
model: json.model || request.model || DEFAULT_MODELS.TRANSCRIPTION,
|
|
45
46
|
duration: json.duration || estimatedDuration,
|
|
46
47
|
segments: json.segments?.map(s => ({
|
|
47
48
|
id: s.id,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export interface SchemaDefinition {
|
|
3
|
+
name: string;
|
|
4
|
+
schema: z.ZodType<any> | Record<string, any>;
|
|
5
|
+
description?: string;
|
|
6
|
+
strict?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class Schema {
|
|
9
|
+
readonly definition: SchemaDefinition;
|
|
10
|
+
constructor(definition: SchemaDefinition);
|
|
11
|
+
static fromZod(name: string, schema: z.ZodType<any>, options?: {
|
|
12
|
+
description?: string;
|
|
13
|
+
strict?: boolean;
|
|
14
|
+
}): Schema;
|
|
15
|
+
static fromJson(name: string, schema: Record<string, any>, options?: {
|
|
16
|
+
description?: string;
|
|
17
|
+
strict?: boolean;
|
|
18
|
+
}): Schema;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/schema/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,qBAAa,MAAM;aAEC,UAAU,EAAE,gBAAgB;gBAA5B,UAAU,EAAE,gBAAgB;IAG9C,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM;IASlH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM;CAQzH"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class Schema {
|
|
2
|
+
definition;
|
|
3
|
+
constructor(definition) {
|
|
4
|
+
this.definition = definition;
|
|
5
|
+
}
|
|
6
|
+
static fromZod(name, schema, options) {
|
|
7
|
+
return new Schema({
|
|
8
|
+
name,
|
|
9
|
+
schema,
|
|
10
|
+
description: options?.description,
|
|
11
|
+
strict: options?.strict,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
static fromJson(name, schema, options) {
|
|
15
|
+
return new Schema({
|
|
16
|
+
name,
|
|
17
|
+
schema,
|
|
18
|
+
description: options?.description,
|
|
19
|
+
strict: options?.strict,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-json-schema.d.ts","sourceRoot":"","sources":["../../src/schema/to-json-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,wBAAgB,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAO9F"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
|
+
export function toJsonSchema(schema) {
|
|
4
|
+
// If it's a Zod schema, convert it
|
|
5
|
+
if (schema instanceof z.ZodType) {
|
|
6
|
+
return zodToJsonSchema(schema, { target: "openApi3" });
|
|
7
|
+
}
|
|
8
|
+
// If it's already a JSON schema object, return as is
|
|
9
|
+
return schema;
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface Base64Data {
|
|
2
|
+
data: string;
|
|
3
|
+
mimeType: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class BinaryUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a URL (data:, http:, or local path) to a base64 string and mime type.
|
|
8
|
+
*/
|
|
9
|
+
static toBase64(url: string): Promise<Base64Data | null>;
|
|
10
|
+
private static guessMimeType;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=Binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Binary.d.ts","sourceRoot":"","sources":["../../src/utils/Binary.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,WAAW;IACtB;;OAEG;WACU,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IA4C9D,OAAO,CAAC,MAAM,CAAC,aAAa;CAgB7B"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
export class BinaryUtils {
|
|
4
|
+
/**
|
|
5
|
+
* Converts a URL (data:, http:, or local path) to a base64 string and mime type.
|
|
6
|
+
*/
|
|
7
|
+
static async toBase64(url) {
|
|
8
|
+
if (url.startsWith("data:")) {
|
|
9
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
10
|
+
if (match && match[1] && match[2]) {
|
|
11
|
+
return {
|
|
12
|
+
mimeType: match[1],
|
|
13
|
+
data: match[2],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else if (url.startsWith("http")) {
|
|
18
|
+
try {
|
|
19
|
+
const response = await fetch(url);
|
|
20
|
+
if (!response.ok)
|
|
21
|
+
return null;
|
|
22
|
+
const buffer = await response.arrayBuffer();
|
|
23
|
+
const base64 = Buffer.from(buffer).toString("base64");
|
|
24
|
+
const mimeType = response.headers.get("content-type") || this.guessMimeType(url);
|
|
25
|
+
return {
|
|
26
|
+
mimeType,
|
|
27
|
+
data: base64,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
console.error("Error converting URL to base64:", e);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Assume local file path
|
|
37
|
+
try {
|
|
38
|
+
if (fs.existsSync(url)) {
|
|
39
|
+
const buffer = fs.readFileSync(url);
|
|
40
|
+
const base64 = buffer.toString("base64");
|
|
41
|
+
const mimeType = this.guessMimeType(url);
|
|
42
|
+
return {
|
|
43
|
+
mimeType,
|
|
44
|
+
data: base64,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
console.error("Error reading local file for base64:", e);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
static guessMimeType(filePath) {
|
|
56
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
57
|
+
switch (ext) {
|
|
58
|
+
case ".png": return "image/png";
|
|
59
|
+
case ".jpg":
|
|
60
|
+
case ".jpeg": return "image/jpeg";
|
|
61
|
+
case ".webp": return "image/webp";
|
|
62
|
+
case ".gif": return "image/gif";
|
|
63
|
+
case ".mp3": return "audio/mpeg";
|
|
64
|
+
case ".wav": return "audio/wav";
|
|
65
|
+
case ".ogg": return "audio/ogg";
|
|
66
|
+
case ".m4a": return "audio/mp4";
|
|
67
|
+
case ".pdf": return "application/pdf";
|
|
68
|
+
default: return "application/octet-stream";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-llm/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"README.md"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"zod": "^3.23.8"
|
|
24
|
+
"zod": "^3.23.8",
|
|
25
|
+
"zod-to-json-schema": "^3.25.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@pollyjs/adapter-fetch": "^6.0.7",
|