| 24 | export type OpenAIRunTask = InstanceType<typeof OpenAI>["runTask"]; |
| 25 | |
| 26 | export class OpenAI implements TriggerIntegration { |
| 27 | // @internal |
| 28 | private _options: OpenAIIntegrationOptions; |
| 29 | // @internal |
| 30 | private _client?: OpenAIApi; |
| 31 | // @internal |
| 32 | private _io?: IO; |
| 33 | // @internal |
| 34 | private _connectionKey?: string; |
| 35 | |
| 36 | /** |
| 37 | * The native OpenAIApi client. This is exposed for use outside of Trigger.dev jobs |
| 38 | * |
| 39 | * @example |
| 40 | * ```ts |
| 41 | * import { OpenAI } from "@trigger.dev/openai"; |
| 42 | * |
| 43 | * const openAI = new OpenAI({ |
| 44 | * id: "my-openai", |
| 45 | * apiKey: process.env.OPENAI_API_KEY!, |
| 46 | * }); |
| 47 | * |
| 48 | * const response = await openAI.native.completions.create({}); // ... |
| 49 | * ``` |
| 50 | */ |
| 51 | public readonly native: OpenAIApi; |
| 52 | |
| 53 | constructor(private options: OpenAIIntegrationOptions) { |
| 54 | this._options = options; |
| 55 | |
| 56 | this.native = new OpenAIApi({ |
| 57 | apiKey: options.apiKey, |
| 58 | organization: options.organization, |
| 59 | baseURL: options.baseURL, |
| 60 | defaultHeaders: options.defaultHeaders, |
| 61 | defaultQuery: options.defaultQuery, |
| 62 | maxRetries: 0, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | get authSource() { |
| 67 | return "LOCAL" as const; |
| 68 | } |
| 69 | |
| 70 | cloneForRun(io: IO, connectionKey: string, auth?: ConnectionAuth) { |
| 71 | const apiKey = this._options.apiKey ?? auth?.accessToken; |
| 72 | |
| 73 | if (!apiKey) { |
| 74 | throw new Error( |
| 75 | `Can't initialize OpenAI integration (${this._options.id}) as apiKey was undefined` |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | const openai = new OpenAI(this._options); |
| 80 | openai._io = io; |
| 81 | openai._connectionKey = connectionKey; |
| 82 | openai._client = new OpenAIApi({ |
| 83 | apiKey, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…