ExLLM.Providers.Gemini.Corpus (ex_llm v0.8.1)

View Source

Google Gemini Corpus Management API implementation.

Corpora are collections of Documents used for semantic retrieval. A project can create up to 5 corpora.

Usage

# Create a corpus
{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.create_corpus(%{
  display_name: "My Knowledge Base"
}, oauth_token: "your-oauth-token")

# List corpora
{:ok, response} = ExLLM.Providers.Gemini.Corpus.list_corpora(%{
  page_size: 10
}, oauth_token: "your-oauth-token")

# Query a corpus
{:ok, response} = ExLLM.Providers.Gemini.Corpus.query_corpus(
  "corpora/my-corpus",
  "search query",
  %{results_count: 5},
  oauth_token: "your-oauth-token"
)

# Update corpus
{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.update_corpus(
  "corpora/my-corpus",
  %{display_name: "Updated Name"},
  ["displayName"],
  oauth_token: "your-oauth-token"
)

# Delete corpus
:ok = ExLLM.Providers.Gemini.Corpus.delete_corpus(
  "corpora/my-corpus",
  oauth_token: "your-oauth-token"
)

Summary

Functions

Builds a CreateCorpusRequest struct from the given parameters.

Builds a ListCorporaRequest struct from the given parameters.

Builds a QueryCorpusRequest struct from the given parameters.

Builds an UpdateCorpusRequest struct from the given parameters.

Formats an operator string from the API to an atom.

Gets information about a specific corpus.

Lists corpora owned by the user.

Converts an operator atom to the API string format.

Parses a corpus response from the API.

Parses a list corpora response from the API.

Parses a query corpus response from the API.

Performs semantic search over a corpus.

Validates if a corpus name has the correct format.

Validates if a display name has a valid length.

Functions

build_create_corpus_request(corpus_data)

@spec build_create_corpus_request(map()) ::
  ExLLM.Providers.Gemini.Corpus.CreateCorpusRequest.t()

Builds a CreateCorpusRequest struct from the given parameters.

build_list_corpora_request(list_options)

@spec build_list_corpora_request(keyword() | map()) ::
  ExLLM.Providers.Gemini.Corpus.ListCorporaRequest.t()

Builds a ListCorporaRequest struct from the given parameters.

build_query_corpus_request(corpus_name, query, query_options)

@spec build_query_corpus_request(String.t(), String.t(), map()) ::
  ExLLM.Providers.Gemini.Corpus.QueryCorpusRequest.t()

Builds a QueryCorpusRequest struct from the given parameters.

build_update_corpus_request(corpus_name, update_data, update_mask)

@spec build_update_corpus_request(String.t(), map(), [String.t()]) ::
  ExLLM.Providers.Gemini.Corpus.UpdateCorpusRequest.t()

Builds an UpdateCorpusRequest struct from the given parameters.

create_corpus(corpus_data, opts \\ [])

@spec create_corpus(map(), Keyword.t()) ::
  {:ok, ExLLM.Providers.Gemini.Corpus.CorpusInfo.t()} | {:error, map()}

Creates a new corpus.

Parameters

  • corpus_data - Map containing corpus information
    • :name - Optional corpus name (will be auto-generated if not provided)
    • :display_name - Optional human-readable display name
  • opts - Options including OAuth2 token

Options

  • :oauth_token - OAuth2 token (required for corpus operations)

Examples

# Create corpus with auto-generated name
{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.create_corpus(%{
  display_name: "My Knowledge Base"
}, oauth_token: "your-oauth-token")

# Create corpus with specific name
{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.create_corpus(%{
  name: "corpora/my-custom-corpus",
  display_name: "Custom Corpus"
}, oauth_token: "your-oauth-token")

delete_corpus(corpus_name, opts \\ [])

@spec delete_corpus(String.t(), Keyword.t()) :: :ok | {:error, map()}

Deletes a corpus.

Parameters

  • corpus_name - The name of the corpus to delete
  • opts - Options including OAuth2 token and force flag
    • :force - If true, delete all documents in the corpus as well

Examples

# Delete empty corpus
:ok = ExLLM.Providers.Gemini.Corpus.delete_corpus(
  "corpora/my-corpus",
  oauth_token: "your-oauth-token"
)

# Force delete corpus with documents
:ok = ExLLM.Providers.Gemini.Corpus.delete_corpus(
  "corpora/my-corpus",
  oauth_token: "your-oauth-token",
  force: true
)

format_operator(arg1)

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

Formats an operator string from the API to an atom.

get_corpus(corpus_name, opts \\ [])

@spec get_corpus(String.t(), Keyword.t()) ::
  {:ok, ExLLM.Providers.Gemini.Corpus.CorpusInfo.t()} | {:error, map()}

Gets information about a specific corpus.

Parameters

  • corpus_name - The name of the corpus (e.g., "corpora/my-corpus")
  • opts - Options including OAuth2 token

Examples

{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.get_corpus(
  "corpora/my-corpus",
  oauth_token: "your-oauth-token"
)

list_corpora(list_options, opts \\ [])

@spec list_corpora(keyword() | map(), Keyword.t()) ::
  {:ok, ExLLM.Providers.Gemini.Corpus.ListCorporaResponse.t()} | {:error, map()}

Lists corpora owned by the user.

Parameters

  • list_options - Map containing pagination options
    • :page_size - Optional maximum number of corpora per page (1-20)
    • :page_token - Optional page token for pagination
  • opts - Options including OAuth2 token

Examples

# List all corpora
{:ok, response} = ExLLM.Providers.Gemini.Corpus.list_corpora(%{}, 
  oauth_token: "your-oauth-token")

# List with pagination
{:ok, response} = ExLLM.Providers.Gemini.Corpus.list_corpora(%{
  page_size: 5,
  page_token: "next_page_token"
}, oauth_token: "your-oauth-token")

operator_to_api_string(op)

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

Converts an operator atom to the API string format.

parse_corpus_response(response_body)

@spec parse_corpus_response(map()) :: ExLLM.Providers.Gemini.Corpus.CorpusInfo.t()

Parses a corpus response from the API.

parse_list_corpora_response(response_body)

@spec parse_list_corpora_response(map()) ::
  ExLLM.Providers.Gemini.Corpus.ListCorporaResponse.t()

Parses a list corpora response from the API.

parse_query_corpus_response(response_body)

@spec parse_query_corpus_response(map()) ::
  ExLLM.Providers.Gemini.Corpus.QueryCorpusResponse.t()

Parses a query corpus response from the API.

query_corpus(corpus_name, query, query_options, opts \\ [])

@spec query_corpus(String.t(), String.t(), map(), Keyword.t()) ::
  {:ok, ExLLM.Providers.Gemini.Corpus.QueryCorpusResponse.t()} | {:error, map()}

Performs semantic search over a corpus.

Parameters

  • corpus_name - The name of the corpus to query
  • query - The search query string
  • query_options - Map containing query options
    • :results_count - Maximum number of chunks to return (1-100)
    • :metadata_filters - List of metadata filters
  • opts - Options including OAuth2 token

Examples

# Simple query
{:ok, response} = ExLLM.Providers.Gemini.Corpus.query_corpus(
  "corpora/my-corpus",
  "artificial intelligence",
  %{results_count: 10},
  oauth_token: "your-oauth-token"
)

# Query with metadata filters
{:ok, response} = ExLLM.Providers.Gemini.Corpus.query_corpus(
  "corpora/my-corpus",
  "machine learning",
  %{
    results_count: 5,
    metadata_filters: [
      %{
        key: "document.custom_metadata.category",
        conditions: [
          %{string_value: "technology", operation: "EQUAL"}
        ]
      }
    ]
  },
  oauth_token: "your-oauth-token"
)

update_corpus(corpus_name, update_data, update_mask, opts \\ [])

@spec update_corpus(String.t(), map(), [String.t()], Keyword.t()) ::
  {:ok, ExLLM.Providers.Gemini.Corpus.CorpusInfo.t()} | {:error, map()}

Updates a corpus.

Parameters

  • corpus_name - The name of the corpus to update
  • update_data - Map containing fields to update
    • :display_name - New display name
  • update_mask - List of fields to update (currently only ["displayName"] is supported)
  • opts - Options including OAuth2 token

Examples

{:ok, corpus} = ExLLM.Providers.Gemini.Corpus.update_corpus(
  "corpora/my-corpus",
  %{display_name: "Updated Name"},
  ["displayName"],
  oauth_token: "your-oauth-token"
)

valid_corpus_name?(name)

@spec valid_corpus_name?(String.t()) :: boolean()

Validates if a corpus name has the correct format.

valid_display_name?(display_name)

@spec valid_display_name?(String.t()) :: boolean()

Validates if a display name has a valid length.