Get startedGet started for free

The text-to-query agent architecture

1. The text-to-query agent architecture

Great work so far! It's time to take our manual text-to-query workflow and begin making it agentic.

2. The agentic approach

At its core, an agent consists of an LLM and a set of tools that the LLM can use to perform actions. Our text-to-query agent will have access to a set of database tools that it can use to read the database schema, understand its content, perform queries, and more. For example,

3. The agentic approach

if a user asks the agent a question, such as "Give the top 5 latest movie releases." The agent will take this input and forward this to the LLM, which is the brain of the agent. The LLM will likely realize that it requires tools to respond, and

4. The agentic approach

begins by identifying which tool to call, in this case, a tool to list the collections in the database to find the ones required to answer the question. The LLM also identifies the arguments for the tool call.

5. The agentic approach

The agent also has logic to actually execute the tool call. The tool call is executed using the arguments generated by the LLM.

6. The agentic approach

The outcome of the tool execution is returned to the LLM, and next,

7. The agentic approach

it may call another tool to load the schema of the collection it decides to query, so it knows the fields to use and their types. Now for the querying!

8. The agentic approach

The agent will write a query given the task and schemas and use another tool to validate it for correctness.

9. The agentic approach

Finally, the agent can call one final tool to execute the query on the database and retrieve the results. In the final step, the LLM, now aware of the query result, can respond to the user.

10. The agentic approach

One important mechanism for orchestrating this is the storing of messages produced by the user, agent, and tools. For example, the agent needs to know what collections were listed to load their schemas.

11. The agentic approach

This is managed by a state, which essentially logs the messages and actions in the workflow.

12. Building AI agents

There are lots of frameworks and libraries out there for building and orchestrating AI agents, but we'll be using LangGraph. In LangGraph, agentic workflows are defined as graphs, consisting of nodes with a shared state, and edges. Nodes are components like an agent or a set of tools, and edges indicate where information flows under different conditions. The workflow starts and reaches the agent node, which reasons about whether a tool call is required to complete the task. If it is, it directs to the tools node which can trigger actions; if not, it can provide a "standard LLM response" and end the workflow. The agent can choose to call the same or different tools repeatedly until the task is completed.

13. Let's practice!

Time to explore and build an agentic text-to-query agent!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.