fix(ai): Ask P0 禁 CoT、可澄清、禁 UPDATE_TIME 终答
CI / Go server (push) Canceled after 0s
CI / Desktop UI (push) Canceled after 0s
CI / Desktop (Tauri) cargo check (push) Canceled after 0s

prompt 禁止推理外露与编造列;信息不足允许一句澄清。
新增 SanitizeAssistantContent 剥回复中的推理草稿;业务「最近 N 天」不得默认用 information_schema.UPDATE_TIME。
This commit is contained in:
logan committed 2026-10-26 23:03:03 +08:00
1 parent dc7089857b
commit 4c68539db6
5 files changed
+154 -3

No files matched your search

+6
View File
@@ -118,6 +118,12 @@ func TestAskChatsWithoutClaimingDatabaseExecution(t *testing.T) {
if !strings.Contains(generate[0].Content, "生成一条 mysql 方言的 SQL") { if !strings.Contains(generate[0].Content, "生成一条 mysql 方言的 SQL") {
t.Fatalf("SQL generation lost its dedicated instruction: %s", generate[0].Content) t.Fatalf("SQL generation lost its dedicated instruction: %s", generate[0].Content)
} }
if !strings.Contains(generate[0].Content, "澄清") || strings.Contains(generate[0].Content, "只输出可执行 SQL") {
t.Fatalf("generate must allow clarification and not force executable-only: %s", generate[0].Content)
}
if !strings.Contains(generate[0].Content, "禁止编造") || !strings.Contains(generate[0].Content, "UPDATE_TIME") {
t.Fatalf("system rules missing invent/UPDATE_TIME guards: %s", generate[0].Content)
}
offline := chatStub(request) offline := chatStub(request)
if offline.SQL != "" || !strings.Contains(offline.Content, "离线体验无法回答") { if offline.SQL != "" || !strings.Contains(offline.Content, "离线体验无法回答") {
t.Fatalf("offline Ask must not pretend to generate or query: %+v", offline) t.Fatalf("offline Ask must not pretend to generate or query: %+v", offline)
+3 -3
View File
@@ -14,13 +14,13 @@ func BuildMessages(req ChatReq) []Message {
if driver == "" { if driver == "" {
driver = "mysql" driver = "mysql"
} }
sys := "你是资深数据库工程师助手,服务于数据库客户端 OpsxDB。回答使用中文,SQL 代码放在 ```sql 代码块中。" sys := "你是资深数据库工程师助手,服务于数据库客户端 OpsxDB。回答使用中文。SQL 放在 ```sql 代码块中。\n规则:\n- 禁止输出思考过程、推理草稿、内心独白或逐步分析;只给用户最终答案。\n- 禁止编造上下文中未出现的列名或表结构。\n- 缺表/缺时间列/意图不清时:用一两句中文澄清(可问具体表与时间字段),不要为了「可执行」硬造 SQL。\n- 「最近 N 天数据变化」默认指业务行时间过滤;不得默认用 information_schema.TABLES.UPDATE_TIME 充当终答(除非用户明确要表元数据更新时间)。"
var task string var task string
switch strings.ToLower(req.Action) { switch strings.ToLower(req.Action) {
case "generate": case "generate":
task = fmt.Sprintf("根据用户描述生成一条 %s 方言的 SQL。只输出可执行 SQL 与极简说明。", driver) task = fmt.Sprintf("根据用户描述生成一条 %s 方言的 SQL。结构足够时:极简说明(≤2 行)+ 一条可执行 SQL;结构不足或缺时间列时:只输出一两句澄清问题,不要编造列名,不要输出思考过程。", driver)
case "ask": case "ask":
task = "回答用户关于当前数据库、数据或 SQL 的问题;只有用户明确要求编写 SQL 时才给出 SQL。你没有调用数据库工具,不要声称已经查询或修改数据。" task = "回答用户关于当前数据库、数据或 SQL 的问题;只有用户明确要求编写 SQL 时才给出 SQL(极简说明≤2 行 + SQL 块)。结构不足时一句澄清即可。你没有调用数据库工具,不要声称已经查询或修改数据。禁止输出思考过程。"
case "explain": case "explain":
task = "逐部分解释用户给出的 SQL:访问路径、索引使用、潜在代价。" task = "逐部分解释用户给出的 SQL:访问路径、索引使用、潜在代价。"
case "optimize": case "optimize":
+1
View File
@@ -191,6 +191,7 @@ func (p *RemoteProvider) Complete(ctx context.Context, req ChatReq) (ChatRes, er
if err != nil { if err != nil {
return ChatRes{}, err return ChatRes{}, err
} }
content = SanitizeAssistantContent(content)
return ChatRes{Content: content, SQL: extractSQL(content)}, nil return ChatRes{Content: content, SQL: extractSQL(content)}, nil
} }
+111
View File
@@ -0,0 +1,111 @@
package ai
import (
"regexp"
"strings"
"unicode/utf8"
)
var (
anyFence = regexp.MustCompile("(?s)```[a-zA-Z]*\\s*\n.*?```")
cotCue = regexp.MustCompile(`(?i)(我们需要|作为助手|指令要求|让我们看看|最终答案|chain of thought|step by step|I need to|as an assistant|the instruction)`)
sqlFenceFind = regexp.MustCompile("(?s)```sql\\s*\n(.+?)```")
)
// SanitizeAssistantContent 去掉 Ask/Generate 回复里的推理草稿,只保留短说明 + SQL 块(或短澄清)。
func SanitizeAssistantContent(content string) string {
s := strings.TrimSpace(content)
if s == "" {
return s
}
fences := anyFence.FindAllStringIndex(s, -1)
if len(fences) == 0 {
if utf8.RuneCountInString(s) <= 240 && !cotCue.MatchString(s) {
return s
}
return keepLeadingLines(s, 2)
}
var b strings.Builder
prev := 0
for i, loc := range fences {
prose := strings.TrimSpace(s[prev:loc[0]])
if prose != "" {
if i == 0 {
prose = trimProse(prose)
} else {
prose = keepLeadingLines(prose, 2)
}
if prose != "" {
if b.Len() > 0 {
b.WriteByte('\n')
}
b.WriteString(prose)
}
}
if b.Len() > 0 {
b.WriteString("\n\n")
}
b.WriteString(strings.TrimSpace(s[loc[0]:loc[1]]))
prev = loc[1]
}
tail := strings.TrimSpace(s[prev:])
if tail != "" {
tail = keepLeadingLines(tail, 2)
if tail != "" {
b.WriteString("\n\n")
b.WriteString(tail)
}
}
out := strings.TrimSpace(b.String())
if out == "" {
if m := sqlFenceFind.FindString(s); m != "" {
return strings.TrimSpace(m)
}
return keepLeadingLines(s, 2)
}
return out
}
func trimProse(prose string) string {
lines := nonEmptyLines(prose)
if len(lines) == 0 {
return ""
}
var notes []string
for _, ln := range lines {
if strings.HasPrefix(ln, "说明") || strings.HasPrefix(ln, "注意") || strings.HasPrefix(ln, "提示") {
notes = append(notes, ln)
}
}
if len(notes) > 0 {
if len(notes) > 2 {
notes = notes[len(notes)-2:]
}
return strings.Join(notes, "\n")
}
joined := strings.Join(lines, "\n")
if cotCue.MatchString(joined) || utf8.RuneCountInString(joined) > 160 {
return ""
}
return keepLeadingLines(joined, 2)
}
func keepLeadingLines(s string, n int) string {
lines := nonEmptyLines(s)
if len(lines) > n {
lines = lines[:n]
}
return strings.Join(lines, "\n")
}
func nonEmptyLines(s string) []string {
raw := strings.Split(s, "\n")
out := make([]string, 0, len(raw))
for _, ln := range raw {
ln = strings.TrimSpace(ln)
if ln != "" {
out = append(out, ln)
}
}
return out
}
+33
View File
@@ -0,0 +1,33 @@
package ai
import (
"strings"
"testing"
)
func TestSanitizeAssistantContentStripsCoTKeepsSQL(t *testing.T) {
raw := `我们需要回答中文。用户说查询最近7天。作为助手我们只能根据现有信息。指令要求生成SQL。
说明:因未提供业务时间字段,以表更新时间近似。
` + "```sql\nSELECT TABLE_NAME, UPDATE_TIME\nFROM information_schema.TABLES\nWHERE TABLE_SCHEMA = 'demo';\n```"
out := SanitizeAssistantContent(raw)
if strings.Contains(out, "我们需要回答中文") || strings.Contains(out, "作为助手") {
t.Fatalf("CoT leaked: %s", out)
}
if !strings.Contains(out, "```sql") || !strings.Contains(out, "UPDATE_TIME") {
t.Fatalf("SQL fence lost: %s", out)
}
// prose should be short
prose := strings.Split(out, "```")[0]
if len([]rune(strings.TrimSpace(prose))) > 80 {
t.Fatalf("prose too long: %q", prose)
}
}
func TestSanitizeAssistantContentKeepsShortClarify(t *testing.T) {
in := "当前结构未给出时间列,请说明要查哪张表以及时间字段名。"
if got := SanitizeAssistantContent(in); got != in {
t.Fatalf("got %q", got)
}
}