-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjson.go
227 lines (196 loc) · 4.99 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package deepseek
import (
"encoding/json"
"fmt"
"strings"
)
// JSONExtractor helps extract structured data from LLM responses
type JSONExtractor struct {
// Optional JSON schema for validation
schema json.RawMessage
}
// NewJSONExtractor creates a new JSONExtractor instance
func NewJSONExtractor(schema json.RawMessage) *JSONExtractor {
return &JSONExtractor{
schema: schema,
}
}
// ExtractJSON attempts to extract and parse JSON from an LLM response
func (je *JSONExtractor) ExtractJSON(response *ChatCompletionResponse, target interface{}) error {
if response == nil {
return fmt.Errorf("response cannot be nil")
}
if len(response.Choices) == 0 {
return fmt.Errorf("no choices in response")
}
content := response.Choices[0].Message.Content
if content == "" {
return fmt.Errorf("empty content in response")
}
// Try to find JSON content with or without code blocks
jsonStr := je.extractJSONContent(content)
if jsonStr == "" {
return fmt.Errorf("no valid JSON content found in response")
}
// If schema is provided, validate the JSON against it
if je.schema != nil {
if err := je.validateJSON([]byte(jsonStr)); err != nil {
return fmt.Errorf("JSON validation failed: %w", err)
}
}
// Parse the JSON into the target structure
if err := json.Unmarshal([]byte(jsonStr), target); err != nil {
return fmt.Errorf("failed to parse JSON: %w", err)
}
return nil
}
// validateJSON validates JSON content against the schema
func (je *JSONExtractor) validateJSON(data []byte) error {
var schemaMap map[string]interface{}
if err := json.Unmarshal(je.schema, &schemaMap); err != nil {
return fmt.Errorf("invalid schema: %w", err)
}
var jsonData interface{}
if err := json.Unmarshal(data, &jsonData); err != nil {
return fmt.Errorf("invalid JSON data: %w", err)
}
// Basic type validation
if schemaType, ok := schemaMap["type"].(string); ok {
switch schemaType {
case "object":
if _, ok := jsonData.(map[string]interface{}); !ok {
return fmt.Errorf("expected object, got %T", jsonData)
}
case "array":
if _, ok := jsonData.([]interface{}); !ok {
return fmt.Errorf("expected array, got %T", jsonData)
}
}
}
return nil
}
// extractJSONContent attempts to extract valid JSON from the content
func (je *JSONExtractor) extractJSONContent(content string) string {
content = strings.TrimSpace(content)
// First try to parse the entire content as JSON
if json.Valid([]byte(content)) {
return content
}
// Try to find JSON content in various formats
patterns := []struct {
extract func(string) string
}{
// Try code blocks first
{func(s string) string {
return je.extractBetween(s, "```json\n", "```")
}},
{func(s string) string {
return je.extractBetween(s, "```json", "```")
}},
{func(s string) string {
return je.extractBetween(s, "```\n", "```")
}},
{func(s string) string {
return je.extractBetween(s, "```", "```")
}},
// Then try to find JSON objects or arrays
{func(s string) string {
return je.findJSONInText(s)
}},
}
for _, pattern := range patterns {
if result := pattern.extract(content); result != "" {
return result
}
}
return ""
}
// extractBetween extracts content between start and end markers
func (je *JSONExtractor) extractBetween(content, start, end string) string {
startIdx := strings.Index(content, start)
if startIdx == -1 {
return ""
}
content = content[startIdx+len(start):]
endIdx := strings.Index(content, end)
if endIdx == -1 {
return ""
}
result := strings.TrimSpace(content[:endIdx])
if json.Valid([]byte(result)) {
return result
}
return ""
}
// findJSONInText attempts to find valid JSON objects or arrays in text
func (je *JSONExtractor) findJSONInText(content string) string {
// Find potential JSON start
var start, end int
var found bool
// Try to find JSON object
start = strings.Index(content, "{")
if start != -1 {
end = je.findMatchingBrace(content[start:])
if end != -1 {
found = true
end += start
}
}
// If no object found, try to find JSON array
if !found {
start = strings.Index(content, "[")
if start != -1 {
end = je.findMatchingBracket(content[start:])
if end != -1 {
found = true
end += start
}
}
}
if !found {
return ""
}
result := content[start : end+1]
if json.Valid([]byte(result)) {
return result
}
return ""
}
// findMatchingBrace finds the matching closing brace for a JSON object
func (je *JSONExtractor) findMatchingBrace(content string) int {
if !strings.HasPrefix(content, "{") {
return -1
}
depth := 0
for i, char := range content {
switch char {
case '{':
depth++
case '}':
depth--
if depth == 0 {
return i
}
}
}
return -1
}
// findMatchingBracket finds the matching closing bracket for a JSON array
func (je *JSONExtractor) findMatchingBracket(content string) int {
if !strings.HasPrefix(content, "[") {
return -1
}
depth := 0
for i, char := range content {
switch char {
case '[':
depth++
case ']':
depth--
if depth == 0 {
return i
}
}
}
return -1
}