Skip to main content

Namespace: openai

Functions

Api

Api(settings): OpenAIApiConfiguration

Creates an API configuration for the OpenAI API. It calls the API at https://api.openai.com/v1 and uses the OPENAI_API_KEY env variable by default.

Parameters

NameType
settingsOmit<BaseUrlPartsApiConfigurationOptions, "baseUrl"> & { baseUrl?: string | Partial<UrlParts> } & { apiKey?: string }

Returns

OpenAIApiConfiguration

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:37


AzureApi

AzureApi(settings): AzureOpenAIApiConfiguration

Configuration for the Azure OpenAI API. This class is responsible for constructing URLs specific to the Azure OpenAI deployment. It creates URLs of the form https://[resourceName].openai.azure.com/openai/deployments/[deploymentId]/[path]?api-version=[apiVersion]

Parameters

NameType
settingsAzureOpenAIApiConfigurationOptions

Returns

AzureOpenAIApiConfiguration

See

https://learn.microsoft.com/en-us/azure/ai-services/openai/reference

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:52


ChatTextGenerator

ChatTextGenerator(settings): OpenAIChatModel

Create a text generation model that calls the OpenAI chat completion API.

Parameters

NameType
settingsOpenAIChatSettings

Returns

OpenAIChatModel

See

https://platform.openai.com/docs/api-reference/chat/create

Example

const model = openai.ChatTextGenerator({
model: "gpt-3.5-turbo",
temperature: 0.7,
maxGenerationTokens: 500,
});

const text = await generateText({
model,
prompt: [
openai.ChatMessage.system(
"Write a short story about a robot learning to love:"
),
]
});

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:103


CompletionTextGenerator

CompletionTextGenerator(settings): OpenAICompletionModel

Create a text generation model that calls the OpenAI text completion API.

Parameters

NameType
settingsOpenAICompletionModelSettings

Returns

OpenAICompletionModel

A new instance of OpenAICompletionModel.

See

https://platform.openai.com/docs/api-reference/completions/create

Example

const model = openai.CompletionTextGenerator({
model: "gpt-3.5-turbo-instruct",
temperature: 0.7,
maxGenerationTokens: 500,
retry: retryWithExponentialBackoff({ maxTries: 5 }),
});

const text = await generateText({
model,
prompt: "Write a short story about a robot learning to love:\n\n"
});

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:76


ImageGenerator

ImageGenerator(settings): OpenAIImageGenerationModel

Create an image generation model that calls the OpenAI AI image creation API.

Parameters

NameType
settingsOpenAIImageGenerationSettings

Returns

OpenAIImageGenerationModel

A new instance of OpenAIImageGenerationModel.

See

https://platform.openai.com/docs/api-reference/images/create

Example

const image = await generateImage({
model: new OpenAIImageGenerationModel({ size: "512x512" }),
prompt: "the wicked witch of the west in the style of early 19th century painting"
});

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:173


SpeechGenerator

SpeechGenerator(settings): OpenAISpeechModel

Synthesize speech using the OpenAI API.

Parameters

NameType
settingsOpenAISpeechModelSettings

Returns

OpenAISpeechModel

A new instance of OpenAISpeechModel.

See

https://platform.openai.com/docs/api-reference/audio/createSpeech

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:134


TextEmbedder

TextEmbedder(settings): OpenAITextEmbeddingModel

Create a text embedding model that calls the OpenAI embedding API.

Parameters

NameType
settingsOpenAITextEmbeddingModelSettings

Returns

OpenAITextEmbeddingModel

A new instance of OpenAITextEmbeddingModel.

See

https://platform.openai.com/docs/api-reference/embeddings

Example

const embeddings = await embedMany({
model: openai.TextEmbedder({ model: "text-embedding-ada-002" }),
values: [
"At first, Nox didn't know what to do with the pup.",
"He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
]
});

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:123


Tokenizer

Tokenizer(settings): TikTokenTokenizer

Creates a TikToken tokenizer for OpenAI language models.

Parameters

NameType
settingsTikTokenTokenizerSettings

Returns

TikTokenTokenizer

A new instance of TikTokenTokenizer.

See

https://github.com/openai/tiktoken

Example

const tokenizer = openai.Tokenizer({ model: "gpt-4" });

const text = "At first, Nox didn't know what to do with the pup.";

const tokenCount = await countTokens(tokenizer, text);
const tokens = await tokenizer.tokenize(text);
const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
const reconstructedText = await tokenizer.detokenize(tokens);

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:194


Transcriber

Transcriber(settings): OpenAITranscriptionModel

Create a transcription model that calls the OpenAI transcription API.

Parameters

NameType
settingsOpenAITranscriptionModelSettings

Returns

OpenAITranscriptionModel

A new instance of OpenAITranscriptionModel.

See

https://platform.openai.com/docs/api-reference/audio/create

Example

const data = await fs.promises.readFile("data/test.mp3");

const transcription = await transcribe({
model: openai.Transcriber({ model: "whisper-1" }),
data: {
type: "mp3",
data,
}
});

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIFacade.ts:156

Variables

ChatMessage

ChatMessage: Object

Type declaration

NameType
assistant(content: null | string, options?: { functionCall?: { arguments: string ; name: string } ; toolCalls?: null | ToolCall<string, unknown>[] }) => ChatMessage
fn(__namedParameters: { content: unknown ; fnName: string }) => ChatMessage
system(content: string) => ChatMessage
tool(__namedParameters: { content: unknown ; toolCallId: string }) => ChatMessage
user(content: string | (TextPart | ImagePart)[], options?: { name?: string }) => ChatMessage

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIChatMessage.ts:8

packages/modelfusion/src/model-provider/openai/OpenAIChatMessage.ts:60

Type Aliases

ChatMessage

Ƭ ChatMessage: { content: string ; name?: string ; role: "system" } | { content: string | ({ text: string ; type: "text" } | { image_url: string | { detail: "low" | "high" | "auto" ; url: string } ; type: "image_url" })[] ; name?: string ; role: "user" } | { content: string | null ; function_call?: { arguments: string ; name: string } ; name?: string ; role: "assistant" ; tool_calls?: { function: { arguments: string ; name: string } ; id: string ; type: "function" }[] } | { content: string | null ; role: "tool" ; tool_call_id: string } | { content: string ; name: string ; role: "function" }

Defined in

packages/modelfusion/src/model-provider/openai/OpenAIChatMessage.ts:8

packages/modelfusion/src/model-provider/openai/OpenAIChatMessage.ts:60


ChatPrompt

Ƭ ChatPrompt: ChatMessage[]

Defined in

packages/modelfusion/src/model-provider/openai/AbstractOpenAIChatModel.ts:98