Gemini.Generate (GeminiEx v0.2.1)
View SourceAPI for generating content with Gemini models.
See Gemini.options/0
in Gemini
for the canonical list of options.
Summary
Types
Options for chat sessions.
Functions
Build a generate content request structure.
Start a chat session for multi-turn conversations.
Generate content using a Gemini model.
Count tokens in the given content.
Send a message in a chat session.
Generate content with streaming support.
Generate content and return only the text from the first candidate.
Types
@type chat_options() :: Gemini.options() | [{:history, [Gemini.Types.Content.t()]}]
Options for chat sessions.
All options from Gemini.options/0
, plus:
:history
- Chat history as a list of Content structs
Functions
Build a generate content request structure.
This function is exposed publicly to allow the streaming manager and other components to construct requests using the same validation and normalization logic as the main content generation functions.
Parameters
contents
- List of Content structs or stringsopts
- Options including generation_config, safety_settings, etc.
Returns
- Map representing the request structure ready for JSON encoding
@spec chat(chat_options()) :: {:ok, map()}
Start a chat session for multi-turn conversations.
All options from Gemini.options/0
, plus:
:history
- Chat history as a list of Content structs
Parameters
opts
- Options including::model
- Model name (default: from config):history
- Chat history as list of Content structs:generation_config
- GenerationConfig struct:safety_settings
- List of SafetySetting structs:system_instruction
- System instruction
Examples
iex> {:ok, chat} = Gemini.Generate.chat()
iex> Gemini.Generate.send_message(chat, "Hello!")
{:ok, response, updated_chat}
@spec content(String.t() | [Gemini.Types.Content.t()], Gemini.options()) :: {:ok, Gemini.Types.Response.GenerateContentResponse.t()} | {:error, Gemini.Error.t()}
Generate content using a Gemini model.
See Gemini.options/0
for available options.
Parameters
contents
- List of Content structs or stringsopts
- Options including::model
- Model name (default: from config):generation_config
- GenerationConfig struct:safety_settings
- List of SafetySetting structs:system_instruction
- System instruction as Content or string:tools
- List of tool definitions:tool_config
- Tool configuration
Examples
iex> Gemini.Generate.content("Hello, world!")
{:ok, %GenerateContentResponse{candidates: [%Candidate{...}]}}
iex> contents = [Content.text("Explain quantum physics")]
iex> config = GenerationConfig.creative()
iex> Gemini.Generate.content(contents, generation_config: config)
{:ok, %GenerateContentResponse{...}}
@spec count_tokens(String.t() | [Gemini.Types.Content.t()], Gemini.options()) :: {:ok, Gemini.Types.Response.CountTokensResponse.t()} | {:error, Gemini.Error.t()}
Count tokens in the given content.
See Gemini.options/0
for available options.
Parameters
contents
- List of Content structs or stringsopts
- Options including::model
- Model name (default: from config)
Examples
iex> Gemini.Generate.count_tokens("Hello, world!")
{:ok, %CountTokensResponse{total_tokens: 3}}
@spec send_message(map(), String.t() | Gemini.Types.Content.t()) :: {:ok, Gemini.Types.Response.GenerateContentResponse.t(), map()} | {:error, Gemini.Error.t()}
Send a message in a chat session.
Uses options from the chat session (see Gemini.Generate.chat_options/0
).
Parameters
chat
- Chat session fromchat/1
message
- Message content as string or Content struct
Returns
{:ok, response, updated_chat}
on success{:error, error}
on failure
@spec stream_content(String.t() | [Gemini.Types.Content.t()], Gemini.options()) :: {:ok, [Gemini.Types.Response.GenerateContentResponse.t()]} | {:error, Gemini.Error.t()}
Generate content with streaming support.
See Gemini.options/0
for available options.
Returns a stream of partial responses as they become available.
Parameters
contents
- List of Content structs or stringsopts
- Same options ascontent/2
Examples
iex> Gemini.Generate.stream_content("Write a story")
{:ok, [%GenerateContentResponse{...}, ...]}
@spec text(String.t() | [Gemini.Types.Content.t()], Gemini.options()) :: {:ok, String.t()} | {:error, Gemini.Error.t()}
Generate content and return only the text from the first candidate.
See Gemini.options/0
for available options.
This is a convenience function for simple text generation.
Examples
iex> Gemini.Generate.text("What is the capital of France?")
{:ok, "The capital of France is Paris."}