prompt 禁止推理外露与编造列;信息不足允许一句澄清。 新增 SanitizeAssistantContent 剥回复中的推理草稿;业务「最近 N 天」不得默认用 information_schema.UPDATE_TIME。
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
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)
|
|
}
|
|
}
|