ExLLM.Gemini.QA (ex_llm v0.5.0)

View Source

Google Gemini Question Answering API implementation.

The Semantic Retrieval API provides a hosted question answering service for building Retrieval Augmented Generation (RAG) systems using Google's infrastructure.

Usage

# With inline passages
contents = [
  %{
    parts: [%{text: "What is the capital of France?"}],
    role: "user"
  }
]

passages = [
  %{
    id: "france_info",
    content: %{
      parts: [%{text: "France is a country in Europe. Paris is the capital of France."}]
    }
  }
]

{:ok, response} = ExLLM.Gemini.QA.generate_answer(
  "models/gemini-1.5-flash",
  contents,
  :abstractive,
  inline_passages: passages,
  temperature: 0.1,
  api_key: "your-api-key"
)

# With semantic retriever
{:ok, response} = ExLLM.Gemini.QA.generate_answer(
  "models/gemini-1.5-flash",
  contents,
  :verbose,
  semantic_retriever: %{
    source: "corpora/my_corpus",
    query: %{parts: [%{text: "capital of France"}]},
    max_chunks_count: 5
  },
  oauth_token: "your-oauth-token"
)

Summary

Functions

Builds a GenerateAnswerRequest struct from the given parameters.

Formats an answer style atom to the API string format.

Formats a block reason string to an atom.

Generates a grounded answer from the model given an input.

Parses a GenerateAnswerResponse from the API response body.

Functions

build_generate_answer_request(contents, answer_style, opts)

@spec build_generate_answer_request([map()], atom(), map()) ::
  ExLLM.Gemini.QA.GenerateAnswerRequest.t()

Builds a GenerateAnswerRequest struct from the given parameters.

format_answer_style(style)

@spec format_answer_style(atom()) :: String.t()

Formats an answer style atom to the API string format.

format_block_reason(arg1)

@spec format_block_reason(String.t()) :: atom()

Formats a block reason string to an atom.

generate_answer(model, contents, answer_style, opts \\ [])

@spec generate_answer(String.t(), [map()], atom(), Keyword.t()) ::
  {:ok, ExLLM.Gemini.QA.GenerateAnswerResponse.t()} | {:error, map()}

Generates a grounded answer from the model given an input.

Parameters

  • model - The name of the model to use (e.g., "models/gemini-1.5-flash")
  • contents - List of conversation content (messages)
  • answer_style - Style for the answer (:abstractive, :extractive, :verbose)
  • opts - Additional options

Options

  • :inline_passages - List of passages to use for grounding
  • :semantic_retriever - Configuration for semantic retrieval
  • :temperature - Controls randomness (0.0-1.0)
  • :safety_settings - List of safety settings
  • :api_key - Gemini API key
  • :oauth_token - OAuth2 token (alternative to API key)

Examples

{:ok, response} = ExLLM.Gemini.QA.generate_answer(
  "models/gemini-1.5-flash",
  [%{parts: [%{text: "What is AI?"}], role: "user"}],
  :abstractive,
  inline_passages: [
    %{
      id: "ai_definition",
      content: %{parts: [%{text: "AI is artificial intelligence..."}]}
    }
  ],
  temperature: 0.1,
  api_key: "your-api-key"
)

parse_generate_answer_response(response_body)

@spec parse_generate_answer_response(map()) ::
  ExLLM.Gemini.QA.GenerateAnswerResponse.t()

Parses a GenerateAnswerResponse from the API response body.