生成AIエージェントでのSDKでの関数呼出し
このガイドを使用して、OCI Java SDKを使用したアプリケーションへのファンクション・コールの統合について理解します。次のステップは、ファンクション・コール・ツールを作成し、チャットを使用してファンクション・コール・ツールを起動するためのOCI Java SDKの例を示しています。
前提条件
始める前に、次のリソースを設定しておく必要があります。
- エージェント。生成AIエージェントでのエージェントの作成を参照してください。
- エージェント・エンドポイント。「生成AIエージェントでのエンドポイントの作成」を参照してください。
- 開発環境にインストールされている必要なOCI SDKおよび依存関係。設定および前提条件を参照してください。
関数呼び出しツールの作成
次のステップは、OCI Java SDKを使用してエージェント用のファンクション・コール・ツールを作成する方法を示しています。
-
クライアントを構成します。例:
GenerativeAiAgentRuntimeClient agentClient = GenerativeAiAgentRuntimeClient.builder() .endpoint(endpoint) .configuration(clientConfiguration) .build(provider);
-
関数を定義し、その関数を
ToolConfig
に埋め込みます。たとえば、OCI Object Storageバケットを作成する関数を次に示します。private static Function prepareFunction() { return Function.builder() .name("create_object") .description("A function to create an object storage bucket") .parameters("{\n" + " \"displayName\": \"Create Object Storage Bucket\",\n" + " \"description\": \"Create a new bucket in OCI Object Storage.\",\n" + " \"toolConfig\": {\n" + " \"toolConfigType\": \"FUNCTION_CALLING_TOOL_CONFIG\",\n" + " \"function\": {\n" + " \"name\": \"create_object_storage_bucket\",\n" + " \"description\": \"Creates a new bucket in OCI Object Storage within a specified compartment.\",\n" + " \"parameters\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"bucket_name\": {\n" + " \"type\": \"string\",\n" + " \"description\": \"The name of the bucket to create.\"\n" + " },\n" + " \"compartment_ocid\": {\n" + " \"type\": \"string\",\n" + " \"description\": \"The OCID of the compartment where the bucket will be created.\"\n" + " },\n" + " \"storage_tier\": {\n" + " \"type\": \"string\",\n" + " \"enum\": [\"Standard\", \"Archive\"],\n" + " \"description\": \"The storage tier for the bucket.\"\n" + " }\n" + " },\n" + " \"required\": [\"bucket_name\", \"compartment_ocid\"],\n" + " \"additionalProperties\": false\n" + " }\n" + " }\n" + " }\n" + "}") .build(); } private static ToolConfig createFunctionToolConfig() { return FunctionCallingToolConfig.builder() .function(prepareFunction()) .build(); }
-
ツール・リクエストを作成し、クライアントを使用してツールを作成します。例:
CreateToolDetails createToolDetails = CreateToolDetails.builder() .agentId(agentId) .compartmentId(COMPARTMENT_ID) .toolConfig(toolConfig) .displayName("tool-sdk") .description("tool description") .build(); CreateToolRequest toolRequest = CreateToolRequest.builder() .createToolDetails(createToolDetails) .build(); client.createTool(toolRequest);
エージェントとのチャット
-
セッションを作成します。例:
GenerativeAiAgentRuntimeClient agentClient = GenerativeAiAgentRuntimeClient.builder() .endpoint(endpoint) .configuration(clientConfiguration) .build(provider); // Create a new chat session request CreateSessionRequest request = CreateSessionRequest.builder() .agentEndpointId(<agentEndpointId>) .createSessionDetails(CreateSessionDetails.builder() .description("description") .displayName("display_name") .build()) .build(); String sessionId = agentClient.createSession(request) .getSession() .getId();
<agentEndpointId>を、前提条件セクションのエージェント・エンドポイントのOCIDに置き換えます。
-
セッションを使用して、関数を呼び出すチャット・コールを作成します。この例では、エージェントにメッセージを送信し、ファンクション・コールを含むレスポンスを想定しています。
String message = "Create an Object Store."; // Create Chat request final ChatDetails chatDetails = ChatDetails.builder() .shouldStream(shouldStream) .userMessage(message) .sessionId(sessionId) .build(); final ChatRequest chatRequest = ChatRequest.builder() .chatDetails(chatDetails) .agentEndpointId(agentEndpointId) .build(); final ChatResult chatResult = agentClient.chat(chatRequest);
エージェントからのレスポンスには、起動するファンクションの詳細を含む
required-actions
フィールドが含まれます。chatResult(message=null, traces=[], usageDetails=null, requiredActions=[FunctionCallingRequiredAction(super=RequiredAction(super=BmcModel (__explicitlySet__=[functionCall, actionId]) actionId=<some-action-id>), functionCall=FunctionCall(super=BmcModel(__explicitlySet__=[name, arguments])name=object_storage, arguments= {"bucket_name": "my_object_store", "compartment_ocid": "ocid1.compartment.oc1..xxx", "storage_tier": "Standard" }))], toolResults=null)
レスポンスから、ファンクション・コールに関連付けられている
action-id
を抽出します。このIDは、次のステップでアクションを実行するために使用されます。String actionId = "<some-action-id>";
-
アクションを実行します。レスポンスには、ファンクション・コールの結果を含む、エージェントからの最終レスポンスが含まれます。例:
String actionId = chatResponse.getChatResult().getRequiredActions().get(0).getActionId(); String functionCallOutput = "{"bucket_name": "test_bucket", "compartment_ocid": "
<compartment_OCID>
", "storage_tier":"Standard"}"; PerformedAction performedAction = FunctionCallingPerformedAction.builder() .actionId(actionId) .functionCallOutput(functionCallOutput) .build(); // Execute the chat session with the performed action final ChatDetails chatDetails = ChatDetails.builder() .shouldStream(shouldStream) .userMessage(userMessage) .performedActions(List.of(performedAction)) .sessionId(sessionId) .build(); final ChatRequest chatRequest = ChatRequest.builder() .chatDetails(chatDetails) .agentEndpointId(agentEndpointId) .build(); ChatResult chatResult = agentClient.chat(chatRequest); # Chat Result: # ChatResult(message=The object store 'my_object_store' has been successfully created in the specified compartment with the 'Standard' storage tier., traces=[], usageDetails=null, toolResults=null)