WeaviateEx.API.Generative (WeaviateEx v0.7.4)

View Source

Generative Search (RAG) operations for Phase 2.3.

Provides AI-powered generation capabilities with 20+ provider integrations:

  • OpenAI (GPT-4, GPT-3.5, O1, O3 reasoning models, etc.)
  • Anthropic (Claude 3.5 Sonnet, etc.)
  • Cohere
  • Google PaLM / Vertex AI / Gemini
  • AWS Bedrock / SageMaker
  • Azure OpenAI
  • Anyscale
  • Hugging Face
  • Mistral
  • Ollama (local models)
  • OctoAI
  • Together AI
  • Voyage AI
  • XAI (Grok)
  • ContextualAI
  • NVIDIA NIM
  • Databricks
  • FriendliAI

Summary

Functions

Generate per-object results (grouped task).

Generate a single result from all retrieved objects.

List all supported providers.

Check if provider is valid.

Add generation clause to existing query map.

Types

collection_name()

@type collection_name() :: String.t()

opts()

@type opts() :: keyword()

prompt()

@type prompt() :: String.t()

provider()

@type provider() ::
  :openai
  | :anthropic
  | :cohere
  | :palm
  | :google_vertex
  | :google_gemini
  | :aws_bedrock
  | :aws_sagemaker
  | :azure_openai
  | :anyscale
  | :huggingface
  | :mistral
  | :ollama
  | :octoai
  | :together
  | :voyage
  | :xai
  | :contextualai
  | :nvidia
  | :databricks
  | :friendliai

Functions

grouped_task(client, collection_name, prompt, opts \\ [])

@spec grouped_task(WeaviateEx.Client.t(), collection_name(), prompt(), opts()) ::
  {:ok, [map()]} | {:error, WeaviateEx.Error.t()}

Generate per-object results (grouped task).

Each retrieved object gets its own generated result with property interpolation.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • prompt - Generation prompt with optional {property} interpolation
  • opts - Options (same as single_prompt/4)

Examples

{:ok, results} = Generative.grouped_task(client, "Article",
  "Summarize: {title}",
  provider: :openai,
  limit: 10
)

# Each result has _additional.generate.groupedResult

Returns

  • {:ok, [map()]} - List of objects with generated results
  • {:error, Error.t()} - Error if generation fails

single_prompt(client, collection_name, prompt, opts \\ [])

@spec single_prompt(WeaviateEx.Client.t(), collection_name(), prompt(), opts()) ::
  {:ok, map()} | {:error, WeaviateEx.Error.t()}

Generate a single result from all retrieved objects.

The prompt can use property interpolation with {property_name} syntax.

Parameters

  • client - WeaviateEx client
  • collection_name - Name of the collection
  • prompt - Generation prompt with optional {property} interpolation
  • opts - Options:
    • :provider - AI provider (required)
    • :model - Model name (provider-specific)
    • :properties - Properties to retrieve for interpolation
    • :near_text - Semantic search query
    • :where - Filter conditions
    • :limit - Number of objects to retrieve
    • :temperature - Sampling temperature (0.0-1.0)
    • :max_tokens - Maximum tokens to generate
    • :top_p - Nucleus sampling parameter

Examples

# Basic generation with OpenAI
{:ok, result} = Generative.single_prompt(client, "Article",
  "Summarize these articles about {title}",
  provider: :openai
)

# With Anthropic Claude
{:ok, result} = Generative.single_prompt(client, "Article",
  "Explain {title} in simple terms",
  provider: :anthropic,
  model: "claude-3-5-sonnet-20241022",
  temperature: 0.7
)

# With semantic search
{:ok, result} = Generative.single_prompt(client, "Article",
  "What are the main themes?",
  near_text: "artificial intelligence",
  provider: :openai,
  max_tokens: 500
)

Returns

  • {:ok, map()} - Generated result with "singleResult" and "error" keys
  • {:error, Error.t()} - Error if generation fails

supported_providers()

@spec supported_providers() :: [provider()]

List all supported providers.

valid_provider?(provider)

@spec valid_provider?(atom()) :: boolean()

Check if provider is valid.

Examples

Generative.valid_provider?(:openai)  # => true
Generative.valid_provider?(:invalid) # => false

with_generate(query, prompt, opts \\ [])

@spec with_generate(map(), prompt(), opts()) :: map()

Add generation clause to existing query map.

This is a helper for query builder integration.

Examples

query = %{collection: "Article", fields: ["title"]}
query = Generative.with_generate(query, "Summarize {title}", provider: :openai)

Returns

  • query - Modified query map with generate clause