Files

54 lines
2.5 KiB
Java

package com.ankurm.toolconfirmation;
import com.ankurm.toolconfirmation.support.Describe;
import com.ankurm.toolconfirmation.support.Env;
import com.ankurm.toolconfirmation.support.ScriptedChatModel;
import com.ankurm.toolconfirmation.support.Transcript;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.model.tool.ToolCallDecision;
import org.springframework.ai.model.tool.ToolCallDecisions;
import org.springframework.ai.model.tool.ToolExecutionResult;
import static org.assertj.core.api.Assertions.assertThat;
/** What the model is told when a person says no, with and without a message. */
class DenyTest {
@Test
void aDenialIsAToolResultTheModelCanReadAndReactTo() {
Env env = new Env();
env.model.callTools(ScriptedChatModel.call("call_1", "bookFlight", "{\"flightNumber\":\"VX205\"}"),
ScriptedChatModel.call("call_2", "cancelBooking", "{\"reference\":\"BK-9\"}"))
.respond("Understood, I will ask which seat you prefer.");
ChatResponse reply = env.ask("Rebook me on VX205");
try (Transcript t = new Transcript("03-deny-messages.txt", "What the model receives when a call is denied")) {
ToolCallDecisions decisions = ToolCallDecisions.of(
ToolCallDecision.deny("call_1"),
ToolCallDecision.deny("call_2", "The user wants an aisle seat, ask again."));
ToolExecutionResult result = env.resume(Env.user("Rebook me on VX205"), reply, decisions);
Message last = result.conversationHistory().get(result.conversationHistory().size() - 1);
t.line("DEFAULT_DENY_MESSAGE: %s", ToolCallDecision.DEFAULT_DENY_MESSAGE);
t.blank();
t.line("history the model is sent next:");
t.line("%s", Describe.messages(result.conversationHistory()));
t.blank();
t.line("ledger (nothing ran): %s", env.ledger.entries());
t.line("returnDirect(): %s", result.returnDirect());
String answer = env.client.prompt().messages(result.conversationHistory()).call().content();
t.line("model's next answer: %s", answer);
assertThat(env.ledger.entries()).isEmpty();
assertThat(last.toString()).contains(ToolCallDecision.DEFAULT_DENY_MESSAGE)
.contains("The user wants an aisle seat, ask again.");
assertThat(result.returnDirect()).isFalse();
}
}
}