設定と実行の分離
エージェント設定の処理および本番での実行について学習します。
setupと runの呼び出しを分離する理由
クイックスタートの例のステップ2では、agent.setup()
とagent.run()
が同じスクリプトに書き込まれ、同じプロセスで実行されます。この方法は、簡単な「Hello World」に十分です。
ただし、本番では、これらの呼出しを個別のフローに埋め込むことができます。
agent.setup()
メソッドは、指示またはツールの更新がある場合にのみ起動する必要があります。たとえば、CI/CDパイプラインの一部としてagent.setup()
をトリガーしたり、顧客がエージェントの指示またはツールを更新するたびに、プラットフォームで顧客構成エージェントが許可される場合があります。
一方、agent.run()
は、エージェントが処理するリクエストを受信したときに実行する必要があります。たとえば、Webアプリケーション、Slackチャットボット、またはエンド・ユーザーのその他のアプリケーション・リクエスト処理フローの一部として実行をトリガーできます。このようなフローは、通常、エージェント構成フローではありません。
コードの再利用中に別の呼出しを実行
ADKを使用すると、同じエージェント定義を使用しながら、適切なコンテキストまたはフローでagent.setup()
およびagent.run()
を個別に起動できます。例:
Python
agent_def.py
# Shared agent definition component using ADK
# To be imported and used by agent_setup.py and agent_run.py
@tool
def get_weather(location: str) -> Dict[str, str]:
"""Get the weather for a given location"""
return {"location": location, "temperature": 72, "unit": "F"}
client = AgentClient(
auth_type="api_key",
profile="DEFAULT",
region="us-chicago-1",
)
weather_agent = Agent(
client=client,
agent_endpoint_id="YOUR_AGENT_ENDPOINT_ID",
instructions="You perform weather queries using tools.",
tools=[get_weather]
)
agent_setup.py
# The setup code
# Invoke when agent definition changes
from agent_def import weather_agent
weather_agent.setup()
agent_run.py
# The run code
# Invoke when there's a request to handle
from agent_def import weather_agent
input = "Is it cold in Seattle?"
response = agent.run(input)
response.pretty_print()
Java
WeatherAgentSetup.java
package demos.weather;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.adk.agent.Agent;
import com.oracle.bmc.adk.client.AgentClient;
import com.oracle.bmc.adk.tools.Param;
import com.oracle.bmc.adk.tools.Tool;
import com.oracle.bmc.adk.tools.Toolkit;
import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SessionTokenAuthenticationDetailsProvider;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
public class WeatherAgentSetup {
public static class WeatherToolkit extends Toolkit {
/**
* Get the weather for a given location.
*
* @param location The location to get the weather for.
* @return A map containing the weather information.
*/
@Tool(name = "getWeather", description = "Get weather of a given location")
public static Map<String, String> getWeather(
@Param(description = "The location") String location) {
Map<String, String> weather = new HashMap<>();
weather.put("location", location);
weather.put("temperature", "72");
weather.put("unit", "F");
return weather;
}
}
public static Agent setupWeatherAgent() {
// Initialize the AgentClient
final String configLocation = "~/.oci/config";
final String configProfile = "DEFAULT";
BasicAuthenticationDetailsProvider authProvider =
new SessionTokenAuthenticationDetailsProvider(
ConfigFileReader.parse(configLocation, configProfile));
AgentClient agentClient = AgentClient.builder()
.authProvider(authProvider)
.region("us-chicago-1")
.build();
Agent weatherAgent = Agent.builder()
.client(agentClient)
.agentEndpointId("YOUR_AGENT_ENDPOINT_ID")
.instructions("You perform weather queries using tools.")
.tools(Arrays.asList(new WeatherToolkit()))
.build();
return weatherAgent;
}
}
WeatherAgentSetupRunner.java
package demos.weather;
public class WeatherAgentSetupRunner {
public static void main(String[] args) {
try {
// Create and set up the weather agent
WeatherAgentSetup.setupWeatherAgent().setup();
System.out.println("Weather agent setup completed.");
} catch (Exception e) {
System.err.println("Failed to set up the weather agent: " + e.getMessage());
e.printStackTrace();
}
}
}
WeatherAgentRun.java
package demos.weather;
import com.oracle.bmc.adk.agent.Agent;
import com.oracle.bmc.adk.utils.RunResponse;
public class WeatherAgentRun {
public static void main(String[] args) {
try {
// Create the agent instance
Agent weatherAgent = WeatherAgentSetup.setupWeatherAgent().setup();
// Input prompt
String input = "Is it cold in Seattle?";
// Run the agent with the input
RunResponse response = weatherAgent.run(input);
// Print the result
response.prettyPrint();
} catch (Exception e) {
System.err.println("Failed to run the weather agent: " + e.getMessage());
e.printStackTrace();
}
}
}