2026-08-26 06:41:07 +08:00
|
|
|
|
package extraction
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2026-08-29 21:56:51 +08:00
|
|
|
|
"unicode"
|
2026-08-26 06:41:07 +08:00
|
|
|
|
|
|
|
|
|
|
"data-terminal-cloud/internal/contentpolicy"
|
|
|
|
|
|
"data-terminal-cloud/internal/outbox"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type GenerationInput struct {
|
2026-08-30 10:51:32 +08:00
|
|
|
|
TenantID string
|
|
|
|
|
|
Content string
|
2026-08-26 06:41:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type GenerationResult struct {
|
2026-08-29 21:56:51 +08:00
|
|
|
|
Summary string `json:"summary"`
|
|
|
|
|
|
OpenTopic string `json:"open_topic"`
|
|
|
|
|
|
StoryTitle string `json:"story_title"`
|
|
|
|
|
|
StoryContent string `json:"story_content"`
|
|
|
|
|
|
FactContent string `json:"fact_content"`
|
|
|
|
|
|
Keywords []string `json:"keywords"`
|
|
|
|
|
|
Confidence float64 `json:"confidence"`
|
|
|
|
|
|
ModelRef string `json:"-"`
|
|
|
|
|
|
PromptVersion string `json:"-"`
|
|
|
|
|
|
AllowStory bool `json:"-"`
|
2026-08-26 06:41:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Generator interface {
|
|
|
|
|
|
Generate(context.Context, GenerationInput) (GenerationResult, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DeterministicGenerator struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (DeterministicGenerator) Generate(_ context.Context, input GenerationInput) (GenerationResult, error) {
|
|
|
|
|
|
content := strings.TrimSpace(input.Content)
|
|
|
|
|
|
if content == "" {
|
|
|
|
|
|
return GenerationResult{}, errors.New("extraction content is required")
|
|
|
|
|
|
}
|
|
|
|
|
|
return GenerationResult{
|
|
|
|
|
|
Summary: "长者在本次通话中谈到:" + content,
|
|
|
|
|
|
OpenTopic: "下次可以继续询问长者刚才提到的经历,并让长者自行补充人物、时间和地点。",
|
|
|
|
|
|
StoryTitle: "关于“" + truncateRunes(content, 24) + "”的回忆",
|
|
|
|
|
|
StoryContent: content,
|
|
|
|
|
|
FactContent: content,
|
2026-08-29 21:56:51 +08:00
|
|
|
|
Keywords: deterministicKeywords(content),
|
2026-08-26 06:41:07 +08:00
|
|
|
|
Confidence: 0.9,
|
|
|
|
|
|
ModelRef: "local-deterministic-extractor",
|
|
|
|
|
|
PromptVersion: "story-v1",
|
|
|
|
|
|
AllowStory: true,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func sensitiveGeneration(decision contentpolicy.Decision) GenerationResult {
|
|
|
|
|
|
modelRef := strings.TrimSpace(decision.ProviderRef)
|
|
|
|
|
|
if modelRef == "" {
|
|
|
|
|
|
modelRef = "local-content-safety-policy"
|
|
|
|
|
|
}
|
|
|
|
|
|
reason := safePolicyToken(decision.Reason)
|
|
|
|
|
|
return GenerationResult{
|
|
|
|
|
|
Summary: "本次通话包含需要人工谨慎处理的敏感信息,系统未生成普通故事卡片。",
|
|
|
|
|
|
OpenTopic: "如确有必要处理本次敏感内容,应由有权限人员在受控流程中单独核对。",
|
|
|
|
|
|
Confidence: 1,
|
|
|
|
|
|
ModelRef: modelRef,
|
|
|
|
|
|
PromptVersion: "summary-sensitive-filter-v1:" + reason,
|
|
|
|
|
|
AllowStory: false,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func safePolicyToken(value string) string {
|
|
|
|
|
|
value = strings.ToLower(strings.TrimSpace(value))
|
|
|
|
|
|
if value == "" || len(value) > 64 {
|
|
|
|
|
|
return "policy_block"
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, character := range value {
|
|
|
|
|
|
if (character < 'a' || character > 'z') && (character < '0' || character > '9') && character != '_' && character != '-' {
|
|
|
|
|
|
return "policy_block"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return value
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ArkGenerator struct {
|
2026-08-30 10:51:32 +08:00
|
|
|
|
endpoint string
|
|
|
|
|
|
apiKey string
|
|
|
|
|
|
model string
|
|
|
|
|
|
provider string
|
|
|
|
|
|
temperature float64
|
|
|
|
|
|
maxTokens int
|
|
|
|
|
|
client *http.Client
|
2026-08-26 06:41:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewArkGenerator(baseURL, apiKey, model string, timeout time.Duration) (*ArkGenerator, error) {
|
2026-08-30 10:51:32 +08:00
|
|
|
|
return NewCompatibleGenerator("ark", baseURL, apiKey, model, 0.2, 1200, timeout)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCompatibleGenerator(provider, baseURL, apiKey, model string, temperature float64, maxTokens int, timeout time.Duration) (*ArkGenerator, error) {
|
|
|
|
|
|
provider = strings.ToLower(strings.TrimSpace(provider))
|
2026-08-26 06:41:07 +08:00
|
|
|
|
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
|
|
|
|
apiKey = strings.TrimSpace(apiKey)
|
|
|
|
|
|
model = strings.TrimSpace(model)
|
|
|
|
|
|
parsed, err := url.Parse(baseURL)
|
|
|
|
|
|
if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
|
|
|
|
|
return nil, errors.New("Ark base URL must be an absolute HTTP(S) URL")
|
|
|
|
|
|
}
|
2026-08-30 10:51:32 +08:00
|
|
|
|
if provider != "ark" && provider != "openai_compatible" {
|
|
|
|
|
|
return nil, errors.New("content model provider must be ark or openai_compatible")
|
|
|
|
|
|
}
|
|
|
|
|
|
if apiKey == "" || model == "" || timeout <= 0 || temperature < 0 || temperature > 2 || maxTokens < 256 || maxTokens > 8192 {
|
|
|
|
|
|
return nil, errors.New("content model credentials and valid generation parameters are required")
|
2026-08-26 06:41:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
return &ArkGenerator{
|
|
|
|
|
|
endpoint: baseURL + "/chat/completions", apiKey: apiKey, model: model,
|
2026-08-30 10:51:32 +08:00
|
|
|
|
provider: provider, temperature: temperature, maxTokens: maxTokens,
|
2026-08-26 06:41:07 +08:00
|
|
|
|
client: &http.Client{Timeout: timeout},
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (g *ArkGenerator) Generate(ctx context.Context, input GenerationInput) (GenerationResult, error) {
|
|
|
|
|
|
content := strings.TrimSpace(input.Content)
|
|
|
|
|
|
if content == "" {
|
|
|
|
|
|
return GenerationResult{}, errors.New("extraction content is required")
|
|
|
|
|
|
}
|
|
|
|
|
|
requestBody := map[string]any{
|
|
|
|
|
|
"model": g.model,
|
|
|
|
|
|
"messages": []map[string]string{
|
2026-08-29 21:56:51 +08:00
|
|
|
|
{"role": "system", "content": "你是长者口述内容整理助手。只依据输入字幕生成可人工核对的候选,不补充未出现的事实,不把推断写成事实。必须返回JSON对象,字段为summary、keywords、open_topic、story_title、story_content、fact_content、confidence。summary概括本次内容,keywords是1至8个简短关键词字符串组成的数组,open_topic给出一个温和的后续话题,story和fact保留原意,confidence取0到1。"},
|
2026-08-26 06:41:07 +08:00
|
|
|
|
{"role": "user", "content": content},
|
|
|
|
|
|
},
|
2026-08-30 10:51:32 +08:00
|
|
|
|
"temperature": g.temperature,
|
|
|
|
|
|
"max_tokens": g.maxTokens,
|
2026-08-26 06:41:07 +08:00
|
|
|
|
"response_format": map[string]string{"type": "json_object"},
|
|
|
|
|
|
}
|
|
|
|
|
|
encoded, err := json.Marshal(requestBody)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return GenerationResult{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, g.endpoint, bytes.NewReader(encoded))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return GenerationResult{}, fmt.Errorf("build Ark content request: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+g.apiKey)
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
req.Header.Set("User-Agent", "voice-platform-content-extractor/1.0")
|
|
|
|
|
|
resp, err := g.client.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(fmt.Errorf("call Ark content model: %w", err))
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
responseBody, readErr := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(fmt.Errorf("read Ark content response: %w", readErr))
|
|
|
|
|
|
}
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
|
requestErr := fmt.Errorf("Ark content model returned HTTP %d", resp.StatusCode)
|
|
|
|
|
|
if resp.StatusCode == http.StatusRequestTimeout || resp.StatusCode == http.StatusTooEarly || resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(requestErr)
|
|
|
|
|
|
}
|
|
|
|
|
|
return GenerationResult{}, requestErr
|
|
|
|
|
|
}
|
|
|
|
|
|
var completion struct {
|
|
|
|
|
|
Choices []struct {
|
|
|
|
|
|
Message struct {
|
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
|
} `json:"message"`
|
|
|
|
|
|
} `json:"choices"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err = json.Unmarshal(responseBody, &completion); err != nil || len(completion.Choices) == 0 {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(errors.New("Ark content response has no usable choice"))
|
|
|
|
|
|
}
|
|
|
|
|
|
jsonContent := strings.TrimSpace(completion.Choices[0].Message.Content)
|
|
|
|
|
|
jsonContent = strings.TrimPrefix(jsonContent, "```json")
|
|
|
|
|
|
jsonContent = strings.TrimPrefix(jsonContent, "```")
|
|
|
|
|
|
jsonContent = strings.TrimSuffix(jsonContent, "```")
|
|
|
|
|
|
var result GenerationResult
|
|
|
|
|
|
if err = json.Unmarshal([]byte(strings.TrimSpace(jsonContent)), &result); err != nil {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(fmt.Errorf("decode Ark structured content: %w", err))
|
|
|
|
|
|
}
|
|
|
|
|
|
result.Summary = strings.TrimSpace(result.Summary)
|
|
|
|
|
|
result.OpenTopic = strings.TrimSpace(result.OpenTopic)
|
|
|
|
|
|
result.StoryTitle = strings.TrimSpace(result.StoryTitle)
|
|
|
|
|
|
result.StoryContent = strings.TrimSpace(result.StoryContent)
|
|
|
|
|
|
result.FactContent = strings.TrimSpace(result.FactContent)
|
2026-08-29 21:56:51 +08:00
|
|
|
|
result.Keywords = normalizedKeywords(result.Keywords)
|
2026-08-26 06:41:07 +08:00
|
|
|
|
if !validGeneratedContent(result) {
|
|
|
|
|
|
return GenerationResult{}, outbox.Retryable(errors.New("Ark structured content failed validation"))
|
|
|
|
|
|
}
|
2026-08-30 10:51:32 +08:00
|
|
|
|
result.ModelRef = g.provider + ":" + g.model
|
|
|
|
|
|
result.PromptVersion = "story-" + g.provider + "-json-v2"
|
2026-08-26 06:41:07 +08:00
|
|
|
|
result.AllowStory = true
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func validGeneratedContent(result GenerationResult) bool {
|
|
|
|
|
|
return result.Summary != "" && len([]rune(result.Summary)) <= 2000 &&
|
|
|
|
|
|
result.OpenTopic != "" && len([]rune(result.OpenTopic)) <= 1000 &&
|
|
|
|
|
|
result.StoryTitle != "" && len([]rune(result.StoryTitle)) <= 300 &&
|
|
|
|
|
|
result.StoryContent != "" && len([]rune(result.StoryContent)) <= 20000 &&
|
|
|
|
|
|
result.FactContent != "" && len([]rune(result.FactContent)) <= 5000 &&
|
2026-08-29 21:56:51 +08:00
|
|
|
|
len(result.Keywords) >= 1 && len(result.Keywords) <= 8 &&
|
2026-08-26 06:41:07 +08:00
|
|
|
|
result.Confidence >= 0 && result.Confidence <= 1
|
|
|
|
|
|
}
|
2026-08-29 21:56:51 +08:00
|
|
|
|
|
|
|
|
|
|
func normalizedKeywords(values []string) []string {
|
|
|
|
|
|
result := make([]string, 0, min(len(values), 8))
|
|
|
|
|
|
seen := make(map[string]struct{}, len(values))
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
|
if value == "" || len([]rune(value)) > 40 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if _, exists := seen[value]; exists {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
seen[value] = struct{}{}
|
|
|
|
|
|
result = append(result, value)
|
|
|
|
|
|
if len(result) == 8 {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func deterministicKeywords(content string) []string {
|
|
|
|
|
|
parts := strings.FieldsFunc(content, func(character rune) bool {
|
|
|
|
|
|
return unicode.IsSpace(character) || strings.ContainsRune(",。!?;、,.!?;::\"“”‘’()()", character)
|
|
|
|
|
|
})
|
|
|
|
|
|
candidates := make([]string, 0, len(parts))
|
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
|
runes := []rune(strings.TrimSpace(part))
|
|
|
|
|
|
if len(runes) < 2 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(runes) > 12 {
|
|
|
|
|
|
runes = runes[:12]
|
|
|
|
|
|
}
|
|
|
|
|
|
candidates = append(candidates, string(runes))
|
|
|
|
|
|
}
|
|
|
|
|
|
keywords := normalizedKeywords(candidates)
|
|
|
|
|
|
if len(keywords) > 0 {
|
|
|
|
|
|
return keywords
|
|
|
|
|
|
}
|
|
|
|
|
|
runes := []rune(content)
|
|
|
|
|
|
if len(runes) > 12 {
|
|
|
|
|
|
runes = runes[:12]
|
|
|
|
|
|
}
|
|
|
|
|
|
return []string{string(runes)}
|
|
|
|
|
|
}
|