Interacting with API

AIFINEX Flow ile harici API entegrasyonlarını nasıl kullanacağınızı öğrenin

OpenAPI Spesifikasyonu (OAS), HTTP API'lerine yönelik standart, dilden bağımsız bir arayüz tanımlar. Bu kullanım senaryosunun amacı, LLM'nin hangi API'nin çağrılacağını otomatik olarak bulmasını sağlamak ve aynı zamanda kullanıcı ile durum bilgisi içeren bir görüşme yapmaktır.

OpenAPI Chain

  1. Bu eğitimde Klarna OpenAPI'yi kullanacağız.

{
  "openapi": "3.0.1",
  "info": {
    "version": "v0",
    "title": "Open AI Klarna product Api"
  },
  "servers": [
    {
      "url": "https://www.klarna.com/us/shopping"
    }
  ],
  "tags": [
    {
      "name": "open-ai-product-endpoint",
      "description": "Open AI Product Endpoint. Query for products."
    }
  ],
  "paths": {
    "/public/openai/v0/products": {
      "get": {
        "tags": [
          "open-ai-product-endpoint"
        ],
        "summary": "API for fetching Klarna product information",
        "operationId": "productsUsingGET",
        "parameters": [
          {
            "name": "countryCode",
            "in": "query",
            "description": "ISO 3166 country code with 2 characters based on the user location. Currently, only US, GB, DE, SE and DK are supported.",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "q",
            "in": "query",
            "description": "A precise query that matches one very small category or product that needs to be searched for to find the products the user is looking for. If the user explicitly stated what they want, use that as a query. The query is as specific as possible to the product name or category mentioned by the user in its singular form, and don't contain any clarifiers like latest, newest, cheapest, budget, premium, expensive or similar. The query is always taken from the latest topic, if there is a new topic a new query is started. If the user speaks another language than English, translate their request into English (example: translate fia med knuff to ludo board game)!",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "size",
            "in": "query",
            "description": "number of products returned",
            "required": false,
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "min_price",
            "in": "query",
            "description": "(Optional) Minimum price in local currency for the product searched for. Either explicitly stated by the user or implicitly inferred from a combination of the user's request and the kind of product searched for.",
            "required": false,
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "max_price",
            "in": "query",
            "description": "(Optional) Maximum price in local currency for the product searched for. Either explicitly stated by the user or implicitly inferred from a combination of the user's request and the kind of product searched for.",
            "required": false,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Products found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProductResponse"
                }
              }
            }
          },
          "503": {
            "description": "one or more services are unavailable"
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {
      "Product": {
        "type": "object",
        "properties": {
          "attributes": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "name": {
            "type": "string"
          },
          "price": {
            "type": "string"
          },
          "url": {
            "type": "string"
          }
        },
        "title": "Product"
      },
      "ProductResponse": {
        "type": "object",
        "properties": {
          "products": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Product"
            }
          }
        },
        "title": "ProductResponse"
      }
    }
  }
}
  1. Bir .yaml dosyası olarak kaydedin ve OpenAPI Chain'e yükleyin, ardından bazı sorular sorarak test edin. OpenAPI Chain, OpenAPI spesifikasyonunu JSON Schema'ya ayrıştırarak LLM'nin API çağrısı için doğru yöntemi ve parametreleri otomatik olarak kullanmasını sağlar.

  1. Ancak, normal bir konuşma sohbeti yapmak istiyorsanız, bunu yapamaz. Aşağıdaki hatayı göreceksiniz:

Tool Agent + OpenAPI Chain

Yukarıdaki hatayı çözmek için Agent kullanabiliriz.

  1. OpenAPI Chain'i bir Chain Tool ile bağlayın. Bu, chain'in tool olarak kullanılmasına izin verir. Tool altında, LLM'nin bu tool'u ne zaman kullanması gerektiğine dair uygun bir açıklama veririz. Örneğin:

  1. Chain Tool'u bir Tool Agent ile bağlayın:

  1. Hadi deneyelim!

Sonuç

Gerektiğinde API ile etkileşime girebilen ve yine de kullanıcılarla durum bilgisi içeren konuşmaları gerçekleştirebilen bir agent oluşturmayı başardık. Şablon aşağıdadır:

JSON DOSYASI LİNKİ!!

Last updated