Skip to content
DuckDB community extension

open_prompt

Interact with LLMs with a simple DuckDB Extension

01 / SETUP

Install and load

Run these commands in a Haybarn SQL session. The extension is available from the community repository.

Haybarn 2.0.0 Β· SQL
INSTALL open_prompt FROM community;
LOAD open_prompt FROM community;

Try it in a temporary SQL session with open_prompt already loaded. Queries run in this tab; examples using files or services need their own setup.

Using upstream DuckDB? Follow the maintainer’s installation instructions.

02 / QUERY

Usage example

Configure the required parameters to access OpenAI Completions compatible APIs

SQL session and output
-- Configure the required parameters to access OpenAI Completions compatible APIs
D CREATE SECRET IF NOT EXISTS open_prompt (
      TYPE open_prompt,
      PROVIDER config,
      api_token 'your-api-token',
      api_url 'http://localhost:11434/v1/chat/completions',
      model_name 'qwen2.5:0.5b',
      api_timeout '30'
  );

-- Prompt any OpenAI Completions API form your query
D SELECT open_prompt('Write a one-line poem about ducks') AS response;
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    response                    β”‚
β”‚                    varchar                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Ducks quacking at dawn, swimming in the light. β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

-- Prompt requesting JSON Structured Output for ChatGPT, LLama3, etc
SET VARIABLE openprompt_model_name = 'llama3.2:3b';
SELECT open_prompt('I want ice cream', json_schema := '{
   "type": "object",
   "properties": {
     "summary": { "type": "string" },
     "sentiment": { "type": "string", "enum": ["pos", "neg", "neutral"] }
   },
   "required": ["summary", "sentiment"],
   "additionalProperties": false
 }');

-- Use Custom System Prompt to request JSON Output in smaller models
SET VARIABLE openprompt_model_name = 'qwen2.5:1.5b';
SELECT open_prompt('I want ice cream.', system_prompt:='Response MUST be JSON with the following schema: {
       "type": "object",
       "properties": {
         "summary": { "type": "string" },
         "sentiment": { "type": "string", "enum": ["pos", "neg", "neutral"] }
       },
       "required": ["summary", "sentiment"],
       "additionalProperties": false
     }');

Provided by quackscience. Not yet verified on Haybarn 2.0.0.

Sample data & setup

About this extension

The open_prompt() community extension is shamelessly inspired by the Motherduck prompt() but focused on self-hosted usage.

Setup the completions API URL configuration w/ optional auth token and model name

Project overview from the community descriptor.

Documentation and resources

Project and build details
Repository
quackscience/duckdb-extension-openprompt
License
MIT
Maintainers
lmangani, akvlad
Written in
C++
Build source
a562370dfb02
Build definition
Community descriptor