Files
ssw-ontology/server/ai/heuristic.go
T
admin 1e30d4c355 docs+code: 通用化平台叙事,去掉尚数网业务绑定
## 文档重构(5 个 md + 1 个 html)
- README.md: '尚数网本体管理平台' → '本体元数据平台 · Ontology Platform'
- docs/README.md: 通用化定位,补全 P0/P1/P2 (Palantir Ontology 对标) + Materialization/BackingSource/Suggestions 全清单
- docs/PLAN.md: 通用化设计目标,改为 'Palantir Ontology 对标' 路线图
- docs/USAGE.md: 用通用占位符(项目/空间/组织)替换尚数网具体业务举例
- docs/IMPORT.md: 把 shangshu 当成 '一个采用方示例' 展示通用模式
- docs/product-intro.html: title/brand/hero/场景/footer 全改通用,沙数只作典型场景之一

## 代码字符串同步去 shangshu
- server/main.go: '[boot] 尚数网本体管理平台监听' → '[boot] 本体平台监听'
- server/handlers_connector.go: manifest 'server' 字段 → 'ontology-platform'
- server/handlers_mcp.go: tool description + serverInfo → 通用
- server/handlers_functions.go: builtin interface description → 通用
- server/handlers_backing_source.go: 注释去 shangshuwang.cn 具体网址
- server/config.go + server/db.go: 注释/seed 数据改占位 (demo_self / demo_partner_a/b)
- server/ai/anthropic.go: fallback systemPrompt → '通用本体平台工程师'
- server/ai/heuristic.go: 未识别 summary → '本体概念'
- server/ai/prompt.go: 静态 systemPrompt 头部 + JSON 示例去 shangshu

## 前端文案同步
- web/src/App.vue: title 改 '本体元数据平台'
- web/src/views/LoginView.vue: 登录页 h1 改通用
- web/src/views/OntologyView.vue: backing source placeholder 改 example.com
- web/src/components/ReviewWorkbench.vue + useReviewWorkbench.js: '尚数网' → '本体平台'

## 验证
- go build ./server OK
- 剩余 '尚数网' / 'shangshu-ontology' 引用:
  - 文档 README/README/USAGE/PLAN/IMPORT 仅在 '采用方示例' 上下文出现
  - 代码注释 sswOntologySystemPrompt 等 identifier 保持不变(避免改动 API 表面)
  - skill 名 'ssw-ontology-reviewer' 保持不变(已发布技能)

Refs: docs/README.md (1.1 元数据组件表)、docs/PLAN.md (4 当前已完成能力)
2026-08-24 14:04:59 +08:00

316 lines
9.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ai
import (
"context"
"fmt"
"hash/fnv"
"regexp"
"strings"
"unicode/utf8"
)
// heuristicProvider 不依赖大模型的内置启发式抽取器,保证无 Key 时平台也可运行。
// 它扫描资料中的「实体名」并结合前后文关键词,将已知概念映射为对象实例,并尽量保持幂等。
type heuristicProvider struct{}
func (h *heuristicProvider) Name() string { return "heuristic" }
// Complete 启发式提供者不支持 AI 补全. 返回空字符串 + nil (调用方据此走启发式兜底).
func (h *heuristicProvider) Complete(ctx context.Context, prompt string) (string, error) {
return "", nil
}
// CompleteWithSystem 启发式同样返回空字符串.
func (h *heuristicProvider) CompleteWithSystem(ctx context.Context, system, prompt string) (string, error) {
return "", nil
}
// 关键词 -> 对象类型(api_name) + 中文名
var keywordMap = []struct {
kw string
apiName string
display string
}{
// 业务对象
{"数据资源", "DataResource", "数据资源"},
{"资源", "DataResource", "数据资源"},
{"数据产品", "DataProduct", "数据产品"},
{"产品", "DataProduct", "数据产品"},
{"可信数据空间", "TrustedDataSpace", "可信数据空间"},
{"数据空间", "TrustedDataSpace", "可信数据空间"},
{"数据交易所", "DataExchange", "数据交易所"},
{"交易所", "DataExchange", "数据交易所"},
{"应用场景", "AppScenario", "应用场景"},
{"场景", "AppScenario", "应用场景"},
{"分支机构", "Branch", "分支机构"},
{"分公司", "Branch", "分支机构"},
{"总部", "Branch", "分支机构"},
{"会员单位", "MemberOrg", "会员单位"},
{"会员", "MemberOrg", "会员单位"},
// 硬件 / 一体机
{"密算一体机", "Appliance", "密算一体机"},
{"一体机", "Appliance", "密算一体机"},
// 服务
{"核心服务", "Service", "核心服务"},
{"服务", "Service", "核心服务"},
// 知识产权
{"专利", "IPAsset", "知识产权"},
{"软著", "IPAsset", "知识产权"},
{"商标", "IPAsset", "知识产权"},
{"标准", "IPAsset", "知识产权"},
{"认证", "IPAsset", "知识产权"},
}
// bracketRe 匹配 「x」 “x” ‘x’ (x) (x) 中的实体名
var bracketRe = regexp.MustCompile(`[「“'((]([^」”'))]+)[」”'))]`)
func (h *heuristicProvider) Suggest(ctx context.Context, text, directive, existingContext string) (*Suggestion, error) {
s := &Suggestion{}
seen := map[string]bool{}
directiveType := ""
for _, km := range keywordMap {
if strings.Contains(directive, km.kw) {
directiveType = km.apiName
break
}
}
// 1) 提取括号/书名号包裹的实体名,并在其“前文小窗口”中判定类型。
// 业务文本中类型关键词通常紧邻实体名之前(如“会员单位新增「X」”),故只取实体名前 N 个字符,
// 并用 LastIndex 取离名字最近的关键词,避免远处“数据产品”等泛化词干扰。
const beforeCtx = 16
matches := bracketRe.FindAllStringSubmatchIndex(text, -1)
for _, m := range matches {
name := strings.TrimSpace(text[m[2]:m[3]])
if len([]rune(name)) > 40 || name == "" {
continue
}
nameRuneInText := utf8.RuneCountInString(text[:m[2]])
runes := []rune(text)
bs := nameRuneInText - beforeCtx
if bs < 0 {
bs = 0
}
before := string(runes[bs:nameRuneInText])
typ := detectType(before+name, directiveType)
if typ == "" {
continue
}
key := typ + "/" + name
if seen[key] {
continue
}
seen[key] = true
s.Objects = append(s.Objects, SuggestedObject{
Type: typ,
PK: objPK(typ, name),
Properties: map[string]interface{}{"name": name},
})
}
// 2) 兜底:未匹配到括号时,按 "关键词:名称" 行式结构抽取
if len(s.Objects) == 0 {
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
typ := detectType(line, directiveType)
if typ == "" {
continue
}
name := extractName(line)
if name == "" {
continue
}
key := typ + "/" + name
if seen[key] {
continue
}
seen[key] = true
s.Objects = append(s.Objects, SuggestedObject{
Type: typ,
PK: objPK(typ, name),
Properties: map[string]interface{}{"name": name},
})
}
}
// 3) 兜底:在整段文本中寻找首个类型关键词 + 紧邻的实体名(如“密算一体机 A5 卡”)。
// 仅在前两步无任何命中时启用,避免与目录化文本重复。
if len(s.Objects) == 0 {
// 按 keywordMap 顺序遍历,每个 kw 仅取首次命中(保证多样性)
usedKinds := map[string]bool{}
for _, km := range keywordMap {
if directiveType != "" && km.apiName != directiveType {
continue
}
if usedKinds[km.apiName] {
continue
}
idx := strings.Index(text, km.kw)
if idx < 0 {
continue
}
rest := text[idx+len(km.kw):]
name := pickFirstName(rest)
if name == "" {
continue
}
usedKinds[km.apiName] = true
key := km.apiName + "/" + name
if seen[key] {
continue
}
seen[key] = true
s.Objects = append(s.Objects, SuggestedObject{
Type: km.apiName,
PK: objPK(km.apiName, name),
Properties: map[string]interface{}{"name": name},
})
}
}
// 4) 兜底:资料整体命中多种类型关键词时,每类至少落 1 条带原始资料的实例(用于让用户看到提取痕迹)。
if len(s.Objects) == 0 {
fallbackTypes := map[string]string{
"密算一体机": "Appliance",
"可信数据空间": "TrustedDataSpace",
"数据资源": "DataResource",
"数据产品": "DataProduct",
"应用场景": "AppScenario",
"核心服务": "Service",
"知识产权": "IPAsset",
}
for kw, api := range fallbackTypes {
if directiveType != "" && api != directiveType {
continue
}
if !strings.Contains(text, kw) {
continue
}
name := kw + "(未识别具体实例)"
key := api + "/" + name
if seen[key] {
continue
}
seen[key] = true
s.Objects = append(s.Objects, SuggestedObject{
Type: api,
PK: objPK(api, name),
Properties: map[string]interface{}{"name": name, "raw_excerpt": truncate(text, 120)},
})
}
}
if len(s.Objects) == 0 {
s.Summary = "启发式抽取未从资料中发现可识别的本体概念;建议配置大模型(AI_ENABLED=true)以获得更精确的抽取与关系识别。"
} else {
s.Summary = fmt.Sprintf("启发式抽取识别到 %d 个候选实例(基于关键词+实体名映射)。如需更精准的结构化抽取与关系识别,请配置 AI_ENABLED=true 并提供大模型 Key。", len(s.Objects))
}
return s, nil
}
func truncate(s string, n int) string {
if utf8.RuneCountInString(s) <= n {
return s
}
r := []rune(s)
return string(r[:n]) + "..."
}
// detectType 在给定文本窗口中查找首个匹配的关键词,返回对应对象类型。
// detectType 在给定的前文窗口中查找关键词,返回离窗口末尾(即离实体名最近)的那个关键词对应的对象类型。
func detectType(window string, directiveType string) string {
best := ""
bestPos := -1
for _, km := range keywordMap {
if directiveType != "" && km.apiName != directiveType {
continue
}
// LastIndex 取最靠近窗口末尾(=最靠近实体名)的一次出现
idx := strings.LastIndex(window, km.kw)
if idx < 0 {
continue
}
if idx > bestPos {
bestPos = idx
best = km.apiName
}
}
return best
}
// extractName 从一行文本中提取实体名称:取冒号/空格后、括号前的内容。
func extractName(line string) string {
line = strings.TrimLeft(line, "-*•· \t#")
line = strings.TrimSpace(line)
re := regexp.MustCompile(`[::]`)
parts := re.Split(line, 2)
body := parts[0]
if len(parts) > 1 && strings.TrimSpace(parts[1]) != "" && len([]rune(parts[1])) < len([]rune(parts[0])) {
body = parts[1]
}
body = strings.TrimSpace(body)
if idx := strings.IndexAny(body, "(("); idx >= 0 {
body = strings.TrimSpace(body[:idx])
}
body = strings.TrimRight(body, "。,,;;、")
body = strings.TrimSpace(body)
if len([]rune(body)) > 40 {
return ""
}
return body
}
// hasTypeKeyword 在文本中查找任意 keywordMap 关键词命中,返回 apiName。
// 若同时指定 directiveType(不为空)则只匹配该 apiName。
func hasTypeKeyword(text, directiveType string) string {
for _, km := range keywordMap {
if directiveType != "" && km.apiName != directiveType {
continue
}
if strings.Contains(text, km.kw) {
return km.apiName
}
}
return ""
}
// pickFirstName 在 text 中提取第一个像实体名的词(≥2 字汉字或纯中文短语),用于无括号、无换行的整段文本兜底。
func pickFirstName(text string) string {
rest := text
if len([]rune(rest)) > 24 {
rest = string([]rune(rest)[:24])
}
re := regexp.MustCompile(`[\p{Han}]{2,40}`)
m := re.FindString(rest)
if m == "" {
return ""
}
return m
}
// objPK 生成对象实例主键:类型前缀 + 名称的 FNV-64 哈希,保证
// (a) 仅含 ASCII、URL 安全;(b) 同一名称稳定且跨次摄取幂等;
// (c) 同类型不同名称不会像单纯 slug 那样塌缩成同一个主键。
func objPK(typ, name string) string {
h := fnv.New64a()
h.Write([]byte(name))
return slug(typ) + "_" + fmt.Sprintf("%016x", h.Sum64())
}
// slug 生成英文 snake_case 主键,保证主键合法且尽量唯一。
func slug(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
repl := strings.NewReplacer(" ", "_", "(", "_", ")", "_", "(", "_", ")", "_", "/", "_", "-", "_", ":", "_", ":", "_", "「", "", "」", "", "“", "", "”", "", "'", "", "(", "", ")", "")
s = repl.Replace(s)
re := regexp.MustCompile(`[^a-z0-9_]+`)
s = re.ReplaceAllString(s, "_")
s = strings.Trim(s, "_")
if s == "" {
s = "item"
}
return s
}