Clickhouse UDFs
Leveraging AI functions directly in your Clickhouse environment
langdb_udf adds support for AI operations directly within ClickHouse through User Defined Functions (UDFs). This enables running AI completions and embeddings natively in your SQL queries. You can access 250+ models directly in Clickhouse.
Check the full list of models supported here
You can find the full instructions in our AI Gateway repository.
Features
ai_completions: Generate AI completions from various modelsai_embed: Create embeddings from text

Why Use LangDB UDFs in ClickHouse?
LangDB UDFs are particularly powerful for running LLM-based evaluations and analysis directly within your ClickHouse environment:
Native Integration: Run AI operations directly in SQL queries without data movement
Batch Processing: Efficiently process and analyze large datasets with LLMs
Real-time Analysis: Perform content moderation, sentiment analysis, and other AI tasks as part of your data pipeline
Model Comparison: Easily compare results across different LLM models in a single query
Scalability: Leverage ClickHouse's distributed architecture for parallel AI processing
Prerequisites
Get your LangDB credentials:
Sign up at LangDB
Get your
LANGDB_PROJECT_IDandLANGDB_API_KEYDownload the latest landb_udf binary
Set up environment variables:
Installation
Usage Examples
Using ai_completions
ai_completionsBasic example with system prompt:
Advanced Parameters
You can specify additional parameters like thread_id and run_id:
Using ai_embed
ai_embedGenerate embeddings from text:
Real-world Example: Content Moderation
This example shows how to score HackerNews comments for harmful content:
Observability
If tracing is enabled you ll be able to view several metrics about the request such as cost, time, Time to First Token etc on https://app.langdb.ai/

References
Last updated
Was this helpful?