使用简单的 DuckDB 扩展与 LLM 交互
安装和加载
INSTALL open_prompt FROM community;
LOAD open_prompt;
示例
-- 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 'https://: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
}');
关于 open_prompt
Open Prompt 扩展
open_prompt()
社区扩展很大程度上受到了 Motherduck prompt()
的启发,但侧重于自托管使用。
有关示例和说明,请查看
open_prompt()
README
配置
设置 completions API URL 配置,包含可选的身份验证令牌和模型名称
SET VARIABLE openprompt_api_url = 'https://:11434/v1/chat/completions';
SET VARIABLE openprompt_api_token = 'your_api_key_here';
SET VARIABLE openprompt_model_name = 'qwen2.5:0.5b';
或者,可以在运行时使用以下 ENV 变量
OPEN_PROMPT_API_URL='https://:11434/v1/chat/completions'
OPEN_PROMPT_API_TOKEN='your_api_key_here'
OPEN_PROMPT_MODEL_NAME='qwen2.5:0.5b'
OPEN_PROMPT_API_TIMEOUT='30'
对于持久使用,请使用 DuckDB SECRETS
配置参数
CREATE PERSISTENT SECRET IF NOT EXISTS open_prompt (
TYPE open_prompt,
PROVIDER config,
api_token 'your-api-token',
api_url 'https://:11434/v1/chat/completions',
model_name 'qwen2.5:0.5b',
api_timeout '30'
);
新增函数
function_name (函数名) | 函数类型 | description (描述) | comment (注释) | examples (例子) |
---|---|---|---|---|
open_prompt | 标量 | NULL (空) | NULL (空) | |
set_api_timeout (设置 API 超时) | 标量 | NULL (空) | NULL (空) | |
set_api_token (设置 API 令牌) | 标量 | NULL (空) | NULL (空) | |
set_api_url (设置 API URL) | 标量 | NULL (空) | NULL (空) | |
set_model_name (设置模型名称) | 标量 | NULL (空) | NULL (空) |