AI機能
AI機能はカスタム機能によってAIエージェントを強化・拡張します
あなたが AI agents や AI assistants を設定する際、指示文だけでは構成が難しい特定の回答が必要な質問に直面する場合があります。例えば、業界固有の数式が存在し、モデルがそれに精通していない場合や、特定のプロンプトに応じてエージェントにデータベースの更新などのアクションを実行させたい場合などが考えられます。
このような場合(およびその他多くの場合)には、SquidのAI機能を使用できます。Squid Service内の関数に @aiFunction デコレーターを拡張することで、エージェントをこれらの関数に接続し、それぞれのエージェントに必要な機能をカスタマイズすることが可能です。
これらの関数はTypescriptのSquid Service内で定義されるため、Squidプラットフォームの全機能にアクセスできます。つまり、これらの関数のカスタマイズには限界がありません。
AI機能の作成方法
AI機能は関数を説明する文字列である description パラメーターを受け取ります。この説明はAIエージェントが関数をいつ使用するかを判断するために重要であり、明確かつ情報豊かなものである必要があります。
以下の例は、@aiFunction デコレーターを使用して、AIエージェントが利用できる関数を定義する方法を示しています:
import { SquidService, aiFunction } from '@squidcloud/backend';
class AiService extends SquidService {
@aiFunction<{ shipName: string }>(
'This function returns the list of pirates in a ship. ' +
'Call this function whenever the user asks about the crew of a ship.',
[
{
name: 'shipName',
description: 'The name of the ship',
type: 'string',
required: true,
}
]
)
async listPiratesInAShip(params: { shipName: string }): Promise<string> {
const { shipName } = params;
if (shipName === 'Black Pearl') {
return 'Jack Sparrow';
}
else if (shipName === "Queen Anne's Revenge") {
return 'Black Beard';
}
return 'No ship found';
}
}
関数をAIエージェントに追加するには、オプションパラメーターの一部として ask 呼び出しに関数名を渡します:
const response = await squid
.ai()
.agent('AGENT_ID')
.ask('Which pirate was captain of the Black Pearl?', {
functions: ['listPiratesInAShip'],
});
const assistantId = await createAssistant('YOUR_ASSISTANT_NAME', "YOUR ASSISTANT'S INSTRUCTIONS", ['listPiratesInAShip']);
バックエンドをデプロイした後、追加したAI機能はコンソールの Abilities セクションに表示され、Agent Studio内でエージェントによって直接使用可能となります。詳細は Backend deployment guide をご参照ください。
AI機能を用いたデータベース更新
また、AIエージェントにデータベースを更新するための関数を提供することも可能です。例えば、以下の関数は内蔵のno-sql Squidデータベース内の大規模なドキュメントの特定のセクションを更新します:
// Assume we have the following section id definitions
export const SECTION_IDS = ['introduction', 'background', 'methodology', 'results', 'conclusion'] as const;
export type SectionId = typeof SECTION_IDS[number];
@aiFunction('Saves a section in a document', [
{
name: 'sectionId',
type: 'string',
description: 'Which section to update in the document',
required: true,
enum: [...SECTION_IDS],
},
{
name: 'content',
type: 'string',
description: 'The content of the section',
required: true,
},
])
async saveSection(
{
sectionId,
content,
}: {
sectionId: SectionId;
content: string;
},
): Promise<string> {
const docRef = this.squid.collection("documents").doc("my-doc");
await docRef.update({ [sectionId]: content });
return 'section saved: ' + sectionId;
}
ここでは sectionId の可能な値を制限するためにenumも使用しています。これにより、AIエージェントはどの値が有効であるかを理解しやすくなります。
AI Functionへのパラメーターの渡し方(Agent Contextを介して)
また、エージェントコンテキストを通してAI機能にパラメーターを渡すことも可能です。これは、関数に追加情報を提供したい場合に有用です。例えば、前述の例を拡張して、クライアントからのユーザー選択に基づきデータベースで更新されるドキュメントIDを渡す場合は次のようになります:
// Assume we have the following section id definitions
export const SECTION_IDS = ['introduction', 'background', 'methodology', 'results', 'conclusion'] as const;
export type SectionId = typeof SECTION_IDS[number];
// Assume we defined agent context as the following
interface DocumentIdAgentContext {
documentId: string;
}
@aiFunction('Saves a section in a document', [
{
name: 'sectionId',
type: 'string',
description: 'Which section to update in the document',
required: true,
enum: [...SECTION_IDS],
},
{
name: 'content',
type: 'string',
description: 'The content of the section',
required: true,
},
])
async saveSection(
{
sectionId,
content,
}: {
sectionId: SectionId;
content: string;
},
ctx: AiFunctionCallContext<unknown, DocumentIdAgentContext>,
): Promise<string> {
const docRef = this.squid.collection("documents").doc(ctx.agentContext.documentId);
await docRef.update({ [sectionId]: content });
return 'section saved: ' + sectionId;
}
必要なパラメーターは、squid chat widget(またはclient SDK経由)を使ってエージェントに渡すことができます。例えば:
<squid-chat-widget
...
squid-ai-agent-chat-options={{
agentContext: { documentId: "my_document_id" },
}}
...
>
</squid-chat-widget>
次のステップ
より複雑なAI機能の実例を見てみたいですか? AI home maintenance tutorial をご覧ください。