Skip to main content

vlorql_core/prompt/
skill.rs

1//! Prompt skills for custom LLM instruction injection.
2#![allow(missing_docs)]
3
4use crate::errors::{ConfigErrorKind, VlorQLError};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use serde_json::json;
8use std::path::Path;
9
10/// A pair of (question, expected plan) for few-shot examples.
11#[allow(missing_docs)]
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13pub struct ExamplePair {
14    pub question: String,
15    pub plan: serde_json::Value,
16}
17
18/// A skill that injects custom instructions, simplifies schema, or adds examples.
19#[allow(missing_docs)]
20#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
21pub struct PromptSkill {
22    pub name: String,
23    pub description: Option<String>,
24    pub instructions: Vec<String>,
25    pub simplify_schema: bool,
26    pub forbid_features: Vec<String>,
27    pub disable_output_fields: Vec<String>,
28    pub examples: Vec<ExamplePair>,
29}
30
31impl PromptSkill {
32    pub fn load_toml(path: impl AsRef<Path>) -> Result<Self, VlorQLError> {
33        let path = path.as_ref();
34        let content = std::fs::read_to_string(path).map_err(|e| {
35            VlorQLError::config(
36                ConfigErrorKind::ConfigFileError {
37                    path: path.display().to_string(),
38                    reason: e.to_string(),
39                },
40                json!({"path": path.display().to_string()}),
41            )
42        })?;
43        toml::from_str(&content).map_err(|e| {
44            VlorQLError::config(
45                ConfigErrorKind::ConfigFileError {
46                    path: path.display().to_string(),
47                    reason: e.to_string(),
48                },
49                json!({"path": path.display().to_string(), "parse_error": e.to_string()}),
50            )
51        })
52    }
53
54    pub fn load_yaml(path: impl AsRef<Path>) -> Result<Self, VlorQLError> {
55        let path = path.as_ref();
56        let content = std::fs::read_to_string(path).map_err(|e| {
57            VlorQLError::config(
58                ConfigErrorKind::ConfigFileError {
59                    path: path.display().to_string(),
60                    reason: e.to_string(),
61                },
62                json!({"path": path.display().to_string()}),
63            )
64        })?;
65        serde_yaml::from_str(&content).map_err(|e| {
66            VlorQLError::config(
67                ConfigErrorKind::ConfigFileError {
68                    path: path.display().to_string(),
69                    reason: e.to_string(),
70                },
71                json!({"path": path.display().to_string(), "parse_error": e.to_string()}),
72            )
73        })
74    }
75
76    pub fn builtin_small_model() -> Self {
77        Self {
78            name: "small-model".into(),
79            description: Some("Simplifies output schema for small / local LLMs".into()),
80            instructions: vec![
81                "Keep queries simple: max 2 joins, no subqueries in WHERE.".into(),
82                "Always use table aliases (first letter of table name).".into(),
83                "Prefer LEFT JOIN over NOT IN / NOT EXISTS.".into(),
84                "GROUP BY must include every non-aggregated column in SELECT.".into(),
85            ],
86            simplify_schema: true,
87            forbid_features: vec![
88                "set_operation".into(),
89                "window_functions".into(),
90                "ctes".into(),
91                "distinct_on".into(),
92            ],
93            disable_output_fields: vec![
94                "set_operation".into(),
95                "ctes".into(),
96                "distinct_on".into(),
97            ],
98            examples: vec![ExamplePair {
99                question: "Show user names".into(),
100                plan: serde_json::json!({
101                    "select": [{"type": "column_ref", "table": "users", "column": "name", "alias": null}],
102                    "from": {"table": "users", "alias": null}
103                }),
104            }],
105        }
106    }
107}