liveBook · Manning

AI agent

As explained in Data Analysis with LLMs

An agent is a software pattern in which a language model dynamically decomposes a task, selects actions or tools, and plans subsequent steps from their results.

An AI agent uses a language model to choose what to do next instead of following a fixed sequence of processing steps. This is useful when a data-analysis question might require a database query, information from text, or several tools used in an order that cannot be predicted beforehand. section 1.6 section 10.3

Give the agent a task and a set of tools. Each tool offers a function and a natural-language description that helps the model decide when to use it. The model selects a tool and its inputs, receives the result, and uses that result to decide whether to take another step or give a final answer. section 10.3

Figure 10.2 Using language models as agents. Given a prompt describing the task and available tools, the language model decides on termination and tool invocations. Results of tool invocations are added to the prompt used for the next iteration.
figure

Follow how a task and available tools lead to a tool call, then how its result feeds the next decision. section 10.3

For a database-and-web agent, the available tools can include:

  • Database tools to list tables, inspect a table’s schema, check an SQL query, and run it. section 10.4.1
  • A web-search tool to retrieve information that relates to a database result. section 10.4.1

Find a game, then look it up

Ask, “What was the most sold game in 2016, and how is it played?” The agent lists database tables, inspects the games table, checks and runs an SQL query to find the title, then can use that title to search the web for how the game is played. Its next action depends on what earlier tools return. section 10.4 section 10.4.4

To build this example with LangChain, create a ChatOpenAI model and an SQLDatabase for an SQLite file, load the SerpAPI search tool, and pass them to create_sql_agent as the model, database, and extra tools. Call agent.invoke({'input': task}) with the question. Web search requires a SerpAPI access key; verbose output shows the tool calls. The result is an answer, or a failure message if the agent cannot find one. section 10.4.2 section 10.4.3 section 10.4.4

Tools need not be limited to database access and search. A currency-conversion tool, for example, lets an agent query game revenue in US dollars and then convert the result to yen. section 10.5.2 section 10.8

How Data Analysis with LLMs covers it

How deep it goes
Explains the tool-selection loop and walks through LangChain setup and tool traces, without examining model internals.
Examples
Traces a video-game sales query followed by web search, and a revenue query followed by currency conversion.
What it assumes
Introduces the agent-and-tools loop, then uses Python, an SQLite database file, and a SerpAPI access key for implementation.
What you can do after
Apply it via LangChain to configure and invoke a database-and-web agent for a user question.
How it connects
Connects language models, tool calling, SQL queries, relational databases, web search, and custom Python functions.

Also explained in

Compare how 33 books explain AI agent →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.