class AI::OpenAI::OpenAIProvider

Overview

OpenAI provider.

Provides factory methods for creating OpenAI models:

Example:

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

Included Modules

Defined in:

ai/openai/openai_provider.cr

Constructors

Instance Method Summary

Instance methods inherited from module AI::Provider::Provider::V3

embedding_model(model_id : String) : EmbeddingModel::V3 embedding_model, image_model(model_id : String) : ImageModel::V3 image_model, language_model(model_id : String) : LanguageModel::V3 language_model, reranking_model(model_id : String) : RerankingModel::V3 reranking_model, specification_version : String specification_version, speech_model(model_id : String) : SpeechModel::V3 speech_model, text_embedding_model(model_id : String) : EmbeddingModel::V3 text_embedding_model, transcription_model(model_id : String) : TranscriptionModel::V3 transcription_model

Constructor Detail

def self.new(settings : ProviderSettings = ProviderSettings.new) #

[View source]

Instance Method Detail

def 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

[View source]
def embedding(model_id : String) : OpenAIEmbeddingModel #

Alias for embedding_model.


[View source]
def embedding_model(model_id : String) : OpenAIEmbeddingModel #

Creates an embedding model.

Supports:

  • text-embedding-3-small
  • text-embedding-3-large
  • text-embedding-ada-002

[View source]
def image(model_id : String) : OpenAIImageModel #

Alias for image_model.


[View source]
def image_model(model_id : String) : OpenAIImageModel #

Creates an image model.

Supports:

  • dall-e-2
  • dall-e-3
  • gpt-image-1

[View source]
def 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.


[View source]
def provider_name : String #

Provider name.


[View source]
def 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

[View source]
def specification_version : String #

The specification version.


[View source]
def speech(model_id : String) : OpenAISpeechModel #

Alias for speech_model.


[View source]
def 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)

[View source]
def text_embedding(model_id : String) : OpenAIEmbeddingModel #

Deprecated: Use embedding instead.


[View source]
def text_embedding_model(model_id : String) : OpenAIEmbeddingModel #

Deprecated: Use embedding_model instead.


[View source]
def tools : Tools.class #

Access to OpenAI-specific tools.

Returns the Tools module containing provider-defined tools like:

  • WebSearch
  • CodeInterpreter
  • FileSearch
  • ImageGeneration
  • Shell
  • LocalShell
  • ApplyPatch
  • Mcp

[View source]
def transcription(model_id : String) : OpenAITranscriptionModel #

Alias for transcription_model.


[View source]
def 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

[View source]