見出し画像

OpenAI API を使ったチャットボット(応用編)

OpenAI API を使ったチャットボット(設定編)の続きです。ここでは、OnePrompt アプリのカスタム・プラグイン機能について説明します。カスタム・プラグインは、外部ファイルを使ってアプリ内から他のサービスAPIにアクセスする仕組みです。OpenAI API の新機能 "Function Calling" をプログラムせずに試せます。

OnePrompt のカスタム・プラグインのページを参考に、ホットペッパーの API を利用したプラグインを作成します。ホットペッパーの API を利用するには、API キーの発行が必要です。リクルートWebサービスのサイトにアクセスして「新規登録」からユーザ登録を行なって、APIキーの発行手続きを進めて下さい。

まずは、完成版のホットペッパーのカスタム・プラグインの内容を示します。以下のJSON 形式のデータの "Your-API-Key" の箇所を、入手した API キーで置き換えればアプリで利用できます。

{
  "name": "HotPepper Plugin",
  "description": "You can search restaurants with [keywords] in order to satisfy user's request. The [keywords] must be separated by white space.",
  "functions": [
    {
      "name": "search_restaurant",
      "description" : "Search restaurants with HotPepper API.",
      "parameters": {
        "type": "object",
        "properties": {
          "free_drink": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests '飲み放題'."
          },
          "free_food": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests '食べ放題'."
          },
          "sake": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests '日本酒'."
          },
          "wine": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests 'ワイン'."
          },
          "shochu": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests '焼酎'."
          },
          "cocktail": {
            "type": "number",
            "description" : "Set this value to 1 when the user requests 'カクテル'."
          },
          "keyword": {
            "type": "string",
            "description" : "Set search keywords separated by white space. The each keyword is the station name, the area name, the shop name, the food name or the genre of cuisine that user wants to eat or drink."
          }
        },
        "required": [ "keyword" ]
      }
    }
  ],
  "plugins": [
    {
      "plugin_name": "search_restaurant",
      "http_method": "GET",
      "url_string": "https://webservice.recruit.co.jp/hotpepper/gourmet/v1/",
      "parameters": {
        "key": "Your-API-Key",
        "count": 5,
        "type": "lite",
        "format": "json"
      },
      "filter": {
        "paths": [
          "results", "shop"
        ],
        "type": "array",
        "keys": [
          "access",
          "address",
          "genre.name:genre",
          "name",
          "urls.pc:url"
        ]
      }
    }
  ]
}

以下はダウンロード用のファイルです。"Your-API-Key" の箇所を入手した API キーで置き換えて利用できます。

OnePrompt アプリ内でカスタム・プラグインを使うには、もう一手間かかります。作成したプラグイン・ファイルを特定のフォルダに保存する必要があります。

ファイル・アプリ内の OnePrompt フォルダ

iPhone のファイル・アプリを起動し、「この iPhone 内」を選択して、「OnePrompt」フォルダを開きます。Exports, Imports, Plugins のフォルダが確認できるでしょう。ここで、Plugins フォルダを選択します。

Plugins フォルダ

スクリーンショットのように "hotpepper.plugin" ファイルをコピーします。スクリーンショットでは、他のプラグインも表示されていますが、自分で作成したプラグイン・ファイルが表示されていれば問題ありません。

カスタム・プラグインが利用可能なチャット画面

カスタム・プラグインが利用可能な場合には、チャット画面にパズルのピースのアイコンが表示されます。このアイコンをタップすると、先の Plugins フォルダに保存したプラグインのタイトルがメニューが表示されます。

カスタム・プラグイン・メニュー

"HotPepper Plugin" を選択して、プラグインを有効にします。なお、OnePrompt アプリでは、カスタム・プラグインの複数同時利用はできません。また、アプリ内蔵のプラグインとの同時利用もできません。これは、API で利用するトークン制約を考慮しての仕様です。

ホットペッパー・プラグインを使用したチャット

カスタム・プラグインを活用して、チャット上で焼肉屋に関する情報を入手できました。ここでは、ホットペッパーのAPIを利用したカスタム・プラグインの開発方法を紹介しました。しかし、実際の利用においては、焼肉屋情報を得る際にはホットペッパーのiOSアプリを使用する方が便利です。ただし、提供されているAPIがありながらiOSアプリが存在しない場合、OnePrompt内でカスタム・プラグインを開発し活用するのも一つの手段です。OnePromptアプリのコンセプトは、個人専用のアシスタントを実現することにありますので。

OpenAI API を使ったチャットボット(エンタメ編)に続きます。

この記事が気に入ったらサポートをしてみませんか?