Gemini.Generate (GeminiEx v0.0.2)
View SourceAPI for generating content with Gemini models.
Summary
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.
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
Start a chat session for multi-turn conversations.
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}
Generate content using a Gemini model.
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{...}}
Count tokens in the given content.
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}}
Send a message in a chat session.
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
Generate content with streaming support.
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{...}, ...]}
Generate content and return only the text from the first candidate.
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."}