class

AI::OpenAI::OpenAIProvider

Inherits AI::Provider::Provider::V3 < Reference < Object

OpenAI provider.

Provides factory methods for creating OpenAI models:

  • chat - Chat completion models (gpt-4o, gpt-4, gpt-3.5-turbo, etc.)
  • embedding - Embedding models (text-embedding-3-small/large)
  • image - Image generation models (dall-e-2, dall-e-3, gpt-image-1)
  • speech - Text-to-speech models (tts-1, tts-1-hd, gpt-4o-mini-tts)
  • transcription - Speech-to-text models (whisper-1, gpt-4o-transcribe)

Example:

provider = AI::OpenAI.create_openai(api_key: "sk-...")
model = provider.chat("gpt-4o")
result = model.do_generate(options)

Constructors

new(settings : ProviderSettings = ProviderSettings.new)
Source

Instance methods

chat(model_id : String) : OpenAIChatLanguageModel

Creates a chat model for text generation.

Supports all OpenAI chat models including:

  • gpt-4o, gpt-4o-mini
  • gpt-4-turbo, gpt-4
  • gpt-3.5-turbo
  • o1, o1-mini, o1-preview
  • o3, o3-mini
  • o4-mini
  • gpt-5, gpt-5-mini
Source
embedding(model_id : String) : OpenAIEmbeddingModel

Alias for embedding_model.

Source
embedding_model(model_id : String) : OpenAIEmbeddingModel

Creates an embedding model.

Supports:

  • text-embedding-3-small
  • text-embedding-3-large
  • text-embedding-ada-002
Source
image(model_id : String) : OpenAIImageModel

Alias for image_model.

Source
image_model(model_id : String) : OpenAIImageModel

Creates an image model.

Supports:

  • dall-e-2
  • dall-e-3
  • gpt-image-1
Source
language_model(model_id : String) : OpenAIChatLanguageModel

Alias for chat - creates a language model.

By default, returns a chat model. Use the chat method explicitly for chat completion models.

Source
provider_name

Provider name.

Source
responses(model_id : String) : Responses::OpenAIResponsesLanguageModel

Creates a responses model for GPT-5, o3, o4-mini, etc.

The Responses API provides advanced features like:

  • MCP (Model Context Protocol) integration
  • Shell/LocalShell execution
  • ApplyPatch for code editing
  • Image generation tool
  • File search with vector stores
  • Code interpreter
  • Web search with citations

Supports:

  • gpt-5, gpt-5-mini, gpt-5-nano, gpt-5-pro
  • gpt-5.1, gpt-5.2
  • o3, o3-mini, o4-mini
  • gpt-4.1, gpt-4.1-mini, gpt-4.1-nano
  • gpt-4o, gpt-4o-mini
Source
specification_version

The specification version.

Source
speech(model_id : String) : OpenAISpeechModel

Alias for speech_model.

Source
speech_model(model_id : String) : OpenAISpeechModel

Creates a speech model for text-to-speech synthesis.

Supports:

  • tts-1 - Fast text-to-speech model
  • tts-1-hd - High-definition text-to-speech model
  • gpt-4o-mini-tts - GPT-4o mini text-to-speech model

Example:

speech_model = provider.speech("tts-1")
result = speech_model.do_generate(
  AI::Provider::SpeechModel::CallOptions.new(
    text: "Hello, world!",
    voice: "alloy"
  )
)
File.write("output.mp3", result.audio)
Source
text_embedding(model_id : String) : OpenAIEmbeddingModel

Deprecated: Use embedding instead.

Source
text_embedding_model(model_id : String) : OpenAIEmbeddingModel

Deprecated: Use embedding_model instead.

Source
tools

Access to OpenAI-specific tools.

Returns the Tools module containing provider-defined tools like:

  • WebSearch
  • CodeInterpreter
  • FileSearch
  • ImageGeneration
  • Shell
  • LocalShell
  • ApplyPatch
  • Mcp
Source
transcription(model_id : String) : OpenAITranscriptionModel

Alias for transcription_model.

Source
transcription_model(model_id : String) : OpenAITranscriptionModel

Creates a transcription model for speech-to-text.

Supports:

  • whisper-1 - Whisper transcription model
  • gpt-4o-mini-transcribe - GPT-4o mini transcription model
  • gpt-4o-transcribe - GPT-4o transcription model

Example:

transcription_model = provider.transcription("whisper-1")
result = transcription_model.do_generate(
  AI::Provider::TranscriptionModel::CallOptions.new(
    audio: File.read("audio.mp3").to_slice,
    media_type: "audio/mp3"
  )
)
puts result.text
Source