chapter nine

9 Programming agents

 

This chapter covers

  • Introducing agents
  • Creating simple agents using smolagents
  • Creating enterprise-grade agents using LangChain
  • Creating enterprise-grade agents using LangGraph

Up to this point, you’ve worked with Hugging Face transformers to tackle a variety of tasks, ranging from natural language processing (NLP) to image analysis and computer vision. Each of these tasks typically involves a specific, specialized model—­­­a translation model for converting text between languages, for example, or an image captioning model to generate textual descriptions of images.

Although using specialized models works well for clearly defined tasks, it becomes increasingly difficult to manage workflows when the tasks are ambiguous or multistep or when they require dynamic decision-making. This is where agents come into play. Agents use large language models (LLMs) not only to perform tasks but also to reason, plan, and delegate, breaking complex problems into smaller subtasks and calling appropriate tools or models to complete them.

In this chapter, you’ll explore the concept of agents and learn how to build an agent yourself. In particular, the chapter focuses on constructing agents using two practical and widely applicable frameworks:

9.1 What are agents?

9.2 Developing agents using smolagents

9.2.1 Using built-in tools: DuckDuckGoSearchTool

9.2.2 Using built-in tools: PythonInterpreterTool

9.2.3 Writing your own custom tools

9.3 Developing agents with LangChain

9.3.1 Using the built-in Tool class

9.3.2 Using custom tools

9.4 Developing agents using LangGraph

9.4.1 What is LangGraph?

9.4.2 LangGraph agent basics

9.4.3 Using LangGraph with tools

9.4.4 Using LangGraph with a custom tool

9.4.5 Using LangGraph with memory

Summary