Agent Introduction

Agent is a hot topic in AI. Many tools, libraries, services, platforms claim that they can build AI agents. Agent is a broad concept. An agent can be a chatbot to provide cooking suggestions. It can also provide comprehensive results of a research topic.

It’s hard to give agent a clear definition in the context of AI. The Merriam-Webster Dictionary has several definitions of agent. Below are definitions related to agent in AI.

  • one that acts or exerts power
  • something that produces or is capable of producing an effect
  • a means or instrument by which a guiding intelligence achieves a result
  • a computer application designed to automate certain tasks (such as gathering information online)

There are different ways to create agents. Many agent platforms don’t require coding skills to build agents. In this book, I’ll focus on building agents using Java.

An agent have five components.

  • Profile, description of an agent. It may include elements such as background and demographics.
  • Tools, tools used to complete tasks or acquire information.
  • Knowledge and memory, annotates context with most relevant information.
  • Reasoning and evaluation, enables self-reflection and internal reasoning for the completion of a task.
  • Planning and feedback, organizes tasks to achieve high-level goals.

Not all agents have all these five components mentioned above. Having more components will make an agent more powerful, but harder to implement. When building an agent, the components to add depend on the intended usage scenarios. For the agent to provide cooking suggestions, adding a profile is enough. For the agent to do a research, it may require all those five components.

Let’s start from building the agent for cooking suggestions.

Cooking Suggestion Agent

This agent provides cooking suggestions and it only use the profile component. The profile of an agent is set in the system prompt when interacting with the LLM.

This agent is a Spring AI application. The code below shows the REST controller. The key point is the system text, which describes the persona of this agent.

After starting the agent, we can test the agent using Swagger UI. The screenshot below shows the response from the agent. The input is cook fish. The agent provides suggestions about how to cook fish.

Test agent using Swagger UI
Figure 11. Test agent using Swagger UI

In the system text, we instruct the agent to ignore unrelated inputs. This is a common practice to avoid abuse of an agent. The screenshot below shows the response from the agent with a bad input. The input is write quick sort algorithm in java, which is unrelated to cooking. So the agent refuses to give output.

Bad input for the agent
Figure 12. Bad input for the agent