Nous.Providers.Gemini (nous v0.16.2)
View SourceGoogle Gemini provider implementation.
Supports Gemini models via the Google AI Generative Language API.
Usage
# Basic usage
{:ok, response} = Nous.Providers.Gemini.chat(%{
model: "gemini-2.0-flash-exp",
contents: [%{"role" => "user", "parts" => [%{"text" => "Hello"}]}]
})
# With system instruction
{:ok, response} = Nous.Providers.Gemini.chat(%{
model: "gemini-2.0-flash-exp",
systemInstruction: %{"parts" => [%{"text" => "You are helpful."}]},
contents: [%{"role" => "user", "parts" => [%{"text" => "Hello"}]}],
generationConfig: %{"temperature" => 0.7, "maxOutputTokens" => 1024}
})
# Streaming
{:ok, stream} = Nous.Providers.Gemini.chat_stream(params)
Enum.each(stream, fn event -> IO.inspect(event) end)Configuration
# In config.exs
config :nous, :gemini,
api_key: "AIza...",
base_url: "https://generativelanguage.googleapis.com/v1beta" # optionalNote on Authentication
Unlike OpenAI/Anthropic which use headers, Gemini uses query parameter auth:
?key=API_KEY
Thinking (Gemini 2.5/3.x)
For models that support extended thinking, pass :thinking_config in
default_settings. Both the Elixir shape (snake_case atoms) and the native
Vertex shape (camelCase strings) are accepted:
# Elixir shape (recommended)
Nous.new("gemini:gemini-2.5-pro",
default_settings: %{
thinking_config: %{thinking_budget: 1024, include_thoughts: true}
}
)
# Native Vertex shape
Nous.new("gemini:gemini-2.5-pro",
default_settings: %{
thinking_config: %{"thinkingBudget" => 1024, "includeThoughts" => true}
}
)When include_thoughts: true, thought summaries arrive as
Message.reasoning_content. For tool-using thinking models, Vertex emits a
thoughtSignature on each tool call; Nous preserves it in
tool_call["metadata"]["thought_signature"] and echoes it back on the next
turn automatically — required for multi-turn thinking + tool loops to keep
working.
See https://ai.google.dev/gemini-api/docs/thinking for the Gemini-side docs and https://cloud.google.com/vertex-ai/generative-ai/docs/thinking for the Vertex equivalent.
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).
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 (GOOGLE_AI_API_KEY)
- Application config:
config :nous, gemini, api_key: "..."
Get the base URL from options, application config, or default.
Lookup order:
:base_urloption passed directly- Application config:
config :nous, gemini, base_url: "..." - Default: https://generativelanguage.googleapis.com/v1beta
Count tokens in messages (rough estimate).
Override this in your provider for more accurate counting.
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.