Skip to content

Commit 403acac

Browse files
authored
#344 Upgrading the openai package to v4 (#383)
* #344 Upgrading the openai package to v4 * Create neat-roses-wait.md * Fixed a couple of issues with the openai v4 upgrade * Added fine tuning job tasks
1 parent 708ebbd commit 403acac

File tree

7 files changed

+394
-262
lines changed

7 files changed

+394
-262
lines changed

.changeset/neat-roses-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/openai": patch
3+
---
4+
5+
#344 Upgrading the openai package to v4

examples/job-catalog/src/openai.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ client.defineJob({
2828
run: async (payload, io, ctx) => {
2929
const models = await io.openai.listModels("list-models");
3030

31-
if (models.data.length > 0) {
31+
if (models.length > 0) {
3232
await io.openai.retrieveModel("get-model", {
33-
model: models.data[0].id,
33+
model: models[0].id,
3434
});
3535
}
3636

integrations/openai/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
"build:tsup": "tsup"
2525
},
2626
"dependencies": {
27-
"openai": "^3.3.0",
27+
"openai": "^4.2.0",
2828
"@trigger.dev/sdk": "workspace:^2.0.10",
29-
"@trigger.dev/integration-kit": "workspace:^2.0.10",
30-
"zod": "3.21.4"
29+
"@trigger.dev/integration-kit": "workspace:^2.0.10"
3130
},
3231
"engines": {
3332
"node": ">=16.8.0"
3433
}
35-
}
34+
}

integrations/openai/src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IntegrationClient, TriggerIntegration } from "@trigger.dev/sdk";
2-
import { Configuration, OpenAIApi } from "openai";
2+
import OpenAIApi from "openai";
33
import * as tasks from "./tasks";
44
import { OpenAIIntegrationOptions } from "./types";
55

@@ -28,12 +28,10 @@ export class OpenAI implements TriggerIntegration<IntegrationClient<OpenAIApi, t
2828
throw `Can't create OpenAI integration (${options.id}) as apiKey was undefined`;
2929
}
3030

31-
this.native = new OpenAIApi(
32-
new Configuration({
33-
apiKey: options.apiKey,
34-
organization: options.organization,
35-
})
36-
);
31+
this.native = new OpenAIApi({
32+
apiKey: options.apiKey,
33+
organization: options.organization,
34+
});
3735

3836
this.client = {
3937
tasks,

integrations/openai/src/taskUtils.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { CreateCompletionResponseUsage, CreateEmbeddingResponseUsage } from "openai";
2-
import { z } from "zod";
1+
import OpenAI from "openai";
32

43
export function createTaskUsageProperties(
5-
usage: CreateCompletionResponseUsage | CreateEmbeddingResponseUsage | undefined
4+
usage: OpenAI.Completions.CompletionUsage | OpenAI.CreateEmbeddingResponse.Usage | undefined
65
) {
76
if (!usage) {
87
return;
@@ -28,26 +27,6 @@ export function createTaskUsageProperties(
2827
];
2928
}
3029

31-
const OpenAIErrorSchema = z.object({
32-
response: z.object({
33-
data: z.object({
34-
error: z.object({
35-
code: z.string().nullable().optional(),
36-
message: z.string(),
37-
type: z.string(),
38-
}),
39-
}),
40-
}),
41-
});
42-
4330
export function onTaskError(error: unknown) {
44-
const openAIError = OpenAIErrorSchema.safeParse(error);
45-
46-
if (!openAIError.success) {
47-
return;
48-
}
49-
50-
const { message, code, type } = openAIError.data.response.data.error;
51-
52-
return new Error(`${type}: ${message}${code ? ` (${code})` : ""}`);
31+
return;
5332
}

0 commit comments

Comments
 (0)