Creating your own tools

Peoplelogic.dev utilizes the powerful high-level tools mechanism from Langchain4j. You can read more about their Tools interface here: https://docs.langchain4j.dev/tutorials/tools

While most of the base LLMs out there are very powerful in their own right, your agents will only be as good as the as the systems they can interact with. That's where your own Tools come into play. Fortunately, creating tools is trivial. Let's take a look at a simple example:

SampleTools.java
@PeoplelogicTools
public class SampleTools {
    @Autowired
    TestContentRetriever retriever;
    
    @Autowired
    ThirdAgent thirdAgent;

    @Tool("Answers the question: 'what is your name?' or answers if someone asks hello")
    public String whatIsYourName() {
        return "Hello. My name is Inigo Montoya. You killed my father. Prepare to die.";
    }
}

Pretty straightforward. The @PeoplelogicTools annotation tells us where to look for tool methods and also ensures that this is a Spring bean (meaning we can also autowire it elsewhere for re-use).

Within the class, we can also bring in other functionality from our application - this might be other agents we want to call, client libraries for some integration or even an externalized property from application.properties.

The @Tool annotation

Finally, we get to the meat and potatoes - the @Tool annotation. This annotated method is what is passed to the LLM when acceptWork is first called on the agent. Behind the scenes, the description (value of the annotation) and the method name are passed to the LLM as an available tool/function and the LLM will return whether we should call that method. The method is then called and it's output sent to the LLM to include in the response.

Methods annotated with @Tool can also have some additional parameters. For example, if you want to be able to leverage the current chat memory (for passing it to other agents), you can annotate a parameter with @ToolMemoryId and it will be auto-filled. You can also defined parameters to your tool method with the @P parameter. This annotation takes a "name" as its value and that value is passed to the LLM to help assign the parameters. You can also define whether the parameter is required.

Let's take a look at all of this put together:

@Tool("Analyzes and provides specific insights with recommendations into improving OKR cycles or goals or project statuses - either provided as a set of files or a single file.If provided as multiple files, assume that it is multiple cycles and you should combine and compare them against each other.  Keep asking for confirmation until the user explicitly confirms.")
public String analyzeOKRCycleOrGoalFiles(@ToolMemoryId String memoryId, 
    @P("CSV or excel export of an individual OKR cycle or several cycles 
    or goals or project status", required=true) String filenames, 
    @P("all data captured, has the user explicitly confirmed?") Boolean confirmed) {
}

This is a real world example from our HR Analyst tools. Here we pass in the memory id for the conversation, accept a parameter that is required and also capture whether the user has confirmed the operation.

As the Langchain4J docs describe above, you can return many different types from your tool methods. Just remember that not all LLMs have great support for JSON objects as return types - especially from tools!

Adding the tools to your agent

Finally, we need to actually tell the agents to use the tools. To do that, you just need to specify this bean on your agent configuration:

@PeoplelogicAgent(value="coordinatorAgent",
        name = "Know It All Agent", tools = {"sampleTools"},
        persona = "Funny but a bit snarky")

Now let's take a look at some common tools that the SDK ships for you to use.

Last updated

Was this helpful?