@core-ai/openai 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 +26 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +422 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Omnifact (https://omnifact.ai)
|
|
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,26 @@
|
|
|
1
|
+
# @core-ai/openai
|
|
2
|
+
|
|
3
|
+
OpenAI provider package for `@core-ai/core-ai`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @core-ai/core-ai @core-ai/openai zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { generate } from '@core-ai/core-ai';
|
|
15
|
+
import { createOpenAI } from '@core-ai/openai';
|
|
16
|
+
|
|
17
|
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
18
|
+
const model = openai.chatModel('gpt-5-mini');
|
|
19
|
+
|
|
20
|
+
const result = await generate({
|
|
21
|
+
model,
|
|
22
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
console.log(result.content);
|
|
26
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import { ChatModel, EmbeddingModel, ImageModel } from '@core-ai/core-ai';
|
|
3
|
+
|
|
4
|
+
type OpenAIProviderOptions = {
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
baseURL?: string;
|
|
7
|
+
client?: OpenAI;
|
|
8
|
+
};
|
|
9
|
+
type OpenAIProvider = {
|
|
10
|
+
chatModel(modelId: string): ChatModel;
|
|
11
|
+
embeddingModel(modelId: string): EmbeddingModel;
|
|
12
|
+
imageModel(modelId: string): ImageModel;
|
|
13
|
+
};
|
|
14
|
+
declare function createOpenAI(options?: OpenAIProviderOptions): OpenAIProvider;
|
|
15
|
+
|
|
16
|
+
export { type OpenAIProvider, type OpenAIProviderOptions, createOpenAI };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
// src/provider.ts
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
|
|
4
|
+
// src/chat-model.ts
|
|
5
|
+
import { createStreamResult } from "@core-ai/core-ai";
|
|
6
|
+
|
|
7
|
+
// src/chat-adapter.ts
|
|
8
|
+
import { APIError } from "openai";
|
|
9
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
10
|
+
import { ProviderError } from "@core-ai/core-ai";
|
|
11
|
+
function convertMessages(messages) {
|
|
12
|
+
return messages.map(convertMessage);
|
|
13
|
+
}
|
|
14
|
+
function convertMessage(message) {
|
|
15
|
+
if (message.role === "system") {
|
|
16
|
+
return {
|
|
17
|
+
role: "system",
|
|
18
|
+
content: message.content
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (message.role === "user") {
|
|
22
|
+
return {
|
|
23
|
+
role: "user",
|
|
24
|
+
content: typeof message.content === "string" ? message.content : message.content.map(convertUserContentPart)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (message.role === "assistant") {
|
|
28
|
+
return {
|
|
29
|
+
role: "assistant",
|
|
30
|
+
content: message.content,
|
|
31
|
+
...message.toolCalls && message.toolCalls.length > 0 ? {
|
|
32
|
+
tool_calls: message.toolCalls.map((toolCall) => ({
|
|
33
|
+
id: toolCall.id,
|
|
34
|
+
type: "function",
|
|
35
|
+
function: {
|
|
36
|
+
name: toolCall.name,
|
|
37
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
38
|
+
}
|
|
39
|
+
}))
|
|
40
|
+
} : {}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
role: "tool",
|
|
45
|
+
tool_call_id: message.toolCallId,
|
|
46
|
+
content: message.content
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function convertUserContentPart(part) {
|
|
50
|
+
if (part.type === "text") {
|
|
51
|
+
return {
|
|
52
|
+
type: "text",
|
|
53
|
+
text: part.text
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (part.type === "image") {
|
|
57
|
+
const url = part.source.type === "url" ? part.source.url : `data:${part.source.mediaType};base64,${part.source.data}`;
|
|
58
|
+
return {
|
|
59
|
+
type: "image_url",
|
|
60
|
+
image_url: {
|
|
61
|
+
url
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: "file",
|
|
67
|
+
file: {
|
|
68
|
+
file_data: part.data,
|
|
69
|
+
...part.filename ? { filename: part.filename } : {}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function convertTools(tools) {
|
|
74
|
+
return Object.values(tools).map((tool) => ({
|
|
75
|
+
type: "function",
|
|
76
|
+
function: {
|
|
77
|
+
name: tool.name,
|
|
78
|
+
description: tool.description,
|
|
79
|
+
parameters: zodToJsonSchema(tool.parameters)
|
|
80
|
+
}
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
function convertToolChoice(choice) {
|
|
84
|
+
if (typeof choice === "string") {
|
|
85
|
+
return choice;
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
type: "function",
|
|
89
|
+
function: {
|
|
90
|
+
name: choice.toolName
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function createGenerateRequest(modelId, options) {
|
|
95
|
+
return {
|
|
96
|
+
model: modelId,
|
|
97
|
+
messages: convertMessages(options.messages),
|
|
98
|
+
...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
|
|
99
|
+
...options.toolChoice ? { tool_choice: convertToolChoice(options.toolChoice) } : {},
|
|
100
|
+
...options.config?.temperature !== void 0 ? { temperature: options.config.temperature } : {},
|
|
101
|
+
...options.config?.maxTokens !== void 0 ? { max_tokens: options.config.maxTokens } : {},
|
|
102
|
+
...options.config?.topP !== void 0 ? { top_p: options.config.topP } : {},
|
|
103
|
+
...options.config?.stopSequences ? { stop: options.config.stopSequences } : {},
|
|
104
|
+
...options.config?.frequencyPenalty !== void 0 ? { frequency_penalty: options.config.frequencyPenalty } : {},
|
|
105
|
+
...options.config?.presencePenalty !== void 0 ? { presence_penalty: options.config.presencePenalty } : {},
|
|
106
|
+
...options.providerOptions
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function createStreamRequest(modelId, options) {
|
|
110
|
+
return {
|
|
111
|
+
model: modelId,
|
|
112
|
+
messages: convertMessages(options.messages),
|
|
113
|
+
stream: true,
|
|
114
|
+
stream_options: {
|
|
115
|
+
include_usage: true
|
|
116
|
+
},
|
|
117
|
+
...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
|
|
118
|
+
...options.toolChoice ? { tool_choice: convertToolChoice(options.toolChoice) } : {},
|
|
119
|
+
...options.config?.temperature !== void 0 ? { temperature: options.config.temperature } : {},
|
|
120
|
+
...options.config?.maxTokens !== void 0 ? { max_tokens: options.config.maxTokens } : {},
|
|
121
|
+
...options.config?.topP !== void 0 ? { top_p: options.config.topP } : {},
|
|
122
|
+
...options.config?.stopSequences ? { stop: options.config.stopSequences } : {},
|
|
123
|
+
...options.config?.frequencyPenalty !== void 0 ? { frequency_penalty: options.config.frequencyPenalty } : {},
|
|
124
|
+
...options.config?.presencePenalty !== void 0 ? { presence_penalty: options.config.presencePenalty } : {},
|
|
125
|
+
...options.providerOptions
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function mapGenerateResponse(response) {
|
|
129
|
+
const firstChoice = response.choices[0];
|
|
130
|
+
if (!firstChoice) {
|
|
131
|
+
return {
|
|
132
|
+
content: null,
|
|
133
|
+
toolCalls: [],
|
|
134
|
+
finishReason: "unknown",
|
|
135
|
+
usage: {
|
|
136
|
+
inputTokens: 0,
|
|
137
|
+
outputTokens: 0,
|
|
138
|
+
reasoningTokens: 0,
|
|
139
|
+
totalTokens: 0
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const reasoningTokens = response.usage?.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
144
|
+
return {
|
|
145
|
+
content: firstChoice.message.content,
|
|
146
|
+
toolCalls: parseToolCalls(firstChoice.message.tool_calls),
|
|
147
|
+
finishReason: mapFinishReason(firstChoice.finish_reason),
|
|
148
|
+
usage: {
|
|
149
|
+
inputTokens: response.usage?.prompt_tokens ?? 0,
|
|
150
|
+
outputTokens: response.usage?.completion_tokens ?? 0,
|
|
151
|
+
reasoningTokens,
|
|
152
|
+
totalTokens: response.usage?.total_tokens ?? 0
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function parseToolCalls(calls) {
|
|
157
|
+
if (!calls) {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
return calls.flatMap((toolCall) => {
|
|
161
|
+
if (toolCall.type !== "function") {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
return [mapFunctionToolCall(toolCall)];
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function mapFunctionToolCall(toolCall) {
|
|
168
|
+
return {
|
|
169
|
+
id: toolCall.id,
|
|
170
|
+
name: toolCall.function.name,
|
|
171
|
+
arguments: safeParseJsonObject(toolCall.function.arguments)
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function mapFinishReason(reason) {
|
|
175
|
+
if (reason === "stop") {
|
|
176
|
+
return "stop";
|
|
177
|
+
}
|
|
178
|
+
if (reason === "length") {
|
|
179
|
+
return "length";
|
|
180
|
+
}
|
|
181
|
+
if (reason === "tool_calls" || reason === "function_call") {
|
|
182
|
+
return "tool-calls";
|
|
183
|
+
}
|
|
184
|
+
if (reason === "content_filter") {
|
|
185
|
+
return "content-filter";
|
|
186
|
+
}
|
|
187
|
+
return "unknown";
|
|
188
|
+
}
|
|
189
|
+
async function* transformStream(stream) {
|
|
190
|
+
const bufferedToolCalls = /* @__PURE__ */ new Map();
|
|
191
|
+
const emittedToolCalls = /* @__PURE__ */ new Set();
|
|
192
|
+
let finishReason = "unknown";
|
|
193
|
+
let usage = {
|
|
194
|
+
inputTokens: 0,
|
|
195
|
+
outputTokens: 0,
|
|
196
|
+
reasoningTokens: 0,
|
|
197
|
+
totalTokens: 0
|
|
198
|
+
};
|
|
199
|
+
for await (const chunk of stream) {
|
|
200
|
+
if (chunk.usage) {
|
|
201
|
+
usage = {
|
|
202
|
+
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
|
203
|
+
outputTokens: chunk.usage.completion_tokens ?? 0,
|
|
204
|
+
reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
205
|
+
totalTokens: chunk.usage.total_tokens ?? 0
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const choice = chunk.choices[0];
|
|
209
|
+
if (!choice) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (choice.delta.content) {
|
|
213
|
+
yield {
|
|
214
|
+
type: "content-delta",
|
|
215
|
+
text: choice.delta.content
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (choice.delta.tool_calls) {
|
|
219
|
+
for (const partialToolCall of choice.delta.tool_calls) {
|
|
220
|
+
const current = bufferedToolCalls.get(partialToolCall.index) ?? {
|
|
221
|
+
id: partialToolCall.id ?? `tool-${partialToolCall.index}`,
|
|
222
|
+
name: partialToolCall.function?.name ?? "",
|
|
223
|
+
arguments: ""
|
|
224
|
+
};
|
|
225
|
+
const wasNew = !bufferedToolCalls.has(partialToolCall.index);
|
|
226
|
+
if (partialToolCall.id) {
|
|
227
|
+
current.id = partialToolCall.id;
|
|
228
|
+
}
|
|
229
|
+
if (partialToolCall.function?.name) {
|
|
230
|
+
current.name = partialToolCall.function.name;
|
|
231
|
+
}
|
|
232
|
+
if (partialToolCall.function?.arguments) {
|
|
233
|
+
current.arguments += partialToolCall.function.arguments;
|
|
234
|
+
yield {
|
|
235
|
+
type: "tool-call-delta",
|
|
236
|
+
toolCallId: current.id,
|
|
237
|
+
argumentsDelta: partialToolCall.function.arguments
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
bufferedToolCalls.set(partialToolCall.index, current);
|
|
241
|
+
if (wasNew) {
|
|
242
|
+
yield {
|
|
243
|
+
type: "tool-call-start",
|
|
244
|
+
toolCallId: current.id,
|
|
245
|
+
toolName: current.name
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (choice.finish_reason) {
|
|
251
|
+
finishReason = mapFinishReason(choice.finish_reason);
|
|
252
|
+
}
|
|
253
|
+
if (finishReason === "tool-calls") {
|
|
254
|
+
for (const toolCall of bufferedToolCalls.values()) {
|
|
255
|
+
if (emittedToolCalls.has(toolCall.id)) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
emittedToolCalls.add(toolCall.id);
|
|
259
|
+
yield {
|
|
260
|
+
type: "tool-call-end",
|
|
261
|
+
toolCall: {
|
|
262
|
+
id: toolCall.id,
|
|
263
|
+
name: toolCall.name,
|
|
264
|
+
arguments: safeParseJsonObject(toolCall.arguments)
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
yield {
|
|
271
|
+
type: "finish",
|
|
272
|
+
finishReason,
|
|
273
|
+
usage
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function safeParseJsonObject(json) {
|
|
277
|
+
try {
|
|
278
|
+
const parsed = JSON.parse(json);
|
|
279
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
280
|
+
return parsed;
|
|
281
|
+
}
|
|
282
|
+
return {};
|
|
283
|
+
} catch {
|
|
284
|
+
return {};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function wrapError(error) {
|
|
288
|
+
if (error instanceof APIError) {
|
|
289
|
+
return new ProviderError(error.message, "openai", error.status, error);
|
|
290
|
+
}
|
|
291
|
+
return new ProviderError(
|
|
292
|
+
error instanceof Error ? error.message : String(error),
|
|
293
|
+
"openai",
|
|
294
|
+
void 0,
|
|
295
|
+
error
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/chat-model.ts
|
|
300
|
+
function createOpenAIChatModel(client, modelId) {
|
|
301
|
+
return {
|
|
302
|
+
provider: "openai",
|
|
303
|
+
modelId,
|
|
304
|
+
async generate(options) {
|
|
305
|
+
try {
|
|
306
|
+
const request = createGenerateRequest(modelId, options);
|
|
307
|
+
const response = await client.chat.completions.create(request);
|
|
308
|
+
return mapGenerateResponse(response);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
throw wrapError(error);
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
async stream(options) {
|
|
314
|
+
try {
|
|
315
|
+
const request = createStreamRequest(modelId, options);
|
|
316
|
+
const stream = await client.chat.completions.create(request);
|
|
317
|
+
return createStreamResult(transformStream(stream));
|
|
318
|
+
} catch (error) {
|
|
319
|
+
throw wrapError(error);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/embedding-model.ts
|
|
326
|
+
import { APIError as APIError2 } from "openai";
|
|
327
|
+
import { ProviderError as ProviderError2 } from "@core-ai/core-ai";
|
|
328
|
+
function createOpenAIEmbeddingModel(client, modelId) {
|
|
329
|
+
return {
|
|
330
|
+
provider: "openai",
|
|
331
|
+
modelId,
|
|
332
|
+
async embed(options) {
|
|
333
|
+
try {
|
|
334
|
+
const response = await client.embeddings.create({
|
|
335
|
+
model: modelId,
|
|
336
|
+
input: options.input,
|
|
337
|
+
...options.dimensions !== void 0 ? { dimensions: options.dimensions } : {},
|
|
338
|
+
...options.providerOptions
|
|
339
|
+
});
|
|
340
|
+
return {
|
|
341
|
+
embeddings: response.data.slice().sort((a, b) => a.index - b.index).map((item) => item.embedding),
|
|
342
|
+
usage: {
|
|
343
|
+
inputTokens: response.usage.prompt_tokens
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
} catch (error) {
|
|
347
|
+
throw wrapError2(error);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function wrapError2(error) {
|
|
353
|
+
if (error instanceof APIError2) {
|
|
354
|
+
return new ProviderError2(error.message, "openai", error.status, error);
|
|
355
|
+
}
|
|
356
|
+
return new ProviderError2(
|
|
357
|
+
error instanceof Error ? error.message : String(error),
|
|
358
|
+
"openai",
|
|
359
|
+
void 0,
|
|
360
|
+
error
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/image-model.ts
|
|
365
|
+
import { APIError as APIError3 } from "openai";
|
|
366
|
+
import { ProviderError as ProviderError3 } from "@core-ai/core-ai";
|
|
367
|
+
function createOpenAIImageModel(client, modelId) {
|
|
368
|
+
return {
|
|
369
|
+
provider: "openai",
|
|
370
|
+
modelId,
|
|
371
|
+
async generate(options) {
|
|
372
|
+
try {
|
|
373
|
+
const request = {
|
|
374
|
+
model: modelId,
|
|
375
|
+
prompt: options.prompt,
|
|
376
|
+
...options.n !== void 0 ? { n: options.n } : {},
|
|
377
|
+
...options.size !== void 0 ? { size: options.size } : {},
|
|
378
|
+
...options.providerOptions
|
|
379
|
+
};
|
|
380
|
+
const response = await client.images.generate(
|
|
381
|
+
request
|
|
382
|
+
);
|
|
383
|
+
return {
|
|
384
|
+
images: (response.data ?? []).map((image) => ({
|
|
385
|
+
base64: image.b64_json ?? void 0,
|
|
386
|
+
url: image.url ?? void 0,
|
|
387
|
+
revisedPrompt: image.revised_prompt ?? void 0
|
|
388
|
+
}))
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
throw wrapError3(error);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function wrapError3(error) {
|
|
397
|
+
if (error instanceof APIError3) {
|
|
398
|
+
return new ProviderError3(error.message, "openai", error.status, error);
|
|
399
|
+
}
|
|
400
|
+
return new ProviderError3(
|
|
401
|
+
error instanceof Error ? error.message : String(error),
|
|
402
|
+
"openai",
|
|
403
|
+
void 0,
|
|
404
|
+
error
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/provider.ts
|
|
409
|
+
function createOpenAI(options = {}) {
|
|
410
|
+
const client = options.client ?? new OpenAI({
|
|
411
|
+
apiKey: options.apiKey,
|
|
412
|
+
baseURL: options.baseURL
|
|
413
|
+
});
|
|
414
|
+
return {
|
|
415
|
+
chatModel: (modelId) => createOpenAIChatModel(client, modelId),
|
|
416
|
+
embeddingModel: (modelId) => createOpenAIEmbeddingModel(client, modelId),
|
|
417
|
+
imageModel: (modelId) => createOpenAIImageModel(client, modelId)
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
export {
|
|
421
|
+
createOpenAI
|
|
422
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@core-ai/openai",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenAI provider package for @core-ai/core-ai",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Omnifact (https://omnifact.ai)",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/agdevhq/ai-core.git",
|
|
10
|
+
"directory": "packages/openai"
|
|
11
|
+
},
|
|
12
|
+
"keywords": ["llm", "ai", "openai", "provider", "sdk"],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"lint": "eslint src/ --max-warnings 0",
|
|
29
|
+
"check-types": "tsc --noEmit",
|
|
30
|
+
"prepublishOnly": "npm run build",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:watch": "vitest"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@core-ai/core-ai": "^0.1.0",
|
|
36
|
+
"openai": "^6.1.0",
|
|
37
|
+
"zod-to-json-schema": "^3.24.5"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"zod": "^3.25.76"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@core-ai/eslint-config": "^0.0.0",
|
|
44
|
+
"@core-ai/typescript-config": "^0.0.0",
|
|
45
|
+
"typescript": "^5.7.3",
|
|
46
|
+
"vitest": "^3.2.4"
|
|
47
|
+
}
|
|
48
|
+
}
|