複数ターンの会話
会話の交代でコンテキストを維持するエージェントを作成する方法について学習します。
計算機マルチターン例
この例では、session_id
パラメータを使用して、エージェントは前の実行のコンテキストを参照し、複数の会話回転にわたってコンテキストを維持できます。
情報:エージェント実行は、定義されたルールおよび指示に基づいて1つ以上のタスクを実行するエージェントのプロセスです。会話(セッションとも呼ばれる)では、ユーザーが順番(質問やコマンドの指定など)を取るたびに、エージェント実行と呼ばれます。この実行中に、エージェントは、タスクを完了してユーザーに応答するために、いくつかのステップ(異なるツールを使用したり、複数の計算を行ったりするなど)を実行する必要がある場合があります。したがって、1人のユーザー・ターン= 1人のエージェントが実行され、最終的な回答を得るためのいくつかのステップが必要になる場合があります。
Python
calculator_multi_turns.py
from oci.addons.adk import Agent, AgentClient
from oci.addons.adk.tool.prebuilt import CalculatorToolkit
def main():
client = AgentClient(
auth_type="api_key",
profile="DEFAULT",
region="us-chicago-1"
)
agent = Agent(
client=client,
agent_endpoint_id="ocid1.genaiagentendpoint...",
instructions="You perform calculations using tools provided.",
tools=[CalculatorToolkit()]
)
agent.setup()
# First turn (here we start a new session)
input = "What is the square root of 256?"
response = agent.run(input, max_steps=3)
response.pretty_print()
# Second turn (here we use the same session from last run)
input = "do the same thing for 81"
response = agent.run(input, session_id=response.session_id, max_steps=3)
response.pretty_print()
if __name__ == "__main__":
main()
Java
CalculateAgentMultiTurn.java
package demos.multiTurn;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.adk.agent.Agent;
import com.oracle.bmc.adk.agent.RunOptions;
import com.oracle.bmc.adk.client.AgentClient;
import com.oracle.bmc.adk.run.RunResponse;
import demos.singleTurnSingleTool.CalculateAgent.SimpleCalculatorToolkit;
import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SessionTokenAuthenticationDetailsProvider;
import java.util.Arrays;
public class CalculateAgentMultiTurn {
public static void main(String[] args) throws Exception {
final String configLocation = "~/.oci/config";
final String configProfile = "DEFAULT";
final String agentEndpointId = "ocid1.genaiagentendpoint...";
BasicAuthenticationDetailsProvider authProvider =
new SessionTokenAuthenticationDetailsProvider(
ConfigFileReader.parse(configLocation, configProfile));
AgentClient agentClient = AgentClient.builder()
.authProvider(authProvider)
.region("us-chicago-1")
.build();
Agent agent = Agent.builder()
.client(agentClient)
.agentEndpointId(agentEndpointId)
.instructions("You perform calculations using tools provided.")
.tools(Arrays.asList(new SimpleCalculatorToolkit()))
.build();
agent.setup();
// First turn (here we start a new session)
String input = "What is the sum of 2 and 3?";
final Integer maxStep = 3;
RunOptions runOptions = RunOptions.builder().maxSteps(maxStep).build();
RunResponse response = agent.run(input, runOptions);
response.prettyPrint();
// Second turn (here we use the same session from last run)
input = "Do the same thing for 3 and 5.";
runOptions.setSessionId(response.getSessionId());
response = agent.run(input, runOptions);
response.prettyPrint();
}
}
次に例を示します。
- 最初のターン: エージェントは、平方根を計算するための初期ユーザー問合せを処理します。
- セッションID: 最初のレスポンスの後、そのレスポンスの
session_id
が取得されます。 - 2回目のターン: ユーザーが
do the same thing for 81
を要求すると、エージェントはコンテキストを理解します。これは、session_id
が2回目のコールに渡されるためです。 session_id
がない場合、エージェントは「同じもの」の意味を理解しません。
仕組み
session_id
パラメータを渡さないと、実行によって新しいセッションが開始されます。
session_id
パラメータを渡すと、新しいセッションを作成するのではなく、指定したセッションが再利用されます。
セッションは、セッションを開始する実行のコンテキストと、同じsession_id
を使用する実行のコンテキストを保持します。