Get startedGet started for free

Chains and agents

1. Chains and agents

We are ready to make more advanced applications using chains and agents.

2. LLM lifecyle: Chains and agents

While prompts are central to any application, chains and agents provide its flow and structure.

3. From prompts to applications

Let's go back to our calorie prediction template. It requires examples, and an input which is the dish description. But how do we find examples? Suppose we have a database of dishes. We need to find similar dishes based on the description. Also, the application's output should be converted to a number for calorie calculation. To achieve this, we'll go through a few steps: receiving input, searching examples, prompt creation, output retrieval, and output parsing. We can use chains or agents to build this functionality. Let's start with a chain.

4. A chain using our template

A chain, also known as a pipeline or a flow, consists of connected steps that take inputs and produce outputs. For our example, we start with a dish description. First, we find similar dishes in the database. Next, we combine the dish description and examples with the prompt template. This prompt is then given to the LLM. From the output, we extract a number. This process demonstrates a chain in action.

5. The need for chains

Chains are crucial because they enable us to: Develop sophisticated applications that interface with our own systems. Establish a modular design, enhancing scalability and operational efficiency as our system grows. Unlock endless possibilities for customization. But what if we need even more flexibility?

6. Agents

Say our dish calorie prediction isn't giving optimal results, likely due to insufficient details like ingredients and quantities. Suppose we have the option to look up more information about a dish. We now have two actions: searching for extra examples, or retrieving additional dish information. This is where agents shine. LLM-powered agents follow a design where actions, also known as tools, are available, and the LLM decides which action to take. An action functions as a standalone chain that generates output. Unlike deterministic chains, agents are adaptive. They can help us in scenarios with many actions, where the optimal sequence of steps is unknown, especially when we're uncertain about the inputs we'll receive.

7. Agents

We're now faced with two actions: Fetching more dish-information.

8. Agents

Or finding more related dishes.

9. Agents

To decide, we give the LLM a set of actions and ask it to choose the best one. So, for our example, we use an agent. Notice the backward arrows, indicating that these actions provide additional information. This also means each action can happen any number of times, and we don't know in advance how often. This complexity makes agents very powerful.

10. The difference between chains and agents

Deciding between an agent architecture and a simpler chain is vital, as agents can be unpredictable. Let's understand their differences. Chains are deterministic, while agents adaptively choose actions. Chains are simpler, while agents exhibit more complex behavior due to their adaptability. Chains are less flexible, while agents can adjust to diverse inputs. Chains are less risky due to predictability, whereas agents might execute unpredictable actions, resulting in unpredictable outcomes.

11. The development cycle

Let's return to the development cycle.

12. The development cycle

Chains and agents enrich our application by enabling intricate sequencing and transformations of inputs and outputs, thereby enhancing our functionality.

13. The development cycle

The act of creating chains and agents is called chain and agent development.

14. Let's practice!

Let's review differences between chains and agents.