in plugins/wasm-go/extensions/ai-json-resp/main.go [144:258]
func parseConfig(result gjson.Result, config *PluginConfig, log wrapper.Log) error {
config.serviceName = result.Get("serviceName").String()
config.serviceUrl = result.Get("serviceUrl").String()
config.serviceDomain = result.Get("serviceDomain").String()
config.servicePath = result.Get("servicePath").String()
config.servicePort = int(result.Get("servicePort").Int())
if config.serviceUrl != "" {
domain, url := parseUrl(config.serviceUrl)
log.Debugf("serviceUrl: %s, the parsed domain: %s, the parsed url: %s", config.serviceUrl, domain, url)
if config.serviceDomain == "" {
config.serviceDomain = domain
}
if config.servicePath == "" {
config.servicePath = url
}
}
if config.servicePort == 0 {
config.servicePort = 443
}
config.serviceTimeout = int(result.Get("serviceTimeout").Int())
config.apiKey = result.Get("apiKey").String()
config.rejectStruct = RejectStruct{HTTP_STATUS_OK, ""}
if config.serviceTimeout == 0 {
config.serviceTimeout = 50000
}
config.maxRetry = int(result.Get("maxRetry").Int())
if config.maxRetry == 0 {
config.maxRetry = 3
}
config.contentPath = result.Get("contentPath").String()
if config.contentPath == "" {
config.contentPath = "choices.0.message.content"
}
if jsonSchemaValue := result.Get("jsonSchema"); jsonSchemaValue.Exists() {
if schemaValue, ok := jsonSchemaValue.Value().(map[string]interface{}); ok {
config.jsonSchema = schemaValue
} else {
config.rejectStruct = RejectStruct{JSON_SCHEMA_INVALID_CODE, "Json Schema is not valid"}
}
} else {
config.jsonSchema = nil
}
if config.serviceDomain == "" {
config.rejectStruct = RejectStruct{JSON_SCHEMA_INVALID_CODE, "service domain is empty"}
}
config.serviceClient = wrapper.NewClusterClient(wrapper.DnsCluster{
ServiceName: config.serviceName,
Port: int64(config.servicePort),
Domain: config.serviceDomain,
})
enableSwagger := result.Get("enableSwagger").Bool()
enableOas3 := result.Get("enableOas3").Bool()
// set draft version
if enableSwagger {
config.draft = jsonschema.Draft4
}
if enableOas3 {
config.draft = jsonschema.Draft7
}
if !enableSwagger && !enableOas3 {
config.draft = jsonschema.Draft7
}
// create compiler
compiler := jsonschema.NewCompiler()
compiler.Draft = config.draft
config.compiler = compiler
// set max depth of json schema
config.jsonSchemaMaxDepth = 6
enableContentDispositionValue := result.Get("enableContentDisposition")
if !enableContentDispositionValue.Exists() {
config.enableContentDisposition = true
} else {
config.enableContentDisposition = enableContentDispositionValue.Bool()
}
config.enableJsonSchemaValidation = true
jsonSchemaBytes, err := json.Marshal(config.jsonSchema)
if err != nil {
config.rejectStruct = RejectStruct{JSON_SCHEMA_INVALID_CODE, "Json Schema marshal failed"}
return err
}
maxDepth := GetMaxDepth(config.jsonSchema)
log.Debugf("max depth of json schema: %d", maxDepth)
if maxDepth > config.jsonSchemaMaxDepth {
config.enableJsonSchemaValidation = false
log.Infof("Json Schema depth exceeded: %d from %d , Json Schema validation will not be used.", maxDepth, config.jsonSchemaMaxDepth)
}
if config.enableJsonSchemaValidation {
jsonSchemaStr := string(jsonSchemaBytes)
config.compiler.AddResource(DEFAULT_SCHEMA, strings.NewReader(jsonSchemaStr))
// Test if the Json Schema is valid
compile, err := config.compiler.Compile(DEFAULT_SCHEMA)
if err != nil {
log.Infof("Json Schema compile failed: %v", err)
config.rejectStruct = RejectStruct{JSON_SCHEMA_COMPILE_FAILED_CODE, "Json Schema compile failed: " + err.Error()}
config.compile = nil
} else {
config.compile = compile
}
}
return nil
}