Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-wolves-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai-openai': minor
---

allows additional configuration options when creating an openAI client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Capitalize “OpenAI” in the changeset text.
Minor copy edit for brand casing.

✏️ Suggested fix
-allows additional configuration options when creating an openAI client
+allows additional configuration options when creating an OpenAI client
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
allows additional configuration options when creating an openAI client
allows additional configuration options when creating an OpenAI client
🤖 Prompt for AI Agents
In @.changeset/olive-wolves-love.md at line 5, Update the changeset copy that
currently reads "allows additional configuration options when creating an openAI
client" to use the correct brand casing "OpenAI" (i.e., change "openAI" →
"OpenAI"), and scan the same .changeset entry for any other occurrences of
"openAI" to correct them for consistency.

9 changes: 3 additions & 6 deletions packages/typescript/ai-openai/src/adapters/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
createOpenAIClient,
generateId,
getOpenAIApiKeyFromEnv,
} from '../utils'
} from '../utils/client'
import {
validateImageSize,
validateNumberOfImages,
validatePrompt,
} from '../image/image-provider-options'
import type { OPENAI_IMAGE_MODELS } from '../model-meta'
import type { OpenAIImageModel } from '../model-meta'
import type {
OpenAIImageModelProviderOptionsByName,
OpenAIImageModelSizeByName,
Expand All @@ -21,16 +21,13 @@ import type {
ImageGenerationResult,
} from '@tanstack/ai'
import type OpenAI_SDK from 'openai'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI image adapter
*/
export interface OpenAIImageConfig extends OpenAIClientConfig {}

/** Model type for OpenAI Image */
export type OpenAIImageModel = (typeof OPENAI_IMAGE_MODELS)[number]

/**
* OpenAI Image Generation Adapter
*
Expand Down
15 changes: 6 additions & 9 deletions packages/typescript/ai-openai/src/adapters/summarize.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'
import { getOpenAIApiKeyFromEnv } from '../utils'
import { getOpenAIApiKeyFromEnv } from '../utils/client'
import { OpenAITextAdapter } from './text'
import type { OPENAI_CHAT_MODELS } from '../model-meta'
import type { OpenAIChatModel } from '../model-meta'
import type {
StreamChunk,
SummarizationOptions,
SummarizationResult,
} from '@tanstack/ai'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI summarize adapter
Expand All @@ -24,17 +24,14 @@ export interface OpenAISummarizeProviderOptions {
maxTokens?: number
}

/** Model type for OpenAI summarization */
export type OpenAISummarizeModel = (typeof OPENAI_CHAT_MODELS)[number]

/**
* OpenAI Summarize Adapter
*
* A thin wrapper around the text adapter that adds summarization-specific prompting.
* Delegates all API calls to the OpenAITextAdapter.
*/
export class OpenAISummarizeAdapter<
TModel extends OpenAISummarizeModel,
TModel extends OpenAIChatModel,
> extends BaseSummarizeAdapter<TModel, OpenAISummarizeProviderOptions> {
readonly kind = 'summarize' as const
readonly name = 'openai' as const
Expand Down Expand Up @@ -133,7 +130,7 @@ export class OpenAISummarizeAdapter<
* const adapter = createOpenaiSummarize('gpt-4o-mini', "sk-...");
* ```
*/
export function createOpenaiSummarize<TModel extends OpenAISummarizeModel>(
export function createOpenaiSummarize<TModel extends OpenAIChatModel>(
model: TModel,
apiKey: string,
config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
Expand Down Expand Up @@ -165,7 +162,7 @@ export function createOpenaiSummarize<TModel extends OpenAISummarizeModel>(
* });
* ```
*/
export function openaiSummarize<TModel extends OpenAISummarizeModel>(
export function openaiSummarize<TModel extends OpenAIChatModel>(
model: TModel,
config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
): OpenAISummarizeAdapter<TModel> {
Expand Down
9 changes: 6 additions & 3 deletions packages/typescript/ai-openai/src/adapters/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
createOpenAIClient,
generateId,
getOpenAIApiKeyFromEnv,
} from '../utils/client'
import {
makeOpenAIStructuredOutputCompatible,
transformNullsToUndefined,
} from '../utils'
} from '../utils/schema-converter'
import type {
OPENAI_CHAT_MODELS,
OpenAIChatModel,
OpenAIChatModelProviderOptionsByName,
OpenAIModelInputModalitiesByName,
} from '../model-meta'
Expand All @@ -34,7 +37,7 @@ import type {
OpenAIImageMetadata,
OpenAIMessageMetadataByModality,
} from '../message-types'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI text adapter
Expand Down Expand Up @@ -79,7 +82,7 @@ type ResolveInputModalities<TModel extends string> =
* Import only what you need for smaller bundle sizes.
*/
export class OpenAITextAdapter<
TModel extends (typeof OPENAI_CHAT_MODELS)[number],
TModel extends OpenAIChatModel,
> extends BaseTextAdapter<
TModel,
ResolveProviderOptions<TModel>,
Expand Down
10 changes: 3 additions & 7 deletions packages/typescript/ai-openai/src/adapters/transcription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ import {
createOpenAIClient,
generateId,
getOpenAIApiKeyFromEnv,
} from '../utils'
import type { OPENAI_TRANSCRIPTION_MODELS } from '../model-meta'
} from '../utils/client'
import type { OpenAITranscriptionModel } from '../model-meta'
import type { OpenAITranscriptionProviderOptions } from '../audio/transcription-provider-options'
import type {
TranscriptionOptions,
TranscriptionResult,
TranscriptionSegment,
} from '@tanstack/ai'
import type OpenAI_SDK from 'openai'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI Transcription adapter
*/
export interface OpenAITranscriptionConfig extends OpenAIClientConfig {}

/** Model type for OpenAI Transcription */
export type OpenAITranscriptionModel =
(typeof OPENAI_TRANSCRIPTION_MODELS)[number]

/**
* OpenAI Transcription (Speech-to-Text) Adapter
*
Expand Down
9 changes: 3 additions & 6 deletions packages/typescript/ai-openai/src/adapters/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ import {
createOpenAIClient,
generateId,
getOpenAIApiKeyFromEnv,
} from '../utils'
} from '../utils/client'
import {
validateAudioInput,
validateInstructions,
validateSpeed,
} from '../audio/audio-provider-options'
import type { OPENAI_TTS_MODELS } from '../model-meta'
import type { OpenAITTSModel } from '../model-meta'
import type {
OpenAITTSFormat,
OpenAITTSProviderOptions,
OpenAITTSVoice,
} from '../audio/tts-provider-options'
import type { TTSOptions, TTSResult } from '@tanstack/ai'
import type OpenAI_SDK from 'openai'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI TTS adapter
*/
export interface OpenAITTSConfig extends OpenAIClientConfig {}

/** Model type for OpenAI TTS */
export type OpenAITTSModel = (typeof OPENAI_TTS_MODELS)[number]

/**
* OpenAI Text-to-Speech Adapter
*
Expand Down
9 changes: 3 additions & 6 deletions packages/typescript/ai-openai/src/adapters/video.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BaseVideoAdapter } from '@tanstack/ai/adapters'
import { createOpenAIClient, getOpenAIApiKeyFromEnv } from '../utils'
import { createOpenAIClient, getOpenAIApiKeyFromEnv } from '../utils/client'
import {
toApiSeconds,
validateVideoSeconds,
validateVideoSize,
} from '../video/video-provider-options'
import type { VideoModel } from 'openai/resources'
import type { OPENAI_VIDEO_MODELS } from '../model-meta'
import type { OpenAIVideoModel } from '../model-meta'
import type {
OpenAIVideoModelProviderOptionsByName,
OpenAIVideoProviderOptions,
Expand All @@ -18,7 +18,7 @@ import type {
VideoUrlResult,
} from '@tanstack/ai'
import type OpenAI_SDK from 'openai'
import type { OpenAIClientConfig } from '../utils'
import type { OpenAIClientConfig } from '../utils/client'

/**
* Configuration for OpenAI video adapter.
Expand All @@ -27,9 +27,6 @@ import type { OpenAIClientConfig } from '../utils'
*/
export interface OpenAIVideoConfig extends OpenAIClientConfig {}

/** Model type for OpenAI Video */
export type OpenAIVideoModel = (typeof OPENAI_VIDEO_MODELS)[number]

/**
* OpenAI Video Generation Adapter
*
Expand Down
6 changes: 6 additions & 0 deletions packages/typescript/ai-openai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export type { OpenAITranscriptionProviderOptions } from './audio/transcription-p
export type {
OpenAIChatModelProviderOptionsByName,
OpenAIModelInputModalitiesByName,
OpenAIChatModel,
OpenAIImageModel,
OpenAIVideoModel,
OpenAITTSModel,
OpenAITranscriptionModel,
} from './model-meta'
export {
OPENAI_IMAGE_MODELS,
Expand All @@ -94,3 +99,4 @@ export type {
OpenAIDocumentMetadata,
OpenAIMessageMetadataByModality,
} from './message-types'
export type { OpenAIClientConfig } from './utils/client'
11 changes: 11 additions & 0 deletions packages/typescript/ai-openai/src/model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,8 @@ export const OPENAI_CHAT_MODELS = [
O1_PRO.name,
] as const

export type OpenAIChatModel = (typeof OPENAI_CHAT_MODELS)[number]

// Image generation models (based on endpoints: "image-generation" or "image-edit")
export const OPENAI_IMAGE_MODELS = [
GPT_IMAGE_1.name,
Expand All @@ -1704,6 +1706,8 @@ export const OPENAI_IMAGE_MODELS = [
DALL_E_2.name,
] as const

export type OpenAIImageModel = (typeof OPENAI_IMAGE_MODELS)[number]

// Audio models (based on endpoints: "transcription", "speech_generation", or "realtime")
/* const OPENAI_AUDIO_MODELS = [
// Transcription models
Expand Down Expand Up @@ -1734,6 +1738,8 @@ export const OPENAI_IMAGE_MODELS = [
*/
export const OPENAI_VIDEO_MODELS = [SORA2.name, SORA2_PRO.name] as const

export type OpenAIVideoModel = (typeof OPENAI_VIDEO_MODELS)[number]

/**
* Text-to-speech models (based on endpoints: "speech_generation")
*/
Expand All @@ -1743,6 +1749,8 @@ export const OPENAI_TTS_MODELS = [
'gpt-4o-audio-preview',
] as const

export type OpenAITTSModel = (typeof OPENAI_TTS_MODELS)[number]

/**
* Transcription models (based on endpoints: "transcription")
*/
Expand All @@ -1753,6 +1761,9 @@ export const OPENAI_TRANSCRIPTION_MODELS = [
'gpt-4o-transcribe-diarize',
] as const

export type OpenAITranscriptionModel =
(typeof OPENAI_TRANSCRIPTION_MODELS)[number]

/**
* Type-only map from chat model name to its provider options type.
* Used by the core AI types (via the adapter) to narrow
Expand Down
11 changes: 3 additions & 8 deletions packages/typescript/ai-openai/src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import OpenAI_SDK from 'openai'
import type { ClientOptions } from 'openai'

export interface OpenAIClientConfig {
export interface OpenAIClientConfig extends ClientOptions {
apiKey: string
organization?: string
baseURL?: string
}

/**
* Creates an OpenAI SDK client instance
*/
export function createOpenAIClient(config: OpenAIClientConfig): OpenAI_SDK {
return new OpenAI_SDK({
apiKey: config.apiKey,
organization: config.organization,
baseURL: config.baseURL,
})
return new OpenAI_SDK(config)
}

/**
Expand Down
10 changes: 0 additions & 10 deletions packages/typescript/ai-openai/src/utils/index.ts

This file was deleted.

Loading