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.

Features

  • ai_completions: Generate AI completions from various models

  • ai_embed: Create embeddings from text

Using LangDB AI gateway through clickhouse using UDFs

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

  1. Get your LangDB credentials:

  2. Set up environment variables:

Installation

Usage Examples

Using ai_completions

Basic example with system prompt:

Advanced Parameters

You can specify additional parameters like thread_id and run_id:

Using ai_embed

Generate 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?