Search...
ctrl/
Light
Dark
System
Sign in

ext::ai​

This reference documents the Gel ext::ai extension components, configuration options, and database APIs.

The AI extension can be enabled using the extension mechanism:

Copy
using extension ai;

The AI extension can be configured using configure session or configure current branch:

Copy
configure current branch
set ext::ai::Config::indexer_naptime := <duration>'PT30S';
  • indexer_naptime: Duration

    Specifies minimum delay between deferred ext::ai::index indexer runs.

View current configuration:

Copy
select cfg::Config.extensions[is ext::ai::Config]{*};

Reset configuration:

Copy
configure current branch
reset ext::ai::Config::indexer_naptime;

The AI section of the UI can be accessed via the sidebar after the extension has been enabled in the schema. It provides ways to manage provider configurations and RAG prompts, as well as try out different settings in the playground.

Provides an interactive environment for testing and configuring the built-in RAG.

Screenshot of the Playground tab of the UI depicting an empty message window and three input fields set with default values.

Components:

  • Message window: Displays conversation history between the user and the LLM.

  • Model: Dropdown menu for selecting the text generation model.

  • Prompt: Dropdown menu for selecting the RAG prompt template.

  • Context Query: Input field for entering an EdgeQL expression returning a set of objects with AI indexes.

Provides ways to manage system prompts used in the built-in RAG.

Screenshot of the Prompts tab of the UI depicting an expanded prompt configuration menu.

Enables management of API configurations for AI API providers.

Screenshot of the Providers tab of the UI depicting an expanded provider configuration menu.

The ext::ai::index creates a deferred semantic similarity index of an expression on a type.

Copy
module default {
  type Astronomy {
    content: str;
    deferred index ext::ai::index(embedding_model := 'text-embedding-3-small')
      on (.content);
  }
};

Parameters:

  • embedding_model- The name of the model to use for embedding generation as a string.

  • distance_function- The function to use for determining semantic similarity. Default: ext::ai::DistanceFunction.Cosine

  • index_type- The type of index to create. Currently the only option is the default: ext::ai::IndexType.HNSW.

  • index_parameters- A named tuple of additional index parameters:

    • m- The maximum number of edges of each node in the graph. Increasing can increase the accuracy of searches at the cost of index size. Default: 32

    • ef_construction- Dictates the depth and width of the search when building the index. Higher values can lead to better connections and more accurate results at the cost of time and resource usage when building the index. Default: 100

  • dimensions: int64 (Optional) - Embedding dimensions

  • truncate_to_max: bool (Default: False)

ext::ai::to_context()

Returns the indexed expression value for an object with an ext::ai::index.

ext::ai::search()

Searches objects using their ai::index.

function

ext::ai::to_context()
ext::ai::to_context(object: anyobject) -> str

Returns the indexed expression value for an object with an ext::ai::index.

Example:

Schema:

Copy
module default {
  type Astronomy {
    topic: str;
    content: str;
    deferred index ext::ai::index(embedding_model := 'text-embedding-3-small')
      on (.topic ++ ' ' ++ .content);
  }
};

Data:

Copy
db> 
... 
... 
... 
insert Astronomy {
  topic := 'Mars',
  content := 'Skies on Mars are red.'
}
Copy
db> 
... 
... 
... 
insert Astronomy {
  topic := 'Earth',
  content := 'Skies on Earth are blue.'
}

Results of calling to_context:

Copy
db> 
select ext::ai::to_context(Astronomy);

{'Mars Skies on Mars are red.', 'Earth Skies on Earth are blue.'}

function

ext::ai::search()
ext::ai::search( object: anyobject, query: array<float32> ) -> optional tuple<object: anyobject, distance: float64>

Searches objects using their ai::index.

Returns tuples of (object, distance).

The query argument should not be a textual query but the embeddings generated from a textual query.

Copy
db> 
... 
with query := <array<float32>><json>$query
select ext::ai::search(Knowledge, query);

{
  (
    object := default::Knowledge {id: 9af0d0e8-0880-11ef-9b6b-4335855251c4},
    distance := 0.20410746335983276
  ),
  (
    object := default::Knowledge {id: eeacf638-07f6-11ef-b9e9-57078acfce39},
    distance := 0.7843298847773637
  ),
  (
    object := default::Knowledge {id: f70863c6-07f6-11ef-b9e9-3708318e69ee},
    distance := 0.8560434728860855
  ),
}

ext::ai::ProviderAPIStyle

Enum defining supported API styles

ext::ai::ProviderConfig

Abstract base configuration for AI providers.

Provider configurations are required for AI indexes and RAG functionality.

Example provider configuration:

Copy
configure current database
insert ext::ai::OpenAIProviderConfig {
  secret := 'sk-....',
};

All provider types require the secret property be set with a string containing the secret provided by the AI vendor.

ext::ai::CustomProviderConfig requires an ``api_style property be set.

type

ext::ai::ProviderAPIStyle
ProviderAPIStyle

Enum defining supported API styles:

  • OpenAI

  • Anthropic

type

ext::ai::ProviderConfig
ProviderConfig

Abstract base configuration for AI providers.

Properties:

  • name: str (Required) - Unique provider identifier

  • display_name: str (Required) - Human-readable name

  • api_url: str (Required) - Provider API endpoint

  • client_id: str (Optional) - Provider-supplied client ID

  • secret: str (Required) - Provider API secret

  • api_style: ProviderAPIStyle (Required) - Provider's API style

Provider-specific types:

  • ext::ai::OpenAIProviderConfig

  • ext::ai::MistralProviderConfig

  • ext::ai::AnthropicProviderConfig

  • ext::ai::CustomProviderConfig

Each inherits from ext::ai::ProviderConfig with provider-specific defaults.

OpenAI (documentation)

  • text-embedding-3-small

  • text-embedding-3-large

  • text-embedding-ada-002

Mistral (documentation)

  • mistral-embed

OpenAI (documentation)

  • gpt-3.5-turbo

  • gpt-4-turbo-preview

Mistral (documentation)

  • mistral-small-latest

  • mistral-medium-latest

  • mistral-large-latest

Anthropic (documentation)

  • claude-3-haiku-20240307

  • claude-3-sonnet-20240229

  • claude-3-opus-20240229

ext::ai::Model

Abstract base type for AI models.

ext::ai::EmbeddingModel

Abstract type for embedding models.

ext::ai::TextGenerationModel

Abstract type for text generation models.

type

ext::ai::Model
Model

Abstract base type for AI models.

Annotations: * model_name - Model identifier * model_provider - Provider identifier

type

ext::ai::EmbeddingModel
EmbeddingModel

Abstract type for embedding models.

Annotations: * embedding_model_max_input_tokens - Maximum tokens per input * embedding_model_max_batch_tokens - Maximum tokens per batch * embedding_model_max_output_dimensions - Maximum embedding dimensions * embedding_model_supports_shortening - Input shortening support flag

type

ext::ai::TextGenerationModel
TextGenerationModel

Abstract type for text generation models.

Annotations: * text_gen_model_context_window - Model's context window size

ext::ai::DistanceFunction

Enum for similarity metrics.

ext::ai::IndexType

Enum for index implementations.

type

ext::ai::DistanceFunction
DistanceFunction

Enum for similarity metrics.

  • Cosine

  • InnerProduct

  • L2

type

ext::ai::IndexType
IndexType

Enum for index implementations.

  • HNSW

ext::ai::ChatParticipantRole

Enum for chat roles.

ext::ai::ChatPromptMessage

Type for chat prompt messages.

ext::ai::ChatPrompt

Type for chat prompt configuration.

Example custom prompt configuration:

Copy
insert ext::ai::ChatPrompt {
  name := 'test-prompt',
  messages := (
    insert ext::ai::ChatPromptMessage {
      participant_role := ext::ai::ChatParticipantRole.System,
      content := "Your message content"
    }
  )
};

type

ext::ai::ChatParticipantRole
ChatParticipantRole

Enum for chat roles.

  • System

  • User

  • Assistant

  • Tool

type

ext::ai::ChatPromptMessage
ChatPromptMessage

Type for chat prompt messages.

Properties: * participant_role: ChatParticipantRole (Required) * participant_name: str (Optional) * content: str (Required)

type

ext::ai::ChatPrompt
ChatPrompt

Type for chat prompt configuration.

Properties: * name: str (Required) * messages: set of ChatPromptMessage (Required)