Gemini.Generate (GeminiEx v0.0.2)

View Source

API 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_generate_request(contents, opts)

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 strings
  • opts - Options including generation_config, safety_settings, etc.

Returns

  • Map representing the request structure ready for JSON encoding

chat(opts \\ [])

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}

content(contents, opts \\ [])

Generate content using a Gemini model.

Parameters

  • contents - List of Content structs or strings
  • opts - 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(contents, opts \\ [])

Count tokens in the given content.

Parameters

  • contents - List of Content structs or strings
  • opts - Options including:
    • :model - Model name (default: from config)

Examples

iex> Gemini.Generate.count_tokens("Hello, world!")
{:ok, %CountTokensResponse{total_tokens: 3}}

send_message(chat, message)

Send a message in a chat session.

Parameters

  • chat - Chat session from chat/1
  • message - Message content as string or Content struct

Returns

  • {:ok, response, updated_chat} on success
  • {:error, error} on failure

stream_content(contents, opts \\ [])

Generate content with streaming support.

Returns a stream of partial responses as they become available.

Parameters

  • contents - List of Content structs or strings
  • opts - Same options as content/2

Examples

iex> Gemini.Generate.stream_content("Write a story")
{:ok, [%GenerateContentResponse{...}, ...]}

text(contents, opts \\ [])

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."}