dify 提供了创建自定义工具,不过这个自定义工具是需要遵循OpenAPI-Swagger规范。
规范的空模版的例子如下:
{ "openapi": "3.1.0", "info": { "title": "Untitled", "description": "Your OpenAPI specification", "version": "v1.0.0" }, "servers": [ { "url": "" } ], "paths": {}, "components": { "schemas": {} } }
生成OpenAPI-Swagger规范
我们使用chatgpt 帮我把调用的 curl 命令转换成openapi schema,提示词如下:
请把curl请求命令转成openapi 3.1.0 版本的json schema,不需要包含response信息 <curl> curl --location 'https://cloud.dify.ai/generate_image/' --header 'Content-Type: application/json' --data '{"prompt": "一只可爱的小花猫,时尚,头上戴着彩色波点蝴蝶结三角头巾,大大的腮红,很可爱,高饱和度,可爱嘟嘟,毛绒绒且柔软,身穿头巾撞色系旗袍,羊毛毡风格,脖子带你呼应色围巾,非常可爱,怀里抱一束花,上半身肖像,送给你的姿势,卡哇伊,画面简约,高饱和度,轻松气氛,丝滑的画质,中景视角,标准镜头,简约风格,32k高清图,萌态十足,蓝天白云背景,精妙无双"}' </curl> json schema请参照下面的例子 <json-schema> { "openapi": "3.1.0", "info": { "title": "Get weather data", "description": "Retrieves current weather data for a location.", "version": "v1.0.0" }, "servers": [ { "url": "" } ], "paths": {}, "components": { "schemas": {} } } </json-schema>
生成的结果如下:
{ "openapi": "3.1.0", "info": { "title": "Generate Image API", "description": "API to generate an image based on a given prompt.", "version": "v1.0.0" }, "servers": [ { "url": "https://cloud.dify.ai" } ], "paths": { "/generate_image/": { "post": { "summary": "Generate an image based on a prompt", "operationId": "generateImage", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/GenerateImageRequest" } } } }, "responses": {} } } }, "components": { "schemas": { "GenerateImageRequest": { "type": "object", "properties": { "prompt": { "type": "string", "description": "The prompt describing the image to be generated." } }, "required": [ "prompt" ] } } } }