7adf88766a
Closes #1113 The graph extraction pipeline relies on a strict markdown fence regex to pull JSON out of LLM responses. In production the regex misses ~84% of real-world responses, in three ways: 1. The LLM hits max_tokens mid-output and produces no closing fence. 2. The opening fence is malformed or surrounded by prose, so the non-greedy regex fails to anchor a match. 3. The model returns raw JSON with stray backticks but no real fence. In every case extractContent fell through to returning the raw text, which then failed json.Unmarshal with errors like "invalid character '` + "`" + "' looking for beginning of value". This change keeps the existing happy path untouched and adds a conservative recovery step in the default branch of extractContent: - If an opening ``` is present, take everything after it, drop a likely language tag on the first line, cut at any trailing closing fence, and trim stray backticks/whitespace. - Otherwise, look for an outermost JSON object/array in the text using a small bracket-balanced scanner that respects string literals, so embedded {} or [] inside JSON strings don't confuse it. - Only fall back to the original raw-text behavior when neither strategy yields anything plausible. The recovery helpers (stripFencesAndExtract, extractJSONLike, isLikelyLanguageTag) are package-private and have table-driven tests in extract_entity_test.go covering the three failure patterns described in the issue, plus the previously-working fenced and bare-JSON shapes to guard against regressions.
712 lines
21 KiB
Go
712 lines
21 KiB
Go
package chatpipeline
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/Tencent/WeKnora/internal/config"
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
// PluginExtractEntity is a plugin for extracting entities from user queries
|
|
// It uses historical dialog context and large language models to identify key entities in the user's original query
|
|
type PluginExtractEntity struct {
|
|
modelService interfaces.ModelService // Model service for calling large language models
|
|
template *types.PromptTemplateStructured // Template for generating prompts
|
|
knowledgeBaseRepo interfaces.KnowledgeBaseRepository
|
|
knowledgeService interfaces.KnowledgeService // For shared KB document resolution
|
|
knowledgeRepo interfaces.KnowledgeRepository
|
|
}
|
|
|
|
// NewPluginExtractEntity creates a new extract-entity plugin instance
|
|
// Also registers the plugin with the event manager
|
|
func NewPluginExtractEntity(
|
|
eventManager *EventManager,
|
|
modelService interfaces.ModelService,
|
|
knowledgeBaseRepo interfaces.KnowledgeBaseRepository,
|
|
knowledgeService interfaces.KnowledgeService,
|
|
knowledgeRepo interfaces.KnowledgeRepository,
|
|
config *config.Config,
|
|
) *PluginExtractEntity {
|
|
res := &PluginExtractEntity{
|
|
modelService: modelService,
|
|
template: config.ExtractManager.ExtractEntity,
|
|
knowledgeBaseRepo: knowledgeBaseRepo,
|
|
knowledgeService: knowledgeService,
|
|
knowledgeRepo: knowledgeRepo,
|
|
}
|
|
eventManager.Register(res)
|
|
return res
|
|
}
|
|
|
|
// ActivationEvents returns the list of event types this plugin responds to.
|
|
func (p *PluginExtractEntity) ActivationEvents() []types.EventType {
|
|
return []types.EventType{types.QUERY_UNDERSTAND}
|
|
}
|
|
|
|
// OnEvent processes triggered events
|
|
// When receiving a QUERY_UNDERSTAND event, it extracts entities from the query
|
|
func (p *PluginExtractEntity) OnEvent(ctx context.Context,
|
|
eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError,
|
|
) *PluginError {
|
|
if strings.ToLower(os.Getenv("NEO4J_ENABLE")) != "true" {
|
|
logger.Debugf(ctx, "skipping extract entity, neo4j is disabled")
|
|
return next()
|
|
}
|
|
|
|
query := chatManage.Query
|
|
|
|
model, err := p.modelService.GetChatModel(ctx, chatManage.ChatModelID)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "Failed to get model, session_id: %s, error: %v", chatManage.SessionID, err)
|
|
return next()
|
|
}
|
|
|
|
// Collect all knowledge base IDs to query
|
|
kbIDSet := make(map[string]struct{})
|
|
for _, id := range chatManage.KnowledgeBaseIDs {
|
|
kbIDSet[id] = struct{}{}
|
|
}
|
|
|
|
// If KnowledgeIDs is specified, retrieve them and collect their knowledge base IDs (include shared KB docs)
|
|
// Also build a mapping from KnowledgeID to KnowledgeBaseID
|
|
knowledgeToKBMap := make(map[string]string)
|
|
if len(chatManage.KnowledgeIDs) > 0 {
|
|
knowledges, err := p.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, chatManage.TenantID, chatManage.KnowledgeIDs)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "failed to get knowledges: %v", err)
|
|
return next()
|
|
}
|
|
for _, k := range knowledges {
|
|
kbIDSet[k.KnowledgeBaseID] = struct{}{}
|
|
knowledgeToKBMap[k.ID] = k.KnowledgeBaseID
|
|
}
|
|
}
|
|
|
|
// Convert set to slice
|
|
allKBIDs := make([]string, 0, len(kbIDSet))
|
|
for id := range kbIDSet {
|
|
allKBIDs = append(allKBIDs, id)
|
|
}
|
|
|
|
// Batch retrieve all knowledge bases
|
|
kbs, err := p.knowledgeBaseRepo.GetKnowledgeBaseByIDs(ctx, allKBIDs)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "failed to get knowledge bases: %v", err)
|
|
return next()
|
|
}
|
|
|
|
// Check if any knowledge base has ExtractConfig enabled and collect their IDs
|
|
enabledKBSet := make(map[string]struct{})
|
|
for _, kb := range kbs {
|
|
if kb.ExtractConfig != nil && kb.ExtractConfig.Enabled {
|
|
enabledKBSet[kb.ID] = struct{}{}
|
|
}
|
|
}
|
|
if len(enabledKBSet) == 0 {
|
|
logger.Debugf(ctx, "no knowledge base has extract config enabled")
|
|
return next()
|
|
}
|
|
|
|
// Save enabled knowledge base IDs for later use in search_entity
|
|
enabledKBIDs := make([]string, 0, len(enabledKBSet))
|
|
for id := range enabledKBSet {
|
|
enabledKBIDs = append(enabledKBIDs, id)
|
|
}
|
|
chatManage.EntityKBIDs = enabledKBIDs
|
|
|
|
// Filter knowledgeToKBMap to only include files from enabled knowledge bases
|
|
entityKnowledge := make(map[string]string)
|
|
for knowledgeID, kbID := range knowledgeToKBMap {
|
|
if _, ok := enabledKBSet[kbID]; ok {
|
|
entityKnowledge[knowledgeID] = kbID
|
|
}
|
|
}
|
|
chatManage.EntityKnowledge = entityKnowledge
|
|
|
|
template := &types.PromptTemplateStructured{
|
|
Description: p.template.Description,
|
|
Examples: p.template.Examples,
|
|
}
|
|
extractor := NewExtractor(model, template)
|
|
graph, err := extractor.Extract(ctx, query)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "Failed to extract entities, session_id: %s, error: %v", chatManage.SessionID, err)
|
|
return next()
|
|
}
|
|
nodes := []string{}
|
|
for _, node := range graph.Node {
|
|
nodes = append(nodes, node.Name)
|
|
}
|
|
logger.Debugf(ctx, "extracted node: %v", nodes)
|
|
chatManage.Entity = nodes
|
|
return next()
|
|
}
|
|
|
|
// Extractor is a struct for extracting entities
|
|
type Extractor struct {
|
|
chat chat.Chat
|
|
formater *Formater
|
|
template *types.PromptTemplateStructured
|
|
chatOpt *chat.ChatOptions
|
|
}
|
|
|
|
// NewExtractor creates a new extractor
|
|
func NewExtractor(
|
|
chatModel chat.Chat,
|
|
template *types.PromptTemplateStructured,
|
|
) Extractor {
|
|
think := false
|
|
return Extractor{
|
|
chat: chatModel,
|
|
formater: NewFormater(),
|
|
template: template,
|
|
chatOpt: &chat.ChatOptions{
|
|
Temperature: 0.3,
|
|
MaxTokens: 4096,
|
|
Thinking: &think,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Extract extracts entities from content
|
|
func (e *Extractor) Extract(ctx context.Context, content string) (*types.GraphData, error) {
|
|
generator := NewQAPromptGenerator(e.formater, e.template)
|
|
|
|
// logger.Debugf(ctx, "chat system: %s", generator.System(ctx))
|
|
// logger.Debugf(ctx, "chat user: %s", generator.User(ctx, content))
|
|
|
|
chatResponse, err := e.chat.Chat(ctx, generator.Render(ctx, content), e.chatOpt)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "failed to chat: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
graph, err := e.formater.ParseGraph(ctx, chatResponse.Content)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "failed to parse graph: %v", err)
|
|
return nil, err
|
|
}
|
|
// e.RemoveUnknownRelation(ctx, graph)
|
|
return graph, nil
|
|
}
|
|
|
|
// RemoveUnknownRelation removes unknown relations from graph
|
|
func (e *Extractor) RemoveUnknownRelation(ctx context.Context, graph *types.GraphData) {
|
|
relationType := make(map[string]bool)
|
|
for _, tag := range e.template.Tags {
|
|
relationType[tag] = true
|
|
}
|
|
|
|
relationNew := make([]*types.GraphRelation, 0)
|
|
for _, relation := range graph.Relation {
|
|
if _, ok := relationType[relation.Type]; ok {
|
|
relationNew = append(relationNew, relation)
|
|
} else {
|
|
logger.Infof(ctx, "Unknown relation type %s with %v, ignore it", relation.Type, e.template.Tags)
|
|
}
|
|
}
|
|
graph.Relation = relationNew
|
|
}
|
|
|
|
// QAPromptGenerator is a struct for generating QA prompts
|
|
type QAPromptGenerator struct {
|
|
Formater *Formater
|
|
Template *types.PromptTemplateStructured
|
|
ExamplesHeading string
|
|
QuestionHeading string
|
|
QuestionPrefix string
|
|
AnswerPrefix string
|
|
}
|
|
|
|
// NewQAPromptGenerator creates a new QA prompt generator
|
|
func NewQAPromptGenerator(formater *Formater, template *types.PromptTemplateStructured) *QAPromptGenerator {
|
|
return &QAPromptGenerator{
|
|
Formater: formater,
|
|
Template: template,
|
|
ExamplesHeading: "# Examples",
|
|
QuestionHeading: "# Question",
|
|
QuestionPrefix: "Q: ",
|
|
AnswerPrefix: "A: ",
|
|
}
|
|
}
|
|
|
|
// System generates a system prompt
|
|
func (qa *QAPromptGenerator) System(ctx context.Context) string {
|
|
promptLines := []string{}
|
|
|
|
if len(qa.Template.Tags) == 0 {
|
|
promptLines = append(promptLines, qa.Template.Description)
|
|
} else {
|
|
tags, _ := json.Marshal(qa.Template.Tags)
|
|
promptLines = append(promptLines, fmt.Sprintf(qa.Template.Description, string(tags)))
|
|
}
|
|
if len(qa.Template.Examples) > 0 {
|
|
promptLines = append(promptLines, qa.ExamplesHeading)
|
|
for _, example := range qa.Template.Examples {
|
|
// Question
|
|
promptLines = append(promptLines, fmt.Sprintf("%s%s", qa.QuestionPrefix, strings.TrimSpace(example.Text)))
|
|
|
|
// Answer
|
|
answer, err := qa.Formater.formatExtraction(example.Node, example.Relation)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
promptLines = append(promptLines, fmt.Sprintf("%s%s", qa.AnswerPrefix, answer))
|
|
|
|
// new line
|
|
promptLines = append(promptLines, "")
|
|
}
|
|
}
|
|
return strings.Join(promptLines, "\n")
|
|
}
|
|
|
|
// User generates a user prompt
|
|
func (qa *QAPromptGenerator) User(ctx context.Context, question string) string {
|
|
promptLines := []string{}
|
|
promptLines = append(promptLines, qa.QuestionHeading)
|
|
promptLines = append(promptLines, fmt.Sprintf("%s%s", qa.QuestionPrefix, question))
|
|
promptLines = append(promptLines, qa.AnswerPrefix)
|
|
return strings.Join(promptLines, "\n")
|
|
}
|
|
|
|
// Render renders a prompt
|
|
func (qa *QAPromptGenerator) Render(ctx context.Context, question string) []chat.Message {
|
|
return []chat.Message{
|
|
{
|
|
Role: "system",
|
|
Content: qa.System(ctx),
|
|
},
|
|
{
|
|
Role: "user",
|
|
Content: qa.User(ctx, question),
|
|
},
|
|
}
|
|
}
|
|
|
|
// FormatType is a type for format types
|
|
type FormatType string
|
|
|
|
const (
|
|
// FormatTypeJSON is a format type for JSON
|
|
FormatTypeJSON FormatType = "json"
|
|
// FormatTypeYAML is a format type for YAML
|
|
FormatTypeYAML FormatType = "yaml"
|
|
)
|
|
|
|
const (
|
|
_FENCE_START = "```"
|
|
_LANGUAGE_TAG = `(?P<lang>[A-Za-z0-9_+-]+)?`
|
|
_FENCE_NEWLINE = `(?:\s*\n)?`
|
|
_FENCE_BODY = `(?P<body>[\s\S]*?)`
|
|
_FENCE_END = "```"
|
|
)
|
|
|
|
var _FENCE_RE = regexp.MustCompile(
|
|
_FENCE_START + _LANGUAGE_TAG + _FENCE_NEWLINE + _FENCE_BODY + _FENCE_END,
|
|
)
|
|
|
|
// Formater is a struct for formatting entities
|
|
type Formater struct {
|
|
attributeSuffix string
|
|
formatType FormatType
|
|
useFences bool
|
|
nodePrefix string
|
|
|
|
relationSource string
|
|
relationTarget string
|
|
relationPrefix string
|
|
}
|
|
|
|
// NewFormater creates a new formater
|
|
func NewFormater() *Formater {
|
|
return &Formater{
|
|
attributeSuffix: "_attributes",
|
|
formatType: FormatTypeJSON,
|
|
useFences: true,
|
|
nodePrefix: "entity",
|
|
relationSource: "entity1",
|
|
relationTarget: "entity2",
|
|
relationPrefix: "relation",
|
|
}
|
|
}
|
|
|
|
// formatExtraction formats extraction
|
|
func (f *Formater) formatExtraction(nodes []*types.GraphNode, relations []*types.GraphRelation) (string, error) {
|
|
items := make([]map[string]interface{}, 0)
|
|
for _, node := range nodes {
|
|
item := map[string]interface{}{
|
|
f.nodePrefix: node.Name,
|
|
}
|
|
if len(node.Attributes) > 0 {
|
|
item[fmt.Sprintf("%s%s", f.nodePrefix, f.attributeSuffix)] = node.Attributes
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
for _, relation := range relations {
|
|
item := map[string]interface{}{
|
|
f.relationSource: relation.Node1,
|
|
f.relationTarget: relation.Node2,
|
|
f.relationPrefix: relation.Type,
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
formatted := ""
|
|
switch f.formatType {
|
|
default:
|
|
formattedBytes, err := json.MarshalIndent(items, "", " ")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
formatted = string(formattedBytes)
|
|
}
|
|
if f.useFences {
|
|
formatted = f.addFences(formatted)
|
|
}
|
|
return formatted, nil
|
|
}
|
|
|
|
func (f *Formater) parseOutput(ctx context.Context, text string) ([]map[string]interface{}, error) {
|
|
if text == "" {
|
|
return nil, errors.New("empty or invalid input string")
|
|
}
|
|
content := f.extractContent(ctx, text)
|
|
// logger.Debugf(ctx, "Extracted content: %s", content)
|
|
if content == "" {
|
|
return nil, errors.New("empty or invalid input string")
|
|
}
|
|
|
|
var parsed interface{}
|
|
var err error
|
|
if f.formatType == FormatTypeJSON {
|
|
err = json.Unmarshal([]byte(content), &parsed)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse %s content: %s", strings.ToUpper(string(f.formatType)), err.Error())
|
|
}
|
|
if parsed == nil {
|
|
return nil, fmt.Errorf("content must be a list of extractions or a dict")
|
|
}
|
|
|
|
var items []interface{}
|
|
if parsedMap, ok := parsed.(map[string]interface{}); ok {
|
|
items = []interface{}{parsedMap}
|
|
} else if parsedList, ok := parsed.([]interface{}); ok {
|
|
items = parsedList
|
|
} else {
|
|
return nil, fmt.Errorf("expected list or dict, got %T", parsed)
|
|
}
|
|
|
|
itemsList := make([]map[string]interface{}, 0)
|
|
for _, item := range items {
|
|
if itemMap, ok := item.(map[string]interface{}); ok {
|
|
itemsList = append(itemsList, itemMap)
|
|
} else {
|
|
return nil, fmt.Errorf("each item in the sequence must be a mapping.")
|
|
}
|
|
}
|
|
return itemsList, nil
|
|
}
|
|
|
|
func (f *Formater) ParseGraph(ctx context.Context, text string) (*types.GraphData, error) {
|
|
matchData, err := f.parseOutput(ctx, text)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(matchData) == 0 {
|
|
logger.Debugf(ctx, "received empty extraction data.")
|
|
return &types.GraphData{}, nil
|
|
}
|
|
// mm, _ := json.Marshal(matchData)
|
|
// logger.Debugf(ctx, "Parsed graph data: %s", string(mm))
|
|
|
|
var nodes []*types.GraphNode
|
|
var relations []*types.GraphRelation
|
|
|
|
for _, group := range matchData {
|
|
switch {
|
|
case group[f.nodePrefix] != nil:
|
|
attributes := make([]string, 0)
|
|
attributesKey := f.nodePrefix + f.attributeSuffix
|
|
if attr, ok := group[attributesKey].([]interface{}); ok {
|
|
for _, v := range attr {
|
|
attributes = append(attributes, fmt.Sprintf("%v", v))
|
|
}
|
|
}
|
|
nodes = append(nodes, &types.GraphNode{
|
|
Name: fmt.Sprintf("%v", group[f.nodePrefix]),
|
|
Attributes: attributes,
|
|
})
|
|
case group[f.relationSource] != nil && group[f.relationTarget] != nil:
|
|
relations = append(relations, &types.GraphRelation{
|
|
Node1: fmt.Sprintf("%v", group[f.relationSource]),
|
|
Node2: fmt.Sprintf("%v", group[f.relationTarget]),
|
|
Type: fmt.Sprintf("%v", group[f.relationPrefix]),
|
|
})
|
|
default:
|
|
logger.Warnf(ctx, "Unsupported graph group: %v", group)
|
|
continue
|
|
}
|
|
}
|
|
graph := &types.GraphData{
|
|
Node: nodes,
|
|
Relation: relations,
|
|
}
|
|
f.rebuildGraph(ctx, graph)
|
|
return graph, nil
|
|
}
|
|
|
|
func (f *Formater) rebuildGraph(ctx context.Context, graph *types.GraphData) {
|
|
nodeMap := make(map[string]*types.GraphNode)
|
|
nodes := make([]*types.GraphNode, 0, len(graph.Node))
|
|
for _, node := range graph.Node {
|
|
if prenode, ok := nodeMap[node.Name]; ok {
|
|
logger.Infof(ctx, "Duplicate node ID: %s, merge attribute", node.Name)
|
|
// 修复panic:检查Attributes是否为nil
|
|
if node.Attributes == nil {
|
|
node.Attributes = make([]string, 0)
|
|
}
|
|
if prenode.Attributes != nil {
|
|
node.Attributes = append(node.Attributes, prenode.Attributes...)
|
|
}
|
|
continue
|
|
}
|
|
nodeMap[node.Name] = node
|
|
nodes = append(nodes, node)
|
|
}
|
|
|
|
relations := make([]*types.GraphRelation, 0, len(graph.Relation))
|
|
for _, relation := range graph.Relation {
|
|
if relation.Node1 == relation.Node2 {
|
|
logger.Infof(ctx, "Duplicate relation, ignore it")
|
|
continue
|
|
}
|
|
|
|
if _, ok := nodeMap[relation.Node1]; !ok {
|
|
node := &types.GraphNode{Name: relation.Node1}
|
|
nodes = append(nodes, node)
|
|
nodeMap[relation.Node1] = node
|
|
logger.Infof(ctx, "Add unknown source node ID: %s", relation.Node1)
|
|
}
|
|
if _, ok := nodeMap[relation.Node2]; !ok {
|
|
node := &types.GraphNode{Name: relation.Node2}
|
|
nodes = append(nodes, node)
|
|
nodeMap[relation.Node2] = node
|
|
logger.Infof(ctx, "Add unknown target node ID: %s", relation.Node2)
|
|
}
|
|
|
|
relations = append(relations, relation)
|
|
}
|
|
*graph = types.GraphData{
|
|
Node: nodes,
|
|
Relation: relations,
|
|
}
|
|
}
|
|
|
|
func (f *Formater) extractContent(ctx context.Context, text string) string {
|
|
if !f.useFences {
|
|
return strings.TrimSpace(text)
|
|
}
|
|
validTags := map[FormatType]map[string]struct{}{
|
|
FormatTypeYAML: {"yaml": {}, "yml": {}},
|
|
FormatTypeJSON: {"json": {}},
|
|
}
|
|
matches := _FENCE_RE.FindAllStringSubmatch(text, -1)
|
|
var candidates []string
|
|
for _, match := range matches {
|
|
lang := match[1]
|
|
body := match[2]
|
|
if f.isValidLanguageTag(lang, validTags) {
|
|
candidates = append(candidates, body)
|
|
}
|
|
}
|
|
switch {
|
|
case len(candidates) == 1:
|
|
return strings.TrimSpace(candidates[0])
|
|
|
|
case len(candidates) > 1:
|
|
logger.Warnf(ctx, "multiple candidates found: %d", len(candidates))
|
|
return strings.TrimSpace(candidates[0])
|
|
|
|
case len(matches) == 1:
|
|
logger.Debugf(ctx, "no candidate found, use first match without language tag: %s", matches[0][1])
|
|
return strings.TrimSpace(matches[0][2])
|
|
|
|
case len(matches) > 1:
|
|
logger.Warnf(ctx, "multiple matches found: %d", len(matches))
|
|
return strings.TrimSpace(matches[0][2])
|
|
|
|
default:
|
|
// Fallback strategies for cases where the fence regex fails to match.
|
|
// This commonly happens when:
|
|
// 1. The LLM output is truncated (no closing fence) — issue #1113 Pattern 3.
|
|
// 2. The opening fence is malformed or surrounded by unexpected content,
|
|
// so the non-greedy regex falls back to the raw text — issue #1113 Pattern 1.
|
|
// Without these fallbacks, the raw text (including backticks) is passed to
|
|
// json.Unmarshal and fails with `invalid character '`'`.
|
|
if extracted := stripFencesAndExtract(text, f.formatType); extracted != "" {
|
|
logger.Debugf(ctx, "no fence match, recovered content via fallback (%d bytes)", len(extracted))
|
|
return extracted
|
|
}
|
|
logger.Warnf(ctx, "no match found")
|
|
return strings.TrimSpace(text)
|
|
}
|
|
}
|
|
|
|
// stripFencesAndExtract attempts to recover a parseable payload from an LLM
|
|
// response when the strict fence regex fails. It handles three common cases:
|
|
//
|
|
// 1. Truncated responses with an opening ```lang fence but no closing fence
|
|
// (LLM hit max_tokens mid-output).
|
|
// 2. Responses where the JSON/YAML body is preceded or followed by prose
|
|
// and the fences are present but malformed.
|
|
// 3. Responses with no fences at all but a recognizable JSON object/array
|
|
// embedded in surrounding text.
|
|
//
|
|
// It returns an empty string when no plausible payload can be recovered, so
|
|
// callers can fall back to their own behavior.
|
|
func stripFencesAndExtract(text string, format FormatType) string {
|
|
trimmed := strings.TrimSpace(text)
|
|
if trimmed == "" {
|
|
return ""
|
|
}
|
|
|
|
// Case 1: opening fence present (with or without language tag) but no
|
|
// matching closing fence. Take everything after the first fence and
|
|
// strip any trailing backticks.
|
|
if idx := strings.Index(trimmed, "```"); idx >= 0 {
|
|
rest := trimmed[idx+3:]
|
|
// Drop optional language tag on the same line.
|
|
if nl := strings.IndexByte(rest, '\n'); nl >= 0 {
|
|
firstLine := strings.TrimSpace(rest[:nl])
|
|
// A pure language tag is short and alphanumeric-ish.
|
|
if firstLine == "" || isLikelyLanguageTag(firstLine) {
|
|
rest = rest[nl+1:]
|
|
}
|
|
}
|
|
// If there is a closing fence somewhere, cut at it.
|
|
if end := strings.Index(rest, "```"); end >= 0 {
|
|
rest = rest[:end]
|
|
}
|
|
rest = strings.TrimSpace(rest)
|
|
rest = strings.Trim(rest, "`")
|
|
rest = strings.TrimSpace(rest)
|
|
if rest != "" {
|
|
return rest
|
|
}
|
|
}
|
|
|
|
// Case 2: no usable fence found, but the payload may still contain a
|
|
// JSON object/array. Extract the outermost {...} or [...] substring.
|
|
if format == FormatTypeJSON {
|
|
if extracted := extractJSONLike(trimmed); extracted != "" {
|
|
return extracted
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// isLikelyLanguageTag reports whether s looks like a markdown fence language
|
|
// tag (e.g. "json", "yaml", "yml", "go"). It must be short and contain only
|
|
// characters typical for a language identifier.
|
|
func isLikelyLanguageTag(s string) bool {
|
|
if s == "" || len(s) > 16 {
|
|
return false
|
|
}
|
|
for _, r := range s {
|
|
switch {
|
|
case r >= 'a' && r <= 'z':
|
|
case r >= 'A' && r <= 'Z':
|
|
case r >= '0' && r <= '9':
|
|
case r == '_' || r == '-' || r == '+':
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// extractJSONLike returns the outermost JSON object or array substring from s,
|
|
// or an empty string if none is found. It picks whichever bracket type appears
|
|
// first in the input, which mirrors what the LLM is most likely to have
|
|
// produced. The returned slice is not validated as JSON; callers must still
|
|
// json.Unmarshal it.
|
|
func extractJSONLike(s string) string {
|
|
objStart := strings.IndexByte(s, '{')
|
|
arrStart := strings.IndexByte(s, '[')
|
|
var open, closeCh byte
|
|
var start int
|
|
switch {
|
|
case objStart < 0 && arrStart < 0:
|
|
return ""
|
|
case objStart < 0:
|
|
open, closeCh, start = '[', ']', arrStart
|
|
case arrStart < 0:
|
|
open, closeCh, start = '{', '}', objStart
|
|
case objStart < arrStart:
|
|
open, closeCh, start = '{', '}', objStart
|
|
default:
|
|
open, closeCh, start = '[', ']', arrStart
|
|
}
|
|
// Find matching close, respecting string literals so braces/brackets
|
|
// inside JSON strings don't unbalance the count.
|
|
depth := 0
|
|
inString := false
|
|
escaped := false
|
|
for i := start; i < len(s); i++ {
|
|
c := s[i]
|
|
if inString {
|
|
if escaped {
|
|
escaped = false
|
|
continue
|
|
}
|
|
switch c {
|
|
case '\\':
|
|
escaped = true
|
|
case '"':
|
|
inString = false
|
|
}
|
|
continue
|
|
}
|
|
switch c {
|
|
case '"':
|
|
inString = true
|
|
case open:
|
|
depth++
|
|
case closeCh:
|
|
depth--
|
|
if depth == 0 {
|
|
return strings.TrimSpace(s[start : i+1])
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (f *Formater) addFences(content string) string {
|
|
content = strings.TrimSpace(content)
|
|
return fmt.Sprintf("```%s\n%s\n```", f.formatType, content)
|
|
}
|
|
|
|
func (f *Formater) isValidLanguageTag(lang string, validTags map[FormatType]map[string]struct{}) bool {
|
|
if lang == "" {
|
|
return true
|
|
}
|
|
tag := strings.TrimSpace(strings.ToLower(lang))
|
|
validSet, ok := validTags[f.formatType]
|
|
if !ok {
|
|
return false
|
|
}
|
|
_, exists := validSet[tag]
|
|
return exists
|
|
}
|