When exposing methods as tools in LangChain4j, clear communication of parameter intent is key for both developers and the LLM. The @P annotation allows you to describe each parameter, and specify whether it’s required, and help the LLM better understand how to use your tool methods.
How @P Works?
@P annotation takes two attributes.
· value (Mandatory): This field is used to provide a human-readable description of the parameter. It explains what the parameter represents or expects.
· required (Optional, defaults to true): Indicates whether the parameter is mandatory or optional for the method call. If set to false, the parameter is optional.
Example
@Tool("Converts degrees to radians") public Double degreesToRadians(@P(value = "Degrees value to convert", required = true) double degrees) { return Math.toRadians(degrees); }
In this example:
· The degrees parameter is described as “Degrees value to convert.”
· It is marked as required (default behavior).
For optional parameters
@Tool("Greets a user optionally with their name") public String greetUser(@P(value = "Name of the user", required = false) String name) { return (name != null && !name.isEmpty()) ? "Hello, " + name : "Hello!"; }
Why Use @P?
· It provides clear documentation on what inputs your tool expects.
· It allows the LLM to understand which parameters are necessary, preventing errors from missing inputs.
· It improves user experience by enabling better prompt generation and validation.
· It helps in generating auto documentation and more informative API specs.
In summary, Annotating method parameters with @P in LangChain4j is a best practice that improves clarity and control over tool inputs. It guides the LLM in making effective calls and handling optional data gracefully.
Previous Next Home
No comments:
Post a Comment