Skip to content

Commit 356df1a

Browse files
ruudandriessenAlemTuzlakautofix-ci[bot]
authored
feat(ai-openai): allow passing all open ai configuration options (#245)
* feat: allow passing all open ai configuration options * remove barrel file and export model types * ci: apply automated fixes --------- Co-authored-by: Alem Tuzlak <t.zlak@hotmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent b4f44fb commit 356df1a

11 files changed

Lines changed: 49 additions & 55 deletions

File tree

.changeset/olive-wolves-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/ai-openai': minor
3+
---
4+
5+
allows additional configuration options when creating an openAI client

packages/typescript/ai-openai/src/adapters/image.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {
33
createOpenAIClient,
44
generateId,
55
getOpenAIApiKeyFromEnv,
6-
} from '../utils'
6+
} from '../utils/client'
77
import {
88
validateImageSize,
99
validateNumberOfImages,
1010
validatePrompt,
1111
} from '../image/image-provider-options'
12-
import type { OPENAI_IMAGE_MODELS } from '../model-meta'
12+
import type { OpenAIImageModel } from '../model-meta'
1313
import type {
1414
OpenAIImageModelProviderOptionsByName,
1515
OpenAIImageModelSizeByName,
@@ -21,16 +21,13 @@ import type {
2121
ImageGenerationResult,
2222
} from '@tanstack/ai'
2323
import type OpenAI_SDK from 'openai'
24-
import type { OpenAIClientConfig } from '../utils'
24+
import type { OpenAIClientConfig } from '../utils/client'
2525

2626
/**
2727
* Configuration for OpenAI image adapter
2828
*/
2929
export interface OpenAIImageConfig extends OpenAIClientConfig {}
3030

31-
/** Model type for OpenAI Image */
32-
export type OpenAIImageModel = (typeof OPENAI_IMAGE_MODELS)[number]
33-
3431
/**
3532
* OpenAI Image Generation Adapter
3633
*

packages/typescript/ai-openai/src/adapters/summarize.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'
2-
import { getOpenAIApiKeyFromEnv } from '../utils'
2+
import { getOpenAIApiKeyFromEnv } from '../utils/client'
33
import { OpenAITextAdapter } from './text'
4-
import type { OPENAI_CHAT_MODELS } from '../model-meta'
4+
import type { OpenAIChatModel } from '../model-meta'
55
import type {
66
StreamChunk,
77
SummarizationOptions,
88
SummarizationResult,
99
} from '@tanstack/ai'
10-
import type { OpenAIClientConfig } from '../utils'
10+
import type { OpenAIClientConfig } from '../utils/client'
1111

1212
/**
1313
* Configuration for OpenAI summarize adapter
@@ -24,17 +24,14 @@ export interface OpenAISummarizeProviderOptions {
2424
maxTokens?: number
2525
}
2626

27-
/** Model type for OpenAI summarization */
28-
export type OpenAISummarizeModel = (typeof OPENAI_CHAT_MODELS)[number]
29-
3027
/**
3128
* OpenAI Summarize Adapter
3229
*
3330
* A thin wrapper around the text adapter that adds summarization-specific prompting.
3431
* Delegates all API calls to the OpenAITextAdapter.
3532
*/
3633
export class OpenAISummarizeAdapter<
37-
TModel extends OpenAISummarizeModel,
34+
TModel extends OpenAIChatModel,
3835
> extends BaseSummarizeAdapter<TModel, OpenAISummarizeProviderOptions> {
3936
readonly kind = 'summarize' as const
4037
readonly name = 'openai' as const
@@ -133,7 +130,7 @@ export class OpenAISummarizeAdapter<
133130
* const adapter = createOpenaiSummarize('gpt-4o-mini', "sk-...");
134131
* ```
135132
*/
136-
export function createOpenaiSummarize<TModel extends OpenAISummarizeModel>(
133+
export function createOpenaiSummarize<TModel extends OpenAIChatModel>(
137134
model: TModel,
138135
apiKey: string,
139136
config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
@@ -165,7 +162,7 @@ export function createOpenaiSummarize<TModel extends OpenAISummarizeModel>(
165162
* });
166163
* ```
167164
*/
168-
export function openaiSummarize<TModel extends OpenAISummarizeModel>(
165+
export function openaiSummarize<TModel extends OpenAIChatModel>(
169166
model: TModel,
170167
config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
171168
): OpenAISummarizeAdapter<TModel> {

packages/typescript/ai-openai/src/adapters/text.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import {
55
createOpenAIClient,
66
generateId,
77
getOpenAIApiKeyFromEnv,
8+
} from '../utils/client'
9+
import {
810
makeOpenAIStructuredOutputCompatible,
911
transformNullsToUndefined,
10-
} from '../utils'
12+
} from '../utils/schema-converter'
1113
import type {
1214
OPENAI_CHAT_MODELS,
15+
OpenAIChatModel,
1316
OpenAIChatModelProviderOptionsByName,
1417
OpenAIModelInputModalitiesByName,
1518
} from '../model-meta'
@@ -34,7 +37,7 @@ import type {
3437
OpenAIImageMetadata,
3538
OpenAIMessageMetadataByModality,
3639
} from '../message-types'
37-
import type { OpenAIClientConfig } from '../utils'
40+
import type { OpenAIClientConfig } from '../utils/client'
3841

3942
/**
4043
* Configuration for OpenAI text adapter
@@ -79,7 +82,7 @@ type ResolveInputModalities<TModel extends string> =
7982
* Import only what you need for smaller bundle sizes.
8083
*/
8184
export class OpenAITextAdapter<
82-
TModel extends (typeof OPENAI_CHAT_MODELS)[number],
85+
TModel extends OpenAIChatModel,
8386
> extends BaseTextAdapter<
8487
TModel,
8588
ResolveProviderOptions<TModel>,

packages/typescript/ai-openai/src/adapters/transcription.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@ import {
33
createOpenAIClient,
44
generateId,
55
getOpenAIApiKeyFromEnv,
6-
} from '../utils'
7-
import type { OPENAI_TRANSCRIPTION_MODELS } from '../model-meta'
6+
} from '../utils/client'
7+
import type { OpenAITranscriptionModel } from '../model-meta'
88
import type { OpenAITranscriptionProviderOptions } from '../audio/transcription-provider-options'
99
import type {
1010
TranscriptionOptions,
1111
TranscriptionResult,
1212
TranscriptionSegment,
1313
} from '@tanstack/ai'
1414
import type OpenAI_SDK from 'openai'
15-
import type { OpenAIClientConfig } from '../utils'
15+
import type { OpenAIClientConfig } from '../utils/client'
1616

1717
/**
1818
* Configuration for OpenAI Transcription adapter
1919
*/
2020
export interface OpenAITranscriptionConfig extends OpenAIClientConfig {}
2121

22-
/** Model type for OpenAI Transcription */
23-
export type OpenAITranscriptionModel =
24-
(typeof OPENAI_TRANSCRIPTION_MODELS)[number]
25-
2622
/**
2723
* OpenAI Transcription (Speech-to-Text) Adapter
2824
*

packages/typescript/ai-openai/src/adapters/tts.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,27 @@ import {
33
createOpenAIClient,
44
generateId,
55
getOpenAIApiKeyFromEnv,
6-
} from '../utils'
6+
} from '../utils/client'
77
import {
88
validateAudioInput,
99
validateInstructions,
1010
validateSpeed,
1111
} from '../audio/audio-provider-options'
12-
import type { OPENAI_TTS_MODELS } from '../model-meta'
12+
import type { OpenAITTSModel } from '../model-meta'
1313
import type {
1414
OpenAITTSFormat,
1515
OpenAITTSProviderOptions,
1616
OpenAITTSVoice,
1717
} from '../audio/tts-provider-options'
1818
import type { TTSOptions, TTSResult } from '@tanstack/ai'
1919
import type OpenAI_SDK from 'openai'
20-
import type { OpenAIClientConfig } from '../utils'
20+
import type { OpenAIClientConfig } from '../utils/client'
2121

2222
/**
2323
* Configuration for OpenAI TTS adapter
2424
*/
2525
export interface OpenAITTSConfig extends OpenAIClientConfig {}
2626

27-
/** Model type for OpenAI TTS */
28-
export type OpenAITTSModel = (typeof OPENAI_TTS_MODELS)[number]
29-
3027
/**
3128
* OpenAI Text-to-Speech Adapter
3229
*

packages/typescript/ai-openai/src/adapters/video.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { BaseVideoAdapter } from '@tanstack/ai/adapters'
2-
import { createOpenAIClient, getOpenAIApiKeyFromEnv } from '../utils'
2+
import { createOpenAIClient, getOpenAIApiKeyFromEnv } from '../utils/client'
33
import {
44
toApiSeconds,
55
validateVideoSeconds,
66
validateVideoSize,
77
} from '../video/video-provider-options'
88
import type { VideoModel } from 'openai/resources'
9-
import type { OPENAI_VIDEO_MODELS } from '../model-meta'
9+
import type { OpenAIVideoModel } from '../model-meta'
1010
import type {
1111
OpenAIVideoModelProviderOptionsByName,
1212
OpenAIVideoProviderOptions,
@@ -18,7 +18,7 @@ import type {
1818
VideoUrlResult,
1919
} from '@tanstack/ai'
2020
import type OpenAI_SDK from 'openai'
21-
import type { OpenAIClientConfig } from '../utils'
21+
import type { OpenAIClientConfig } from '../utils/client'
2222

2323
/**
2424
* Configuration for OpenAI video adapter.
@@ -27,9 +27,6 @@ import type { OpenAIClientConfig } from '../utils'
2727
*/
2828
export interface OpenAIVideoConfig extends OpenAIClientConfig {}
2929

30-
/** Model type for OpenAI Video */
31-
export type OpenAIVideoModel = (typeof OPENAI_VIDEO_MODELS)[number]
32-
3330
/**
3431
* OpenAI Video Generation Adapter
3532
*

packages/typescript/ai-openai/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export type { OpenAITranscriptionProviderOptions } from './audio/transcription-p
7878
export type {
7979
OpenAIChatModelProviderOptionsByName,
8080
OpenAIModelInputModalitiesByName,
81+
OpenAIChatModel,
82+
OpenAIImageModel,
83+
OpenAIVideoModel,
84+
OpenAITTSModel,
85+
OpenAITranscriptionModel,
8186
} from './model-meta'
8287
export {
8388
OPENAI_IMAGE_MODELS,
@@ -94,3 +99,4 @@ export type {
9499
OpenAIDocumentMetadata,
95100
OpenAIMessageMetadataByModality,
96101
} from './message-types'
102+
export type { OpenAIClientConfig } from './utils/client'

packages/typescript/ai-openai/src/model-meta.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,8 @@ export const OPENAI_CHAT_MODELS = [
16961696
O1_PRO.name,
16971697
] as const
16981698

1699+
export type OpenAIChatModel = (typeof OPENAI_CHAT_MODELS)[number]
1700+
16991701
// Image generation models (based on endpoints: "image-generation" or "image-edit")
17001702
export const OPENAI_IMAGE_MODELS = [
17011703
GPT_IMAGE_1.name,
@@ -1704,6 +1706,8 @@ export const OPENAI_IMAGE_MODELS = [
17041706
DALL_E_2.name,
17051707
] as const
17061708

1709+
export type OpenAIImageModel = (typeof OPENAI_IMAGE_MODELS)[number]
1710+
17071711
// Audio models (based on endpoints: "transcription", "speech_generation", or "realtime")
17081712
/* const OPENAI_AUDIO_MODELS = [
17091713
// Transcription models
@@ -1734,6 +1738,8 @@ export const OPENAI_IMAGE_MODELS = [
17341738
*/
17351739
export const OPENAI_VIDEO_MODELS = [SORA2.name, SORA2_PRO.name] as const
17361740

1741+
export type OpenAIVideoModel = (typeof OPENAI_VIDEO_MODELS)[number]
1742+
17371743
/**
17381744
* Text-to-speech models (based on endpoints: "speech_generation")
17391745
*/
@@ -1743,6 +1749,8 @@ export const OPENAI_TTS_MODELS = [
17431749
'gpt-4o-audio-preview',
17441750
] as const
17451751

1752+
export type OpenAITTSModel = (typeof OPENAI_TTS_MODELS)[number]
1753+
17461754
/**
17471755
* Transcription models (based on endpoints: "transcription")
17481756
*/
@@ -1753,6 +1761,9 @@ export const OPENAI_TRANSCRIPTION_MODELS = [
17531761
'gpt-4o-transcribe-diarize',
17541762
] as const
17551763

1764+
export type OpenAITranscriptionModel =
1765+
(typeof OPENAI_TRANSCRIPTION_MODELS)[number]
1766+
17561767
/**
17571768
* Type-only map from chat model name to its provider options type.
17581769
* Used by the core AI types (via the adapter) to narrow

packages/typescript/ai-openai/src/utils/client.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import OpenAI_SDK from 'openai'
2+
import type { ClientOptions } from 'openai'
23

3-
export interface OpenAIClientConfig {
4+
export interface OpenAIClientConfig extends ClientOptions {
45
apiKey: string
5-
organization?: string
6-
baseURL?: string
76
}
87

98
/**
109
* Creates an OpenAI SDK client instance
1110
*/
1211
export function createOpenAIClient(config: OpenAIClientConfig): OpenAI_SDK {
13-
return new OpenAI_SDK({
14-
apiKey: config.apiKey,
15-
organization: config.organization,
16-
baseURL: config.baseURL,
17-
})
12+
return new OpenAI_SDK(config)
1813
}
1914

2015
/**

0 commit comments

Comments
 (0)