mirror of
https://github.com/alibaba/higress.git
synced 2026-05-28 14:47:29 +08:00
[ai-json-resp] Extract JSON from LLM, Validate with Schema, Ensure Valid JSON, Auto-Retry (#1236)
This commit is contained in:
33
plugins/wasm-go/extensions/ai-json-resp/util.go
Normal file
33
plugins/wasm-go/extensions/ai-json-resp/util.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
func GetMaxDepth(data interface{}) int {
|
||||
type item struct {
|
||||
value interface{}
|
||||
depth int
|
||||
}
|
||||
|
||||
maxDepth := 0
|
||||
stack := []item{{value: data, depth: 1}}
|
||||
|
||||
for len(stack) > 0 {
|
||||
currentItem := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
if currentItem.depth > maxDepth {
|
||||
maxDepth = currentItem.depth
|
||||
}
|
||||
|
||||
switch v := currentItem.value.(type) {
|
||||
case map[string]interface{}:
|
||||
for _, value := range v {
|
||||
stack = append(stack, item{value: value, depth: currentItem.depth + 1})
|
||||
}
|
||||
case []interface{}:
|
||||
for _, value := range v {
|
||||
stack = append(stack, item{value: value, depth: currentItem.depth + 1})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxDepth
|
||||
}
|
||||
Reference in New Issue
Block a user