0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Google Cloud の会話型 AI で任意の API を呼び出してみた

Posted at

はじめに

会話型 AI を使っていく中で、自身でカスタマイズした API を AI が利用できたらより AI が使い易くなるのでは?と思い、会話型 AI で任意の API を呼び出す方法をまとめました。

アーキテクチャ

会話型 AI として Vertex AI の AI Application にある Conversational Agents を使いました。
architecture.png

1. Open API の作成

今回はユーザとのやり取りで得た情報を Cloud Run Functions に与えてデータベースに書き込む API を作成します。
中身については別の記事でまとめているのでそちらをご参照ください。

2. Conversational Agnets の作成

AI Applications の Google Cloud Console から「Create App」で Conversational Agents を「Build your own」で作成します。
名前は何でもよいです。
createApp.png

そして左のペインから「Tool」を選択し、「Create Tool」を押下します。
createTool.png
Tool name を適当 (本記事では Record Favorite Sports) に入力します。
Schema のところは Yaml のトグルボタンを選択したのち、以下を記載します。
こうしてあげることで、Open API に渡すデータの形式を制御することができます。

openapi: 3.0.0
info:
  title: Favorite Sports API
  version: 1.0.0
servers:
  - url: 'YOUR_CLOUD_RUN_FUNCTION_URL' # 自身で作成した Open API の URL を入力
paths:
  /:
    post:
      summary: Record a user's favorite sports
      operationId: recordFavoriteSports
      requestBody:
        description: Favorite sports to add
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FavoriteSports'
      responses:
        '200':
          description: Success
components:
  schemas:
    FavoriteSports:
      type: object
      properties:
        name:
          type: string
        favorite_sports:
          type: string

左ペインの「Playbook」からGoals と Instractions を下記のようにします。
Playbook name は何でもよいです。

Get user's name and favorite sports.
- Greet a user and tell them a fact about ice cream.
- Ask them to share their favorite flavor of ice cream and their email address to receive special promotions they might enjoy.
- When they have provided both, record them with ${TOOL:Record Favorite Flavor}
- Thank the user and suggest that they try our new Chocolate Strawberry Swirl flavor.

下図は設定後の画面
modifyPlaybook.png

そして、Dialogflow Service Agents に Cloud Run Invoker ロールを付与します。
grantCloudRunInvokerRole.png

今回は Open API を Cloud Run Functions にしたのでこの IAM ロールを付与しましたが、そうでない場合は不要です。

3. 動作確認

Conversational Agents の Console 画面右上にあるチャットマークみたいなところから動作確認をしてみます。
checkConversationalAgents.png

成功したようなので BigQuery 側を確認すると下記のようにちゃんとデータが反映されていることが確認されました。
checkBigQueryTable.png

おわりに

簡単ではありましたが、会話型 AI で任意の API を呼び出す方法を示しました。
この方法を応用すれば、基本的には AI と会話ベースで解決しつつ、必要な情報取得やデータベースへの追加 (ホテル予約とか) も出来るようになるのではないかと思います。
また、時間があればより実用的なユースケースについて試してみたいと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?