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 if an error occurs during processing (errors are logged).
Queries execute asynchronously, processing LLM calls in parallel across rows for improved performance. Each invocation queues an asynchronous call to the specified model provider.
Concurrency: The function honors DataFusion concurrency configuration for parallel requests. When multiple models with different providers are configured (e.g., OpenAI and Anthropic), each provider processes requests in parallel according to concurrency settings.
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 task in the task_history table, which tracks execution time, input prompts, outputs, and errors.
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:
embedGenerates vector embeddings for text using specified embedding models. Supports both single text strings and arrays of text for batch processing.
Returns a list of floating-point values representing the embedding vector. For array inputs, returns embeddings for each element, preserving the input array length including NULL values.
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: