[
{
"category": "Clothing",
"description": "Lightweight mesh running sneakers",
"id": 24,
"name": "Running Shoes",
"price": 109.99
},
{
"category": "Clothing",
"description": "Cross-training athletic shoes",
"id": 83,
"name": "Training Shoes",
"price": 109.99
}
]
So, the agent successfully decided what I meant by “sports activities footwear,” chosen some related key phrases to seek for, filtered the merchandise primarily based on worth, and returned an inventory of two choices for me. As a result of LLMs aren’t deterministic, your outcomes could also be completely different from mine. For instance, in different runs with the identical question, the agent searched for various key phrases and returned a bigger listing. However with the ability to translate a pure language question right into a set of database queries and discover related outcomes is spectacular!
Spring AI’s built-in assist for growing brokers
Now that you simply perceive what an agent loop is, what it does, and deal with instrument executions, let’s have a look at Spring AI’s built-in assist for managing its personal agent loop and power execution. Right here is our up to date ProductSearchAgent code:
package deal com.infoworld.springagentdemo.ai.agent;
import java.util.ArrayList;
import java.util.Checklist;
import com.infoworld.springagentdemo.ai.instruments.ProductSearchTools;
import com.infoworld.springagentdemo.mannequin.Product;
import org.springframework.ai.chat.consumer.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.immediate.Immediate;
import org.springframework.ai.instrument.technique.MethodToolCallbackProvider;
import org.springframework.stereotype.Part;
@Part
public class ProductSearchAgent {
personal closing ChatClient chatClient;
personal closing ProductSearchTools productSearchTools;
public ProductSearchAgent(ChatClient.Builder chatClientBuilder, ProductSearchTools productSearchTools) {
this.chatClient = chatClientBuilder.construct();
this.productSearchTools = productSearchTools;
}
public Checklist run(String userRequest) {
Immediate immediate = buildPrompt(userRequest);
AgentResponse response = chatClient
.immediate(immediate)
.toolCallbacks(
MethodToolCallbackProvider.builder().toolObjects(productSearchTools).construct()
)
.name()
.entity(AgentResponse.class);
System.out.println(response.reply());
return response.merchandise();
}
personal Immediate buildPrompt(String userRequest) {
Checklist messages = new ArrayList();
// 1. System message: defines the agent
messages.add(new SystemMessage("""
You're a product search agent.
Your accountability is to assist customers discover related merchandise utilizing the out there instruments.
Tips:
- Use the supplied instruments each time product information is required.
- It's possible you'll name instruments a number of occasions to refine or broaden the search.
- If the request is obscure, make affordable assumptions and try a search.
- Don't ask follow-up questions.
- Proceed utilizing instruments till you might be assured you will have the absolute best outcomes.
If the person asks about merchandise in a sure worth vary, first seek for the merchandise after which filter
the outcomes primarily based on the value. Every product is outlined with a worth.
When you will have accomplished the search course of, return a structured JSON response on this format:
{
"reply": "...",
"merchandise": [...]
}
Don't return conversational textual content.
Return solely legitimate JSON.
"""));
// Add the person's request
messages.add(new UserMessage(userRequest));
return new Immediate(messages);
}
}
As I discussed earlier, the ProductSearchTools’ searchProducts() technique is annotated with the @Software annotation. This annotation has particular that means for Spring AI if we add a toolCallbacks() technique name to our LLM name. On this case, we autowire the ProductSearchTools into our constructor after which invoke the toolCallbacks() technique in our LLM name, passing it an inventory of all of the lessons containing instruments we wish to give the LLM entry to in a MethodToolCallbackProvider.builder().toolObjects() name. Spring AI will see this listing of instruments and do a couple of issues:
- Introspect all strategies annotated with the
@Softwareannotation within the supplied lessons. - Construct the instrument specification and cross it to the LLM for us, together with the outline of the instrument and the strategy signature, which signifies that we now not have to explicitly outline the instrument specification in our
SystemPrompt. - As a result of it has entry to name the instruments, the
ChatClient’sname()technique will run in its personal agent loop and invoke the instruments it wants for us.
Subsequently, the response we obtain would be the closing response from the LLM with our listing of merchandise, so we don’t have to construct an agent loop ourselves. We construct our immediate with a system immediate (which once more doesn’t have the instrument specification) and the person’s request. We then make a single name to the name() technique, which performs all of the actions it must arrive at a conclusion.
