AI functions in Spice provide direct integration with large language models (LLMs) and embedding models within SQL queries. These functions process text through configured model providers and return generated responses or vector embeddings.
aiInvokes large language models (LLMs) directly within SQL queries for text generation tasks. This asynchronous function processes prompts through configured model providers and returns generated text responses.
Returns a string containing the generated text response. Returns NULL for NULL input messages or when the model returns an empty response. If the underlying model call fails, the query errors out (errors are logged).
Queries execute asynchronously, processing LLM calls in parallel across rows for improved performance. Each invocation issues an asynchronous call to the specified model provider.
Concurrency: When a per-model rate controller is configured in the Spicepod, it manages concurrency and backpressure. Otherwise, concurrency falls back to DataFusion's execution.target_partitions setting. When multiple models with different providers are configured (e.g., OpenAI and Anthropic), each provider's requests are controlled independently.
Limits:
Models must be configured in spicepod.yaml under the models section. See Large Language Models for detailed configuration.
Each ai() function invocation creates an ai span in the task_history table, which tracks execution time, input prompts, model name, and row count. Child ai_completion spans capture per-row model call details.
When only one model is configured, the model name can be omitted:
Example output:
When multiple models are configured, specify which model to use:
Use the ai() function to generate search terms for enhanced search functionality:
Process multiple rows efficiently with parallel LLM calls. Each ai() invocation is capped at 1000 rows, so use LIMIT to stay within this bound:
embedGenerates vector embeddings for text using specified embedding models. Supports both single text strings and arrays of text for batch processing.
Utf8 / LargeUtf8 scalars and lists/arrays of strings are supported.ai(), embed() does not auto-select a default when only one model is configured.List<Float32> — a single embedding vector.List<List<Float32>> — one embedding vector per element, preserving the input length. NULL input elements produce NULL output elements.Embedding models must be configured in spicepod.yaml. See Embeddings for configuration details.
Generate an embedding for a single piece of text:
Generate embeddings for multiple strings in one call:
Generate embeddings to use with vector search:
For more information on configuring and using AI models, see: