refactor: migration from OpenAPIV3 to JSONSchema (#108)
parent
05568cd009
commit
a0e96f1391
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2024 CloudWeGo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/eino-contrib/jsonschema"
|
||||
orderedmap "github.com/wk8/go-ordered-map/v2"
|
||||
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
func main() {
|
||||
JSONSchemaToToolInfo()
|
||||
}
|
||||
|
||||
func JSONSchemaToToolInfo() {
|
||||
js := &jsonschema.Schema{
|
||||
Type: string(schema.Object),
|
||||
Required: []string{"title"},
|
||||
Properties: orderedmap.New[string, *jsonschema.Schema](
|
||||
orderedmap.WithInitialData[string, *jsonschema.Schema](
|
||||
orderedmap.Pair[string, *jsonschema.Schema]{
|
||||
Key: "title",
|
||||
Value: &jsonschema.Schema{
|
||||
Type: string(schema.String),
|
||||
},
|
||||
},
|
||||
orderedmap.Pair[string, *jsonschema.Schema]{
|
||||
Key: "completed",
|
||||
Value: &jsonschema.Schema{
|
||||
Type: string(schema.Boolean),
|
||||
},
|
||||
},
|
||||
),
|
||||
),
|
||||
}
|
||||
|
||||
toolInfo := schema.ToolInfo{
|
||||
Name: "todo_manager",
|
||||
Desc: "manage todo list",
|
||||
ParamsOneOf: schema.NewParamsOneOfByJSONSchema(js),
|
||||
}
|
||||
|
||||
fmt.Printf("\n=========tool from api path=========\n")
|
||||
spew.Dump(toolInfo)
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
## 说明
|
||||
|
||||
eino 提供了把 openapi3 文档中的 json schema 转为 eino tool schema 的方法。
|
||||
eino 提供了把 json schema 转为 eino tool schema 的方法。
|
||||
对于想直接把一个 http 接口作为 eino 中的一个 tool 使用的场景,提供了便捷的方法。
|
||||
|
||||
关于 tool 更多的信息,可以参考: https://www.cloudwego.io/zh/docs/eino/core_modules/components/tools_node_guide/
|
||||
@ -1,107 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 CloudWeGo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
func main() {
|
||||
OpenapiDocToToolInfo()
|
||||
}
|
||||
|
||||
func OpenapiDocToToolInfo() {
|
||||
loader := openapi3.NewLoader()
|
||||
|
||||
// 解析 JSON 文档为 json Schema
|
||||
doc, err := loader.LoadFromFile("./openapi.json")
|
||||
if err != nil {
|
||||
log.Fatalf("解析 openapi.json 失败: %v", err)
|
||||
}
|
||||
|
||||
// **** 如果是对 path 的 body 引用 ****
|
||||
// eg: POST /api/v1/todo
|
||||
schemaVal01, err := GetSchemaFromPath(doc, "POST", "/api/v1/todo")
|
||||
if err != nil {
|
||||
log.Fatalf("获取 ref 失败: %v", err)
|
||||
}
|
||||
|
||||
toolInfo := schema.ToolInfo{
|
||||
Name: "todo_manager",
|
||||
Desc: "manage todo list",
|
||||
ParamsOneOf: schema.NewParamsOneOfByOpenAPIV3(schemaVal01),
|
||||
}
|
||||
|
||||
fmt.Printf("\n=========tool from api path=========\n")
|
||||
spew.Dump(toolInfo)
|
||||
|
||||
// **** 如果有 ref 引用 ****
|
||||
// eg: refName "#/components/schemas/TodoRequest" => ref := "TodoRequest"
|
||||
schemaVal02, err := GetSchemaFromRef(doc, "TodoRequest")
|
||||
if err != nil {
|
||||
log.Fatalf("获取 ref 失败: %v", err)
|
||||
}
|
||||
testToolInfo := schema.ToolInfo{
|
||||
Name: "test",
|
||||
Desc: "test desc",
|
||||
ParamsOneOf: schema.NewParamsOneOfByOpenAPIV3(schemaVal02),
|
||||
}
|
||||
|
||||
fmt.Printf("\n\n=========tool from schema ref=========\n")
|
||||
spew.Dump(testToolInfo)
|
||||
}
|
||||
|
||||
// 获取引用的 schema
|
||||
func GetSchemaFromRef(doc *openapi3.T, ref string) (*openapi3.Schema, error) {
|
||||
schemaRef, ok := doc.Components.Schemas[ref]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("未找到引用: %s", ref)
|
||||
}
|
||||
|
||||
return schemaRef.Value, nil
|
||||
}
|
||||
|
||||
// 获取 path 的 schema
|
||||
func GetSchemaFromPath(doc *openapi3.T, method string, path string) (*openapi3.Schema, error) {
|
||||
pattItem := doc.Paths.Find(path)
|
||||
if pattItem == nil {
|
||||
return nil, fmt.Errorf("未找到 path: %s", path)
|
||||
}
|
||||
|
||||
methodItem := pattItem.GetOperation(method)
|
||||
if methodItem == nil {
|
||||
return nil, fmt.Errorf("未找到 method: %s", method)
|
||||
}
|
||||
|
||||
reqBody := methodItem.RequestBody
|
||||
if reqBody == nil || reqBody.Value == nil || reqBody.Value.Content == nil {
|
||||
return nil, fmt.Errorf("未找到 requestBody: %s %s", method, path)
|
||||
}
|
||||
|
||||
jschema := reqBody.Value.Content["application/json"]
|
||||
if jschema == nil || jschema.Schema == nil {
|
||||
return nil, fmt.Errorf("未找到 schema: %s %s", method, path)
|
||||
}
|
||||
|
||||
return jschema.Schema.Value, nil
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
{
|
||||
"openapi": "3.0.0",
|
||||
"info": {
|
||||
"title": "Todo API",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/api/v1/todo": {
|
||||
"post": {
|
||||
"summary": "Create a todo item",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/TodoRequest"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Todo item created"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"TodoRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"type": "string"
|
||||
},
|
||||
"completed": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": ["title"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue