AI Personal Learning
and practical guidance
讯飞绘镜

Dify Customization Tool Example

1. Weather (JSON)

{
"openapi": "3.1.0",
"info": {
"title": "Get weather data",
"description": "Retrieves current weather data for a location.",
"version": "v1.0.0"
},
"servers": [
{
"url": "https://weather.example.com"
}
],
"paths": {
"/location": {
"get": {
"description": "Get temperature for a specific location",
"operationId": "GetCurrentWeather",
"parameters": [
{
"name": "location",
"in": "query",
"description": "The city and state to retrieve the weather for",
"required": true,
"schema": {
"type": "string"
}
}
],
"deprecated": false
}
}
},
"components": {
"schemas": {}
}
}

This code is a JSON file of the OpenAPI specification that describes an API for obtaining weather data.In a nutshell, this API allows the user to query weather data for a specific location.

  • "openapi": "3.1.0": This indicates that the version of the OpenAPI specification used is 3.1.0.
  • "info": This section provides basic information about the API, including title, description, and version.
  • "servers": This section lists the server URLs for the API.
  • "paths": This section defines the paths and operations of the API. In this example, there is a GET operation with a path of/location, which is used to get the weather for a specific location. This operation requires a file namedlocationThe query parameter, which is required, is of type string.
  • "components": This section is used to define the schema used in the API, but in this example it is empty.

In the OpenAPI specification, theparametersis an array that defines the input parameters for API operations.parametersdefines a file namedlocationThe query parameter, which is required, is of type string. Each parameter is an object containing the following properties:

  • "name": The name of the parameter.
  • "in": The location of the parameter. This can be"query""header""path"maybe"cookie"The
  • "description": Description of the parameter.
  • "required": If the value fortrueThis parameter is required if the
  • "schema": The data type of the parameter.

Focusing on the location of the parameters, the 4 cases are shown below:

  • header parameter to pass the API key in the request header.
  • path parameter to specify the ID of the object to be retrieved in the URL path.
  • The cookie parameter, used to pass the user's session ID in a cookie.
  • The query parameter, appended to the URL, is usually used to provide information filtering.

Create a customized tool interface:

Dify自定义工具例子-1


Test tool interface interface:

Dify自定义工具例子-2

 

2. Pet Shop (YAML)

# Taken from https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: https://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
maximum: 100
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:    
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
maxItems: 100
items:
$ref: "#/components/schemas/Pet"
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

pet.yaml is an OpenAPI specification file that describes and defines the interface to an API called "Swagger Petstore". This file uses the YAML format, which is a human-readable data serialization standard for writing configuration files. This file provides a clear definition of the API interface for developers to know how to interact with the "Swagger Petstore" API. The main parts of the file include:

  • openapi: This field specifies the version of the OpenAPI specification used, in this case "3.0.0".
  • info: This section provides basic information about the API, including version, title, and license.
  • servers: This section defines the server URL for the API.
  • paths: This section defines all the paths and operations of the API. For example./pets The path has two operations:get cap (a poem) postTheget operation is used to list all pets.post Operations are used to create a new pet. Each operation has its own parameters, responses and other details.
  • components: This section defines reusable schemas that can be used in the paths cited in the section. For example.Pet,Pets cap (a poem) Error Mode.

Dify自定义工具例子-3

Convert the above YAML file to JSON format:

{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "https://petstore.swagger.io/v1"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": ["pets"],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"maximum": 100,
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"summary": "Create a pet",
"operationId": "createPets",
"tags": ["pets"],
"responses": {
"201": {
"description": "Null response"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/pets/{petId}": {
"get": {
"summary": "Info for a specific pet",
"operationId": "showPetById",
"tags": ["pets"],
"parameters": [
{
"name": "petId",
"in": "path",
"required": true,
"description": "The id of the pet to retrieve",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Expected response to a valid request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"maxItems": 100,
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"type": "object",
"required": ["code", "message"],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}

 

3. Blank template

{
"openapi": "3.1.0",
"info": {
"title": "Untitled",
"description": "Your OpenAPI specification",
"version": "v1.0.0"
},
"servers": [
{
"url": ""
}
],
"paths": {},
"components": {
"schemas": {}
}
}

Note: It seems that the JSON format looks more intuitive.

Dify自定义工具例子-4

 

Structured Output Tutorial:How to use jsonarray object in Dify?

May not be reproduced without permission:Chief AI Sharing Circle " Dify Customization Tool Example
en_USEnglish