Agent Executor
Initialize Logger
try( var file = new java.io.FileInputStream("./logging.properties")) {
var lm = java.util.logging.LogManager.getLogManager();
lm.checkAccess();
lm.readConfiguration( file );
}
var log = org.slf4j.LoggerFactory.getLogger("AgentExecutor");
Create Tools
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import java.util.Optional;
import static java.lang.String.format;
public class TestTool {
private String lastResult;
Optional<String> lastResult() {
return Optional.ofNullable(lastResult);
}
@Tool("tool for test AI agent executor")
String execTest(@P("test message") String message) {
lastResult = format( "test tool executed: %s", message);
return lastResult;
}
}
import static org.bsc.langgraph4j.StateGraph.END;
import static org.bsc.langgraph4j.StateGraph.START;
import org.bsc.langgraph4j.CompileConfig;
import org.bsc.langgraph4j.RunnableConfig;
import org.bsc.langgraph4j.checkpoint.BaseCheckpointSaver;
import org.bsc.langgraph4j.checkpoint.MemorySaver;
import org.bsc.langgraph4j.state.AgentState;
import org.bsc.langgraph4j.serializer.StateSerializer;
import org.bsc.langgraph4j.agentexecutor.AgentExecutor;
import dev.langchain4j.model.openai.OpenAiChatModel;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
var chatLanguageModel = OpenAiChatModel.builder()
.apiKey( System.getenv("OPENAI_API_KEY") )
//.modelName( "gpt-3.5-turbo-0125" )
.modelName( "gpt-4o-mini" )
.logResponses(true)
.maxRetries(2)
.temperature(0.0)
.maxTokens(2000)
.build();
var agentExecutor = new AgentExecutor();
var stateGraph = agentExecutor.graphBuilder()
.chatLanguageModel(chatLanguageModel)
.objectsWithTools(List.of(new TestTool()))
.build();