メインコンテンツまでスキップ

AI で Data を Query する

Squid AI の Query with AI feature を使用して、data に関する information を real-time に取得します。​

構築するもの​

  • healthcare data について質問できる app。
  • data は random に生成され、Squid の built-in database に保存されます。

学ぶこと​

  • data について質問するための Squid executeAiQuery method の使用方法。
  • client が backend と interface できるようにする executable function の構成。

TypeScript のある程度の experience が必要です。React の experience があると役立ちますが、必須ではありません。


この tutorial で collaboration していただいた Otairo の partner に心より感謝します!

Environment Setup​

  1. In the Squid Console, switch to the dev environment.

switch environment

  1. Download the ai-for-data code sample using the following command. Replace the placeholders with the values from your Squid application as shown in the console.

  2. npx @squidcloud/cli init-sample ai-for-data --template ai-for-data --appId YOUR_SQUID_APP_ID --apiKey YOUR_SQUID_API_KEY --environmentId dev --squidDeveloperId YOUR_SQUID_DEVELOPER_ID --region YOUR_REGION

TIP

You can find your environment variables under: 'Application' -> 'Show env vars' as seen below:

switch environment

  1. Open the project in the IDE of your choice.
  2. Start the app locally by running the following command in the project folder:
npm run start
  1. To view the app, navigate to localhost:PORT, where PORT is logged in the terminal. The address will likely be http://localhost:5173.

App の Functionality を確認する​

  1. Add mock data button をクリックし、random に生成された data を database に追加します。data は real-time で client に sync されるため、追加されたことを確認できます。

  2. Squid Console で、app の Connectors tab をクリックします。

  3. built_in_db connector の ... をクリックし、dropdown から Schema を選択します。

  4. 次に、schema の collection と field に description を追加します。これにより Squid AI は data をよりよく理解し、question により正確な answer を提供できます。

    health_data collection の pencil button をクリックします。collection description に以下を追加します。

Each document contains patient information regarding their age, patient ID, and their medical diagnosis.
  1. age field の ... button をクリックして、dropdown menu から Edit field を選択します。以下の description を追加してから、Update をクリックします。
The age of the patient
  1. diagnosis field の ... button をクリックして、dropdown menu から Edit field を選択します。以下の description を追加してから、Update をクリックします。
The patient's medical diagnosis
  1. patentId field の ... button をクリックして、dropdown menu から Edit field を選択します。以下の description を追加してから、Update をクリックします。
The patient's ID
  1. Save schema をクリックします。これにより Squid AI に schema と description が提供され、data に対する query の作成方法を理解できるようになります。

  2. sample app で、chatbot に data について question を尋ねます。Squid AI は prompt に基づいて database query を自動生成します。以下の質問例を試してください。

  • flu と diagnosis された person は何人ですか?
  • 50 歳を超える person の中で、最も common な diagnosis は何ですか?
  • asthma と diagnosis された patient の average age はいくつですか?

Code の内訳​

Backend で AI Query を実行する​

Squid backend の data-ai-service.ts file には、@executable decorator を持つ class があります。Squid の executable は、server 上で実行される function を client に提供します。

注意

executable は client から直接 access でき、backend resource と secret への unlimited permission を持ちます。したがって、authorized user のみが executable を実行できることを確保するため、特に注意する必要があります。

executeAiQuery method は database の connector ID と question を parameter として受け取り、Squid AI response に resolve する promise を返します。詳細については、AI query documentationを参照してください。

backend/src/data-ai-service.ts
export class SquidAiDataService extends SquidService {
@executable()
async askQuestion(question: string): Promise<AiResponse> {
const aiResponse = await this.squid.ai().executeAiQuery('built_in_db', question);
return {
author: 'ai',
answer: aiResponse.answer,
executedQuery: aiResponse.executedQuery,
explanation: aiResponse.explanation,
};
}
}

Client で Question を尋ねる​

frontend では、client は executeFunction method を使用して backend に question を渡します。executable function の name とその function の parameter を渡します。

frontend/src/components/aiDatabot.tsx
squid.executeFunction('askQuestion', question).then((response) => {
// Handle the response
}

これで完了です!​

おめでとうございます!Squid AI 上で実行される chatbot を使用して、data に関する question に answer する web app を正常に構築・実行できました。次に試すこと:

Additional Note​

Squid Environment​

  • Squid は 2 つの異なる target environment を提供します。development 用の dev と production 用の prod です。この tutorial では dev environment を使用します。詳細については、Squid の environmentを参照してください。
  • Squid developer ID は、project をローカルで実行する場合にのみ使用されます。

Squid Functionality​

  • Squid built-in database を使用する場合、Squid backend は追加の customization なしで完全に機能します。Scheduler、database event に response する Trigger、raw SQL または database-specific query を実行する native query などの feature を Squid backend に追加することもできます。すべての option を確認するには、Backend SDK docsを参照してください。
  • built-in database は document-based であるため、notes collection は document の collection を表します。SQL database を使用する場合、collection は table を表し、document は table の row です。