Skip to main content
Skip to main content

AI functions

AI Functions are built-in functions in ClickHouse that you can use to call AI or generate embeddings to work with your data, extract information, classify data, etc...

Note

AI functions can return unpredictable outputs. The result will highly depend on the quality of the prompt and the model used.

All functions are sharing a common infrastructure that provides:

Configuration

AI functions reference a named collection that stores provider credentials and configuration. The first argument to each function is the name of this collection.

Example statement to create a named collection with provider credentials:

CREATE NAMED COLLECTION ai_credentials AS
    provider = 'openai',
    endpoint = 'https://api.openai.com/v1/chat/completions',
    model = 'gpt-4o-mini',
    api_key = 'sk-...';

Named collection parameters

ParameterTypeDefaultDescription
providerStringModel provider. Supported: 'openai', 'anthropic'. See note below.
endpointStringAPI endpoint URL.
modelStringModel name (e.g. 'gpt-4o-mini', 'text-embedding-3-small').
api_keyStringAuthentication key for the provider.
max_tokensUInt641024Maximum number of output tokens per API call.
api_versionStringAPI version string. Used by Anthropic ('2023-06-01').
Note

Any OpenAI-compatible API (e.g. vLLM, Ollama, LiteLLM) can be used by setting provider = 'openai' and pointing the endpoint to your service.

Query-level settings

All AI-related settings are listed in Settings under the ai_function_ prefix.

Supported providers

Providerprovider valueChat functionsNotes
OpenAI'openai'YesDefault provider.
Anthropic'anthropic'YesUses /v1/messages endpoint.

Observability

AI function activity is tracked through ClickHouse ProfileEvents:

ProfileEventDescription
AIAPICallsNumber of HTTP requests made to the AI provider.
AIInputTokensTotal input tokens consumed.
AIOutputTokensTotal output tokens consumed.
AIRowsProcessedNumber of rows that received a result.
AIRowsSkippedNumber of rows skipped (quota exceeded, or error with ai_function_throw_on_error = 0).

Query these events:

SELECT
    ProfileEvents['AIAPICalls'] AS api_calls,
    ProfileEvents['AIInputTokens'] AS input_tokens,
    ProfileEvents['AIOutputTokens'] AS output_tokens
FROM system.query_log
WHERE query_id = 'query_id'
AND type = 'QueryFinish'
ORDER BY event_time DESC;