deepthought/
deepthought_router_prompt.rs

1extern crate log;
2
3use easy_error::bail;
4use minijinja::context;
5
6use crate::*;
7
8pub const DEFAULT_REFINE_PROMPT: &str = r#"
9You are “Prompt Refiner”. Your job is to transform rough prompts into precise, testable instructions for other models
10
11Operating rules:
12- Preserve the user’s intent and domain terms.
13- Remove ambiguity; add only minimal missing constraints.
14- Do not reveal chain‑of‑thought or internal reasoning.
15- If essential info is missing (objective, inputs, audience, format, constraints, or success criteria), do not ask questions.
16- Output valid JSON that matches the schema EXACTLY. No extra text outside JSON. Keep total tokens ≤ 1200.
17
18Output schema (must match precisely):
19{
20  "raw_prompt": "string",
21  "clarifying_questions": [string],                // include only if essential details are missing; ≤3 items
22  "prompts": {
23    "deterministic": "string",                     // concise, reproducible
24    "balanced": "string",                          // practical default
25    "creative": "string"                           // more open-ended
26  },
27  "rationale_bullets": [string],                   // 3–6 short bullets; no CoT
28  "suggested_parameters": {
29    "temperature": number,
30    "top_p": number,
31    "max_tokens": number,
32    "stop": [string],
33    "seed": number
34  },
35  "quick_tests": [string, string, string]          // 3 short inputs to validate the prompt
36}
37
38Quality self‑check (silent): Clarity, Context, Constraints, Format, Evaluation, Safety. In the field "raw_prompt" store original prompt.
39If any check fails, fix and still return schema‑valid JSON only.
40
41
42Task: Improve the following prompt and return JSON ONLY per the system schema.
43
44Rough prompt:
45<<<
46{{ prompt }}
47>>>
48
49"#;
50
51impl DeepThoughtRouter {
52    pub fn refine_prompt(
53        &mut self,
54        prompt: &str,
55    ) -> Result<DeepThoughtRecommededPrompt, easy_error::Error> {
56        match self.prompt_model {
57            Some(ref mut prompt_model) => {
58                let mut ctx = match DeepThoughtContext::init(
59                    "You are “Prompt Refiner”. Your job is to transform rough prompts into precise, testable instructions for other models.",
60                ) {
61                    Ok(ctx) => ctx,
62                    Err(err) => bail!("{}", err),
63                };
64                let true_prompt = match DeepThoughtRouter::template(
65                    DEFAULT_REFINE_PROMPT,
66                    context! {
67                        prompt => prompt,
68                    },
69                ) {
70                    Ok(true_prompt) => true_prompt,
71                    Err(err) => bail!("{}", err),
72                };
73                let result = match prompt_model.chat(&true_prompt, &mut ctx) {
74                    Ok(result) => result,
75                    Err(err) => bail!("{}", err),
76                };
77                let recommended_prompt: DeepThoughtRecommededPrompt =
78                    match serde_json::from_str(&result) {
79                        Ok(recommended_prompt) => recommended_prompt,
80                        Err(err) => bail!("{}", err),
81                    };
82                return Ok(recommended_prompt);
83            }
84            None => bail!("Prompt model not configured. Prompt refining is impossible"),
85        }
86    }
87}