AIパーソナル・ラーニング
と実践的なガイダンス
讯飞绘镜

Difyカスタマイズツールの例

1.天気(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": {}
}
}

このコードは、気象データを取得するためのAPIを記述したOpenAPI仕様のJSONファイルである。一言で言えば、このAPIによって、ユーザーは特定の場所の気象データを照会することができる。

  • "openapi": "3.1.0"これはOpenAPI仕様のバージョンが3.1.0であることを示す。
  • "info": このセクションでは、APIのタイトル、説明、バージョンなどの基本情報を提供します。
  • "servers"このセクションでは、APIのサーバーURLを列挙する。
  • "paths"このセクションでは、APIのパスとオペレーションを定義する。この例では、パスが/locationこれは、特定の場所の天気を取得するのに使われる。この操作にはlocationクエリーパラメーターは必須で、文字列型である。
  • "components"このセクションはAPIで使用されるパターンを定義するために使用されるが、この例では空である。

OpenAPI仕様ではparametersは、API操作の入力パラメータを定義する配列である。parametersという名前のファイルを定義する。locationクエリ・パラメータは必須で、文字列型である。各パラメータは、以下のプロパティを含むオブジェクトです:

  • "name"パラメータ名。
  • "in"パラメータの位置。これは"query""header""path"もしかしたら"cookie".
  • "description"パラメータの説明
  • "required"もしtrueこのパラメータは
  • "schema"パラメータのデータ型。

パラメータの位置に注目し、4つのケースを以下に示す:

  • ヘッダー・パラメーターを使用して、リクエスト・ヘッダーにAPIキーを渡します。
  • path パラメータを使用して、URL パスで取得するオブジェクトの ID を指定します。
  • ユーザーのセッションIDをCookieで渡すためのCookieパラメータ。
  • URLに付加されるクエリーパラメーターは、通常、情報のフィルタリングに使われる。

カスタムツールインターフェースを作成する:

Dify自定义工具例子-1


テストツールのインターフェースインターフェース:

Dify自定义工具例子-2

 

2.ペットショップ(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 はOpenAPI仕様ファイルで、"Swagger Petstore "と呼ばれるAPIのインターフェイスを記述・定義している。このファイルは、設定ファイルを記述するための人間が読めるデータシリアライゼーション標準であるYAMLフォーマットを使用しています。このファイルは、開発者が "Swagger Petstore "APIと対話する方法を知るためのAPIインターフェースの明確な定義を提供します。ファイルの主な部分は以下の通りです:

  • openapiこのフィールドは、使用されるOpenAPI仕様のバージョンを指定する。
  • info: このセクションでは、バージョン、タイトル、ライセンスなど、APIに関する基本的な情報を提供します。
  • serversこのセクションでは、APIのサーバーURLを定義する。
  • pathsこのセクションでは、APIのすべてのパスと操作を定義する。例えば/pets パスには2つの操作がある:get 歌で応える post.get 操作は、すべてのペットをリストアップするために使用されます。post 操作は新しいペットを作るために使われる。それぞれの操作には、パラメータやレスポンスなどの詳細があります。
  • componentsこの節では、次のような再利用可能なスキーマを定義する。 paths セクションで引用されている。例えばPetそしてPets 歌で応える Error モードだ。

Dify自定义工具例子-3

上記のYAMLファイルをJSON形式に変換する:

{
"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.空白のテンプレート

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

注:JSON形式の方がより直感的に見えるようだ。

Dify自定义工具例子-4

 

構造化出力チュートリアル:Difyでjsonarrayオブジェクトを使うには?

無断転載を禁じます:チーフAIシェアリングサークル " Difyカスタマイズツールの例
ja日本語