Nous.Providers.OpenAI (nous v0.16.4)
View SourceOpenAI-specific provider implementation.
Extends the generic OpenAI-compatible provider with OpenAI-specific features:
- Structured outputs (response_format with json_schema)
- Predicted outputs
- Reasoning models (o1, o3)
- Future: Responses API, Assistants API, etc.
For generic OpenAI-compatible endpoints (Groq, Together, LM Studio, etc.),
use Nous.Providers.OpenAICompatible instead.
Usage
# Basic chat
{:ok, response} = Nous.Providers.OpenAI.chat(%{
model: "gpt-4o",
messages: [%{"role" => "user", "content" => "Hello"}]
})
# With structured output
{:ok, response} = Nous.Providers.OpenAI.chat(%{
model: "gpt-4o",
messages: messages,
response_format: %{
type: "json_schema",
json_schema: %{
name: "response",
schema: %{type: "object", properties: %{answer: %{type: "string"}}}
}
}
})
# Streaming
{:ok, stream} = Nous.Providers.OpenAI.chat_stream(params)
Enum.each(stream, fn event -> IO.inspect(event) end)Configuration
# In config.exs
config :nous, :openai,
api_key: "sk-...",
organization: "org-..." # optional
Summary
Functions
Get the API key from options, environment, or application config.
Get the base URL from options, application config, or default.
Count tokens in messages (rough estimate).
Check if a model is a reasoning model (o1, o3 series).
High-level request with message conversion, telemetry, and error wrapping.
High-level streaming request with message conversion and telemetry.
Functions
Get the API key from options, environment, or application config.
Lookup order:
:api_keyoption passed directly- Environment variable (OPENAI_API_KEY)
- Application config:
config :nous, openai, api_key: "..."
Get the base URL from options, application config, or default.
Lookup order:
:base_urloption passed directly- Application config:
config :nous, openai, base_url: "..." - Default: https://api.openai.com/v1
Count tokens in messages (rough estimate).
Override this in your provider for more accurate counting.
Check if a model is a reasoning model (o1, o3 series).
Reasoning models have different API requirements:
- No streaming support
- No system messages (use developer messages instead)
- No temperature parameter
High-level request with message conversion, telemetry, and error wrapping.
Default implementation that:
- Converts messages to provider format
- Builds request params
- Calls chat/2
- Parses response
- Emits telemetry events
- Wraps errors
High-level streaming request with message conversion and telemetry.