From 26a100bdcecf6fc3ea163e731f717e27ec3eedf7 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Tue, 25 Feb 2025 10:40:07 +0100 Subject: [PATCH 01/46] Add Agentic RAG unit files for gala planning scenario --- units/en/unit3/agentic-rag/agentic-rag.mdx | 34 ++++++++++++++++++++++ units/en/unit3/agentic-rag/gala.ipynb | 25 ++++++++++++++++ units/en/unit3/agentic-rag/gala.mdx | 31 ++++++++++++++++++++ units/en/unit3/agentic-rag/web-tool.mdx | 0 4 files changed, 90 insertions(+) create mode 100644 units/en/unit3/agentic-rag/agentic-rag.mdx create mode 100644 units/en/unit3/agentic-rag/gala.ipynb create mode 100644 units/en/unit3/agentic-rag/gala.mdx create mode 100644 units/en/unit3/agentic-rag/web-tool.mdx diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx new file mode 100644 index 0000000..5d52377 --- /dev/null +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -0,0 +1,34 @@ +# Agentic Retrieval Augmented Generation (RAG) + +In this unit, we'll be taking a look at how we can use Agentic RAG to help Alfred, your friendly neighborhood agent, prepare for the amazing gala. + +We know we've already discussed Retrieval Augmented Generation (RAG) and agentic RAG in the previous unit, so feel free to skip ahead if you're already familiar with the concepts. + +LLMs are trained on enormous bodies of data to learn general knowledge. +However, the world knowledge model of LLMs may not always be relevant and up-to-date data. +**RAG solves this problem by finding and retrieving relevant information from your data and forward that to the LLM.** + +![RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/rag.png) + +Now, think about how Alfred works: + +1. We've ask Alfred to help plan a gala +2. Alfred needs to find the latest news and weather information +3. Alfred needs to strcucture and search the guest information + +Just as Alfred needs to search through your household information to be helpful, any agent needs a way to find and understand relevant data. +As we've seen in the previous unit, the `QueryEngine` in LlamaIndex provides exactly this capability. +Even better, it can be converted as an agentic tool that can be used by an agent, opening up the gateway to agentic RAG. + +**Agentic RAG is a powerful way to use agents to answer questions about your data.** We can pass various tools to Alfred to help him answer questions. +However, instead of answering the question on top of documents automatically, Alfred can decide to use any other tool or flow to answer the question. + +![Agentic RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/agentic-rag.png) + +Let's start creating the tools for our agentic RAG workflow! + + + + + + diff --git a/units/en/unit3/agentic-rag/gala.ipynb b/units/en/unit3/agentic-rag/gala.ipynb new file mode 100644 index 0000000..554e7de --- /dev/null +++ b/units/en/unit3/agentic-rag/gala.ipynb @@ -0,0 +1,25 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Agentic RAG: A gala to remember\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx new file mode 100644 index 0000000..135a164 --- /dev/null +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -0,0 +1,31 @@ +# A gala to remember + +And then, it was time to get our hands dirty with an actual use case. Let's set the stage! + +**You decided to host the most extravagant and opulent party of the century.** This means, lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. + +Alfred, your friendly neighborhood agent, is getting ready to watch over your all of the needs for this party and **Alfred is going to manage everything himself**. In order to do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! + +Not only that, but he also needs to make sure that the party is going to be a success, so **he needs to be able to answer any questions about the party during the party**, while also being able to handle any unexpected situations that may arise. + +He can't do this alone, so we need to make sure that Alfred has access to all of the information and tools he needs. + +First, let's give him a list of hard requirements for the gala. + +## The gala requirements + +A properly educated person in the age of the **renaissance** needs to have three main traits. +He or she needed to profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a gala that is truly unforgettable. + +According to etiquette, a good host also needs to be aware of all actualities in the news, as well as the information of any guests that might show up at a hosted party, including their allergies, and preferences. +To avoid any confliect, there are some things that are just not acceptable at a gala because we want it to be an inclusive and open party, such as **politics** and **religion**. + +Lastly, we need to make sure that we've got some general knowledge about the weather to ensure we can continuously find a real-time update to ensure perfect timing to launch the fireworks and end the gala with a bang! 🎆 + +As you can see, Alfred needs a lot of information to be able to host the gala. +Luckily, we can help and prepare Alfred by giving him some Retrieval Augmented Generation (RAG) training! + + + + + diff --git a/units/en/unit3/agentic-rag/web-tool.mdx b/units/en/unit3/agentic-rag/web-tool.mdx new file mode 100644 index 0000000..e69de29 From 5d433c9d06cc8f0bc881bd95b1280f38e3b0d1c5 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Tue, 25 Feb 2025 13:21:59 +0100 Subject: [PATCH 02/46] Update toctree and refine Agentic RAG unit files --- units/en/_toctree.yml | 6 ++++++ units/en/unit3/agentic-rag/gala.ipynb | 25 ------------------------- units/en/unit3/agentic-rag/gala.mdx | 2 +- units/en/unit3/agentic-rag/web-tool.mdx | 0 4 files changed, 7 insertions(+), 26 deletions(-) delete mode 100644 units/en/unit3/agentic-rag/gala.ipynb delete mode 100644 units/en/unit3/agentic-rag/web-tool.mdx diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index 2e8a370..79a4620 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -56,3 +56,9 @@ sections: - local: communication/next-units title: Next Units +- title: Unit 3.2 Use Cases - Agentic RAG + sections: + - local: unit3/agentic-rag/gala.mdx + title: A Gala to Remember + - local: unit3/agentic-rag/agentic-rag.mdx + title: Agentic Retrieval Augmented Generation (RAG) \ No newline at end of file diff --git a/units/en/unit3/agentic-rag/gala.ipynb b/units/en/unit3/agentic-rag/gala.ipynb deleted file mode 100644 index 554e7de..0000000 --- a/units/en/unit3/agentic-rag/gala.ipynb +++ /dev/null @@ -1,25 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Agentic RAG: A gala to remember\n", - "\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "name": "python", - "version": "3.11.11" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index 135a164..36febf7 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -1,4 +1,4 @@ -# A gala to remember +# A Gala to Remember And then, it was time to get our hands dirty with an actual use case. Let's set the stage! diff --git a/units/en/unit3/agentic-rag/web-tool.mdx b/units/en/unit3/agentic-rag/web-tool.mdx deleted file mode 100644 index e69de29..0000000 From 2c8f039356d77b31ca2ba520c855ee12a8ab2b99 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Thu, 27 Feb 2025 07:55:40 +0100 Subject: [PATCH 03/46] Add web search and weather tool documentation for Agentic RAG unit --- units/en/unit3/agentic-rag/actualities.mdx | 32 ++++++++++++++++++++++ units/en/unit3/agentic-rag/agent.mdx | 0 units/en/unit3/agentic-rag/agentic-rag.mdx | 8 +----- units/en/unit3/agentic-rag/gala.mdx | 13 ++++----- units/en/unit3/agentic-rag/invitees.mdx | 1 + units/en/unit3/agentic-rag/weather.mdx | 14 ++++++++++ 6 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 units/en/unit3/agentic-rag/actualities.mdx create mode 100644 units/en/unit3/agentic-rag/agent.mdx create mode 100644 units/en/unit3/agentic-rag/invitees.mdx create mode 100644 units/en/unit3/agentic-rag/weather.mdx diff --git a/units/en/unit3/agentic-rag/actualities.mdx b/units/en/unit3/agentic-rag/actualities.mdx new file mode 100644 index 0000000..a7abf14 --- /dev/null +++ b/units/en/unit3/agentic-rag/actualities.mdx @@ -0,0 +1,32 @@ +# Importing a Tool from the LlamaHub to Websearch Actualities + +Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. + +To do so, we need to make sure that Alfred has access to the latest news and information about the world. + +Let's start by creating a web search tool for Alfred! + +## Websearch using DuckDuckGoSearchToolSpec + +Remember the [`ToolSpecs`](https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/tools/#tool-specs) from the previous unit? +We can use them to create a web search tool for Alfred. +Generally, when working with web search, DuckDuckGo is a good choice, as it is a search engine that is open to all and does not require any authentication. +To create a web search tool for Alfred, we can use the [`DuckDuckGoSearchToolSpec`](https://llamahub.ai/l/tools/llama-index-tools-duckduckgo?from=). + +Let's create the tool spec for the web search tool. + +```python +from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec +from llama_index.agent.openai import OpenAIAgent + +tool_spec = DuckDuckGoSearchToolSpec() + +agent = OpenAIAgent.from_tools(DuckDuckGoSearchToolSpec.to_tool_list()) + +agent.chat("What are the latest developments in machine learning?") +agent.chat("What does the word 'renaissance' mean?") +``` + +We can see that the agent is able to use the tool to search the web for information. + +Great, let's continue with a a tool that can help Alfred get the weather information! diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx new file mode 100644 index 0000000..e69de29 diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx index 5d52377..c8ad762 100644 --- a/units/en/unit3/agentic-rag/agentic-rag.mdx +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -25,10 +25,4 @@ However, instead of answering the question on top of documents automatically, Al ![Agentic RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/agentic-rag.png) -Let's start creating the tools for our agentic RAG workflow! - - - - - - +Let's start **creating the web search and weather tools for our agentic RAG workflow!** diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index 36febf7..2ade2a3 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -16,16 +16,13 @@ First, let's give him a list of hard requirements for the gala. A properly educated person in the age of the **renaissance** needs to have three main traits. He or she needed to profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a gala that is truly unforgettable. +Although, to avoid any conflicts, there are some **topics, like politics and religion, that are to be avoided at a gala.** It needs to be an inclusive party without conflicts related to beliefs and ideals. -According to etiquette, a good host also needs to be aware of all actualities in the news, as well as the information of any guests that might show up at a hosted party, including their allergies, and preferences. -To avoid any confliect, there are some things that are just not acceptable at a gala because we want it to be an inclusive and open party, such as **politics** and **religion**. +According to etiquette, **a good host need to be aware of all guest information**, including their life stories and endevours. A good host also gossips and shares stories about the guests with one another. -Lastly, we need to make sure that we've got some general knowledge about the weather to ensure we can continuously find a real-time update to ensure perfect timing to launch the fireworks and end the gala with a bang! 🎆 +Lastly, we need to make sure that we've got **some general knowledge about the weather** to ensure we can continuously find a real-time update to ensure perfect timing to launch the fireworks and end the gala with a bang! 🎆 As you can see, Alfred needs a lot of information to be able to host the gala. -Luckily, we can help and prepare Alfred by giving him some Retrieval Augmented Generation (RAG) training! - - - - +Luckily, we can help and prepare Alfred by giving him some **Retrieval Augmented Generation (RAG) training!** +Let's start by creating the tools that Alfred needs to be able to host the gala! diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx new file mode 100644 index 0000000..6e2664f --- /dev/null +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -0,0 +1 @@ +# Creating a RAG Tool for Guest Stories \ No newline at end of file diff --git a/units/en/unit3/agentic-rag/weather.mdx b/units/en/unit3/agentic-rag/weather.mdx new file mode 100644 index 0000000..8ca067c --- /dev/null +++ b/units/en/unit3/agentic-rag/weather.mdx @@ -0,0 +1,14 @@ +# Creating a Custom Tool for Weather Information to Schedule the Fireworks + +Remember that we need to make sure the fireworks are not cancelled due to bad weather? + +As before, we can use a `ToolSpec` to create a tool for Alfred. +When searching for `"weather"` in LlamaHub, we get a list of `ToolSpecs` that are relevant to weather. +However, the tools that are available in the LlamaHub are not always what we need. +Both returned tools require authentication with an API key, which is not what we want so we will create a custom tool. + +Let's create a custom tool that can be used to call an external weather API and get the weather information for a given location. + +```python + +``` \ No newline at end of file From befe4c6ede3c98cc824beb05b4c32426ca434b7b Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 6 Mar 2025 20:02:43 +0100 Subject: [PATCH 04/46] Updated toctree with use case using smolagents --- units/en/_toctree.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index 79a4620..d7510f5 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -56,6 +56,12 @@ sections: - local: communication/next-units title: Next Units +- title: Unit 3.1 Use Cases - Agentic RAG using smolagents + sections: + - local: unit3/agentic-rag/gala.mdx + title: A Gala to Remember + - local: unit3/agentic-rag/agentic-rag.mdx + title: Agentic Retrieval Augmented Generation (RAG) - title: Unit 3.2 Use Cases - Agentic RAG sections: - local: unit3/agentic-rag/gala.mdx From a33e50d48a8bf39c3a06777384b4585cd60fcb30 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 10 Mar 2025 14:10:27 +0100 Subject: [PATCH 05/46] update toc with combined approach --- units/en/_toctree.yml | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index d7510f5..e16dedf 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -52,19 +52,23 @@ title: Let's Fine-Tune your model for Function-calling - local: bonus-unit1/conclusion title: Conclusion +- title: Unit 3. Use Case for Agentic RAG + sections: + - local: unit3/agentic-rag/gala.mdx + title: A Gala to Remember + - local: unit3/agentic-rag/agentic-rag.mdx + title: Agentic Retrieval Augmented Generation (RAG) + - local: unit3/agentic-rag/invitees.mdx + title: Give your agent a database + - local: unit3/agentic-rag/tools.mdx + title: Creating a Weather Tool + - local: unit3/agentic-rag/memory.mdx + title: Give your agent a memory + - local: unit3/agentic-rag/agent.mdx + title: Putting your agent to the test + - local: unit3/agentic-rag/conclusion.mdx + title: Conclusion - title: When the next steps are published? sections: - local: communication/next-units - title: Next Units -- title: Unit 3.1 Use Cases - Agentic RAG using smolagents - sections: - - local: unit3/agentic-rag/gala.mdx - title: A Gala to Remember - - local: unit3/agentic-rag/agentic-rag.mdx - title: Agentic Retrieval Augmented Generation (RAG) -- title: Unit 3.2 Use Cases - Agentic RAG - sections: - - local: unit3/agentic-rag/gala.mdx - title: A Gala to Remember - - local: unit3/agentic-rag/agentic-rag.mdx - title: Agentic Retrieval Augmented Generation (RAG) \ No newline at end of file + title: Next Units \ No newline at end of file From 49259af0cbe6fc885365a20457065ffef091f1ca Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 10 Mar 2025 14:10:42 +0100 Subject: [PATCH 06/46] update naming and titles to match toc --- units/en/unit3/agentic-rag/actualities.mdx | 26 +------------------ units/en/unit3/agentic-rag/agent.mdx | 1 + units/en/unit3/agentic-rag/conclusion.mdx | 18 +++++++++++++ units/en/unit3/agentic-rag/memory.mdx | 1 + .../agentic-rag/{weather.mdx => tools.mdx} | 20 ++++++++++++-- 5 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 units/en/unit3/agentic-rag/conclusion.mdx create mode 100644 units/en/unit3/agentic-rag/memory.mdx rename units/en/unit3/agentic-rag/{weather.mdx => tools.mdx} (51%) diff --git a/units/en/unit3/agentic-rag/actualities.mdx b/units/en/unit3/agentic-rag/actualities.mdx index a7abf14..1ec0f31 100644 --- a/units/en/unit3/agentic-rag/actualities.mdx +++ b/units/en/unit3/agentic-rag/actualities.mdx @@ -1,4 +1,4 @@ -# Importing a Tool from the LlamaHub to Websearch Actualities +# Give your agent access to the web Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. @@ -6,27 +6,3 @@ To do so, we need to make sure that Alfred has access to the latest news and inf Let's start by creating a web search tool for Alfred! -## Websearch using DuckDuckGoSearchToolSpec - -Remember the [`ToolSpecs`](https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/tools/#tool-specs) from the previous unit? -We can use them to create a web search tool for Alfred. -Generally, when working with web search, DuckDuckGo is a good choice, as it is a search engine that is open to all and does not require any authentication. -To create a web search tool for Alfred, we can use the [`DuckDuckGoSearchToolSpec`](https://llamahub.ai/l/tools/llama-index-tools-duckduckgo?from=). - -Let's create the tool spec for the web search tool. - -```python -from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec -from llama_index.agent.openai import OpenAIAgent - -tool_spec = DuckDuckGoSearchToolSpec() - -agent = OpenAIAgent.from_tools(DuckDuckGoSearchToolSpec.to_tool_list()) - -agent.chat("What are the latest developments in machine learning?") -agent.chat("What does the word 'renaissance' mean?") -``` - -We can see that the agent is able to use the tool to search the web for information. - -Great, let's continue with a a tool that can help Alfred get the weather information! diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index e69de29..d0bd9ac 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -0,0 +1 @@ +# Creating Your Gala Agent diff --git a/units/en/unit3/agentic-rag/conclusion.mdx b/units/en/unit3/agentic-rag/conclusion.mdx new file mode 100644 index 0000000..0c139d6 --- /dev/null +++ b/units/en/unit3/agentic-rag/conclusion.mdx @@ -0,0 +1,18 @@ +# Conclusion + +In this unit, we've learned how to create an agentic RAG system to help Alfred, our friendly neighborhood agent, prepare for and manage an extravagant gala. + +We've covered: +- Setting up the gala scenario and requirements +- Understanding Agentic Retrieval Augmented Generation (RAG) +- Creating a database for guest information +- Building a custom weather tool for scheduling fireworks +- Implementing web search capabilities for real-time information +- Adding memory to our agent for better conversation flow +- Testing our agent in various scenarios + +With these tools and capabilities, Alfred is now well-equipped to be the perfect host, able to answer questions about guests, provide up-to-date information, and ensure the gala runs smoothly - even managing the perfect timing for the fireworks display! + +The combination of RAG with agentic capabilities demonstrates how powerful AI assistants can become when they have access to both structured knowledge and the ability to take actions based on that knowledge. + +In the next unit, we'll explore more advanced agent capabilities and continue building on what we've learned. \ No newline at end of file diff --git a/units/en/unit3/agentic-rag/memory.mdx b/units/en/unit3/agentic-rag/memory.mdx new file mode 100644 index 0000000..b5420fa --- /dev/null +++ b/units/en/unit3/agentic-rag/memory.mdx @@ -0,0 +1 @@ +# Give your agent a memory diff --git a/units/en/unit3/agentic-rag/weather.mdx b/units/en/unit3/agentic-rag/tools.mdx similarity index 51% rename from units/en/unit3/agentic-rag/weather.mdx rename to units/en/unit3/agentic-rag/tools.mdx index 8ca067c..fb9bf91 100644 --- a/units/en/unit3/agentic-rag/weather.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -1,4 +1,20 @@ -# Creating a Custom Tool for Weather Information to Schedule the Fireworks +# Giving your agents access to the web + +In this unit, we'll be giving Alfred access to the web to help him find the latest news and information about the world. + +## Give your agent access to the web + +Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. + +To do so, we need to make sure that Alfred has access to the latest news and information about the world. + +Let's start by creating a web search tool for Alfred! + +```python + +``` + +## Creating a Custom Tool for Weather Information to Schedule the Fireworks Remember that we need to make sure the fireworks are not cancelled due to bad weather? @@ -11,4 +27,4 @@ Let's create a custom tool that can be used to call an external weather API and ```python -``` \ No newline at end of file +``` From 5e2e660f979c5f525748656fd232a8c13e2d3eae Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 11:24:25 +0100 Subject: [PATCH 07/46] update toc and intro pages --- units/en/_toctree.yml | 2 ++ units/en/unit3/agentic-rag/agentic-rag.mdx | 5 +---- units/en/unit3/agentic-rag/gala.mdx | 2 +- units/en/unit3/agentic-rag/introduction.mdx | 8 ++++++++ units/en/unit3/agentic-rag/tools.mdx | 5 ----- 5 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 units/en/unit3/agentic-rag/introduction.mdx diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index e16dedf..1bda64d 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -54,6 +54,8 @@ title: Conclusion - title: Unit 3. Use Case for Agentic RAG sections: + - local: unit3/agentic-rag/introduction + title: Introduction - local: unit3/agentic-rag/gala.mdx title: A Gala to Remember - local: unit3/agentic-rag/agentic-rag.mdx diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx index c8ad762..d55f877 100644 --- a/units/en/unit3/agentic-rag/agentic-rag.mdx +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -1,6 +1,6 @@ # Agentic Retrieval Augmented Generation (RAG) -In this unit, we'll be taking a look at how we can use Agentic RAG to help Alfred, your friendly neighborhood agent, prepare for the amazing gala. +In this unit, we'll be taking a look at how we can use Agentic RAG to help Alfred prepare for the amazing gala. We know we've already discussed Retrieval Augmented Generation (RAG) and agentic RAG in the previous unit, so feel free to skip ahead if you're already familiar with the concepts. @@ -17,9 +17,6 @@ Now, think about how Alfred works: 3. Alfred needs to strcucture and search the guest information Just as Alfred needs to search through your household information to be helpful, any agent needs a way to find and understand relevant data. -As we've seen in the previous unit, the `QueryEngine` in LlamaIndex provides exactly this capability. -Even better, it can be converted as an agentic tool that can be used by an agent, opening up the gateway to agentic RAG. - **Agentic RAG is a powerful way to use agents to answer questions about your data.** We can pass various tools to Alfred to help him answer questions. However, instead of answering the question on top of documents automatically, Alfred can decide to use any other tool or flow to answer the question. diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index 2ade2a3..fb106c1 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -1,6 +1,6 @@ # A Gala to Remember -And then, it was time to get our hands dirty with an actual use case. Let's set the stage! +Now, it's time to get our hands dirty with an actual use case. Let's set the stage! **You decided to host the most extravagant and opulent party of the century.** This means, lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. diff --git a/units/en/unit3/agentic-rag/introduction.mdx b/units/en/unit3/agentic-rag/introduction.mdx new file mode 100644 index 0000000..14d71bd --- /dev/null +++ b/units/en/unit3/agentic-rag/introduction.mdx @@ -0,0 +1,8 @@ +# Use Case for Agentic RAG + +In this unit, we will be using the Agentic RAG to create a tool that can be used to answer questions about the guests at the gala. + +This is a 'real-world' use case for Agentic RAG, that you could use in your own projects or workplaces. + +Of course, we will frame our project around Alfred, our friendly agent, who is hosting the gala. + diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index fb9bf91..827f712 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -18,11 +18,6 @@ Let's start by creating a web search tool for Alfred! Remember that we need to make sure the fireworks are not cancelled due to bad weather? -As before, we can use a `ToolSpec` to create a tool for Alfred. -When searching for `"weather"` in LlamaHub, we get a list of `ToolSpecs` that are relevant to weather. -However, the tools that are available in the LlamaHub are not always what we need. -Both returned tools require authentication with an API key, which is not what we want so we will create a custom tool. - Let's create a custom tool that can be used to call an external weather API and get the weather information for a given location. ```python From 54126329a9ec252689a966327472ae0c3749e5c8 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 11:24:34 +0100 Subject: [PATCH 08/46] add guet retriever page --- units/en/unit3/agentic-rag/invitees.mdx | 174 +++++++++++++++++++++++- 1 file changed, 173 insertions(+), 1 deletion(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index 6e2664f..45ff0f6 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -1 +1,173 @@ -# Creating a RAG Tool for Guest Stories \ No newline at end of file +# Creating a RAG Tool for Guest Stories + +Alfred, your trusted agent, is preparing for the most extravagant gala of the century. To ensure the event runs smoothly, Alfred needs quick access to up-to-date information about each guest. Let's help Alfred by creating a custom Retrieval-Augmented Generation (RAG) tool, powered by our a custom dataset. + +## Why RAG for a Gala? + +Imagine Alfred mingling among the guests, needing to recall specific details about each person at a moment's notice. A traditional LLM might struggle with this task because: + +1. The guest list is specific to your event and not in the model's training data +2. Guest information may change or update frequently +3. Alfred needs to retrieve precise details like email addresses + +This is where Retrieval Augmented Generation (RAG) shines! By combining a retrieval system with an LLM, Alfred can access accurate, up-to-date information about your guests on demand. + +## Dataset Overview + +Our dataset (`agents-course/unit3-invitees`) contains the following fields for each guest: + +- **Name**: Guest's full name +- **Relation**: How the guest is related to the host +- **Description**: A brief biography or interesting facts about the guest +- **Email Address**: Contact information for sending invitations or follow-ups + +Below is a preview of the dataset: + + + + +In a real-world scenario, this dataset could be expanded to include dietary preferences, gift interests, conversation topics to avoid, and other helpful details for a host. + + +## Building the Guestbook Tool + +We'll create a custom tool that Alfred can use to quickly retrieve guest information during the gala. Let's break this down into three manageable steps: + +1. Load and prepare the dataset +2. Create the Retriever Tool +3. Integrate the Tool with Alfred + +### Step 1: Load and Prepare the Dataset + +First, we need to transform our raw guest data into a format that's optimized for retrieval. + +We will use the Hugging Face `datasets` library to load the dataset and convert it into a list of `Document` objects from the `langchain.docstore.document` module. + +```python +import datasets +from langchain.docstore.document import Document + +# Load the dataset +guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") + +# Convert dataset entries into Document objects +docs = [ + Document( + page_content=f"Name: {guest['name']}\nRelation: {guest['relation']}\nDescription: {guest['description']}\nEmail: {guest['email']}", + metadata={"name": guest["name"]} + ) + for guest in guest_dataset +] + +``` +In the code above, we: +- Load the dataset +- Convert each guest entry into a `Document` object with formatted content +- Store the `Document` objects in a list + +### Step 2: Create the Retriever Tool + +Now, let's create a custom tool that Alfred can use to search through our guest information. + +We will use the `BM25Retriever` from the `langchain_community.retrievers` module to create a retriever tool. + + +The `BM25Retriever` is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from [sentence-transformers](https://www.sbert.net/). + + +```python +from smolagents import Tool +from langchain_community.retrievers import BM25Retriever + +class GuestInfoRetrieverTool(Tool): + name = "guest_info_retriever" + description = "Retrieves detailed information about gala guests based on their name or relation." + inputs = { + "query": { + "type": "string", + "description": "The name or relation of the guest you want information about." + } + } + output_type = "string" + + def __init__(self, docs): + self.retriever = BM25Retriever.from_documents(docs) + + def forward(self, query: str): + results = self.retriever.get_relevant_documents(query) + if results: + return "\n\n".join([doc.page_content for doc in results[:3]]) + else: + return "No matching guest information found." + +# Initialize the tool +guest_info_tool = GuestInfoRetrieverTool(docs_processed) +``` + +Let's understand this tool. +- The `name` and `description` help the agent understand when and how to use this tool +- The `inputs` define what parameters the tool expects (in this case, a search query) +- We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings +- The `forward` method processes the query and returns the most relevant guest information + +### Step 3: Integrate the Tool with Alfred + +Finally, let's bring everything together by creating our agent and equipping it with our custom tool: + +```python +from smolagents import CodeAgent, HfApiModel + +# Initialize the Hugging Face model +model = HfApiModel() + +# Create Alfred, our gala agent, with the guest info tool +alfred = CodeAgent(tools=[guest_info_tool], model=model) + +# Example query Alfred might receive during the gala +response = alfred.run("Tell me about our guest named 'Lady Ada Lovelace'.") + +print("🎩 Alfred's Response:") +print(response) +``` + +What's happening in this final step: +- We initialize a Hugging Face model using the `HfApiModel` class +- We create our agent (Alfred) as a `CodeAgent`, which can execute Python code to solve problems +- We ask Alfred to retrieve information about a guest named "Lady Ada Lovelace" + +## Example Interaction + +During the gala, a conversation might flow like this: + +**You:** "Alfred, who is that gentleman talking to the ambassador?" + +**Alfred:** *quickly searches the guest database* "That's Dr. Nikola Tesla, sir. He's an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk." + +```json +{ + "name": "Dr. Nikola Tesla", + "relation": "old friend from university days", + "description": "Dr. Nikola Tesla is an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk.", + "email": "nikola.tesla@gmail.com" +} +``` + +## Taking It Further + +Now that Alfred can retrieve guest information, consider how you might enhance this system: + +1. **Improve the retriever** to use a more sophisticated algorithm like [sentence-transformers](https://www.sbert.net/) +2. **Implement a conversation memory** so Alfred remembers previous interactions +3. **Combine with web search** to get the latest information on unfamiliar guests + +Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring your gala is remembered as the most sophisticated and delightful event of the century! + + +Try extending the `GuestInfoRetrieverTool` to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? + + From ea3d6bc2d372b5b6545a5266377c48cf542c317f Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:12:24 +0100 Subject: [PATCH 09/46] add tools for weather, search, and hub --- units/en/unit3/agentic-rag/tools.mdx | 109 ++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-) diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 827f712..28ba706 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -4,14 +4,21 @@ In this unit, we'll be giving Alfred access to the web to help him find the late ## Give your agent access to the web -Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. +Remember that we want Alfred to establish his presence as a true renaissance host, with a deep knowledge of the world. To do so, we need to make sure that Alfred has access to the latest news and information about the world. Let's start by creating a web search tool for Alfred! ```python +from smolagents import DuckDuckGoSearchTool +# Initialize the DuckDuckGo search tool +search_tool = DuckDuckGoSearchTool() + +# Example usage +results = search_tool("Who's the current president of France?") +print(results) ``` ## Creating a Custom Tool for Weather Information to Schedule the Fireworks @@ -20,6 +27,104 @@ Remember that we need to make sure the fireworks are not cancelled due to bad we Let's create a custom tool that can be used to call an external weather API and get the weather information for a given location. -```python + +For the sake of simplicity, we're using a dummy weather API for this example. If you want to use a real weather API, you could implement a weather tool that uses the OpenWeatherMap API, like in [Unit 1](../unit1/tutorial.mdx). + +```python +from smolagents import Tool +import random + +class WeatherInfoTool(Tool): + name = "weather_info" + description = "Fetches dummy weather information for a given location." + inputs = { + "location": { + "type": "string", + "description": "The location to get weather information for." + } + } + output_type = "string" + + def forward(self, location: str): + # Dummy weather data + weather_conditions = [ + {"condition": "Rainy", "temp_c": 15}, + {"condition": "Clear", "temp_c": 25}, + {"condition": "Windy", "temp_c": 20} + ] + # Randomly select a weather condition + data = random.choice(weather_conditions) + return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" + +# Initialize the tool +weather_info_tool = WeatherInfoTool() ``` + +## Creating a Hub Stats Tool for Influential AI Builders + +Alfred can impress influential AI builders by discussing their most popular models. We'll create a tool to fetch model statistics from the Hugging Face Hub based on a username. + +```python +from smolagents import Tool +from huggingface_hub import list_models + +class HubStatsTool(Tool): + name = "hub_stats" + description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub." + inputs = { + "author": { + "type": "string", + "description": "The username of the model author/organization to find models from." + } + } + output_type = "string" + + def forward(self, author: str): + try: + # List models from the specified author, sorted by downloads + models = list(list_models(author=author, sort="downloads", direction=-1, limit=1)) + + if models: + model = models[0] + return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads." + else: + return f"No models found for author {author}." + except Exception as e: + return f"Error fetching models for {author}: {str(e)}" + +# Initialize the tool +hub_stats_tool = HubStatsTool() + +# Example usage +print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook +``` + +With the Hub Stats Tool, Alfred can now impress influential AI builders by discussing their most popular models. + +## Integrating Tools with Alfred + +Now that we have all the tools, let's integrate them into Alfred's agent: + +```python +from smolagents import CodeAgent, HfApiModel + +# Initialize the Hugging Face model +model = HfApiModel() + +# Create Alfred with all the tools +alfred = CodeAgent( + tools=[search_tool, weather_info_tool, hub_stats_tool], + model=model +) + +# Example query Alfred might receive during the gala +response = alfred.run("Who is Meta AI and what's their most popular model?") + +print("🎩 Alfred's Response:") +print(response) +``` + +## Conclusion + +By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. \ No newline at end of file From c51122a0fb129e3c2a8b60f9c51cd757fc584f30 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:25:08 +0100 Subject: [PATCH 10/46] remove page on memory --- units/en/_toctree.yml | 2 -- units/en/unit3/agentic-rag/memory.mdx | 1 - 2 files changed, 3 deletions(-) delete mode 100644 units/en/unit3/agentic-rag/memory.mdx diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index 1bda64d..5b5c3fb 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -64,8 +64,6 @@ title: Give your agent a database - local: unit3/agentic-rag/tools.mdx title: Creating a Weather Tool - - local: unit3/agentic-rag/memory.mdx - title: Give your agent a memory - local: unit3/agentic-rag/agent.mdx title: Putting your agent to the test - local: unit3/agentic-rag/conclusion.mdx diff --git a/units/en/unit3/agentic-rag/memory.mdx b/units/en/unit3/agentic-rag/memory.mdx deleted file mode 100644 index b5420fa..0000000 --- a/units/en/unit3/agentic-rag/memory.mdx +++ /dev/null @@ -1 +0,0 @@ -# Give your agent a memory From ee8136ea40ffa7e02b4e1ebd54594acb61890e7e Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:25:30 +0100 Subject: [PATCH 11/46] add instruction to persist tools to files --- units/en/unit3/agentic-rag/invitees.mdx | 2 ++ units/en/unit3/agentic-rag/tools.mdx | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index 45ff0f6..53b6039 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -169,5 +169,7 @@ Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring yo Try extending the `GuestInfoRetrieverTool` to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? + +When you're done, implent your guest retriever tool in the `retriever.py` file. diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 28ba706..714b083 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -127,4 +127,10 @@ print(response) ## Conclusion -By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. \ No newline at end of file +By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. + + +Try implementing a tool that can be used to get the latest news about a specific topic. + +When you're done, implement your tool in the `tools.py` file. + \ No newline at end of file From a0cf595291310ab3e85e821e58f5e1848e2638b2 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:25:50 +0100 Subject: [PATCH 12/46] add page on combined agent --- units/en/unit3/agentic-rag/agent.mdx | 185 +++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index d0bd9ac..d8c8d99 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -1 +1,186 @@ # Creating Your Gala Agent + +Now that we've built all the necessary components for Alfred, it's time to bring everything together into a complete agent that can help host our extravagant gala. + +In this section, we'll combine the guest information retrieval, web search, weather information, and Hub stats tools into a single powerful agent. + +## Assembling Alfred: The Complete Agent + +Instead of reimplementing all the tools we've created in previous sections, we'll import them from their respective modules which we saved in the `tools.py` and `retriever.py` files. + + +If you haven't implemented the tools yet, go back to the [tools](./tools.mdx) and [retriever](./invitees.mdx) sections implement them, and add them to the `tools.py` and `retriever.py` files. + + +Let's import the necessary libraries and tools from the previous sections: + +```python +# Import necessary libraries +import random +from smolagents import CodeAgent, HfApiModel + +# Import our custom tools from their modules +from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool +from retriever import GuestInfoRetrieverTool, load_guest_dataset +``` + +Now, let's combine all these tools into a single agent: + +```python +# Initialize the Hugging Face model +model = HfApiModel() + +# Initialize the web search tool +search_tool = DuckDuckGoSearchTool() + +# Initialize the weather tool +weather_info_tool = WeatherInfoTool() + +# Initialize the Hub stats tool +hub_stats_tool = HubStatsTool() + +# Load the guest dataset and initialize the guest info tool +guest_info_tool = load_guest_dataset() + +# Create Alfred with all the tools +alfred = CodeAgent( + tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool], + model=model, + add_base_tools=True, # Add any additional base tools + planning_interval=3 # Enable planning every 3 steps +) +``` + +Your agent is now ready to use! + +## Using Alfred: End-to-End Examples + +Now that Alfred is fully equipped with all the necessary tools, let's see how he can help with various tasks during the gala. + +### Example 1: Finding Guest Information + +Let's see how Alfred can help us with our guest information. + +```python +query = "Tell me about Lady Ada Lovelace. What's her background and how is she related to me?" +response = alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: +``` +🎩 Alfred's Response: +Based on the information I retrieved, Lady Ada Lovelace is an esteemed mathematician and friend. She is renowned for her pioneering work in mathematics and computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email address is ada.lovelace@example.com. +``` + +### Example 2: Checking the Weather for Fireworks + +Let's see how Alfred can help us with the weather. + +```python +query = "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?" +response = alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output (will vary due to randomness): +``` +🎩 Alfred's Response: +I've checked the weather in Paris for you. Currently, it's Clear with a temperature of 25°C. These conditions are perfect for the fireworks display tonight. The clear skies will provide excellent visibility for the spectacular show, and the comfortable temperature will ensure guests can enjoy the outdoor event without discomfort. +``` + +### Example 3: Impressing AI Researchers + +Let's see how Alfred can help us impress AI researchers. + +```python +query = "One of our guests is from Anthropic. What can you tell me about their most popular model?" +response = alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: +``` +🎩 Alfred's Response: +I've looked up information about Anthropic's models. The most downloaded model by Anthropic is claude-3-haiku-20240307, which has received over 1,200,000 downloads. This is their lightweight, fastest, and most efficient model in the Claude 3 family. It would be a great conversation starter to mention this to your guest from Anthropic, perhaps asking them about their latest research or the capabilities of their Claude models. +``` + +### Example 4: Combining Multiple Tools + +Let's see how Alfred can help us prepare for a conversation with Dr. Nikola Tesla. + +```python +query = "I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?" +response = alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: +``` +🎩 Alfred's Response: +I've gathered information to help you prepare for your conversation with Dr. Nikola Tesla. + +Guest Information: +Name: Dr. Nikola Tesla +Relation: old friend from university days +Description: Dr. Nikola Tesla is an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk. +Email: nikola.tesla@gmail.com + +Recent Advancements in Wireless Energy: +Based on my web search, here are some recent developments in wireless energy transmission: +1. Researchers have made progress in long-range wireless power transmission using focused electromagnetic waves +2. Several companies are developing resonant inductive coupling technologies for consumer electronics +3. There are new applications in electric vehicle charging without physical connections + +Conversation Starters: +1. "I'd love to hear about your new patent on wireless energy transmission. How does it compare to your original concepts from our university days?" +2. "Have you seen the recent developments in resonant inductive coupling for consumer electronics? What do you think of their approach?" +3. "How are your pigeons doing? I remember your fascination with them." + +This should give you plenty to discuss with Dr. Tesla while demonstrating your knowledge of his interests and recent developments in his field. +``` + +## Advanced Features: Conversation Memory + +To make Alfred even more helpful during the gala, we can enable conversation memory so he remembers previous interactions: + +```python +# Create Alfred with conversation memory +alfred_with_memory = CodeAgent( + tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool], + model=model, + add_base_tools=True, + planning_interval=3, + memory=True # Enable conversation memory +) + +# First interaction +response1 = alfred_with_memory.run("Tell me about Lady Ada Lovelace.") +print("🎩 Alfred's First Response:") +print(response1) + +# Second interaction (referencing the first) +response2 = alfred_with_memory.run("What projects is she currently working on?") +print("🎩 Alfred's Second Response:") +print(response2) +``` + +## Conclusion + +Congratulations! You've successfully built Alfred, a sophisticated agent equipped with multiple tools to help host the most extravagant gala of the century. Alfred can now: + +1. Retrieve detailed information about guests +2. Check weather conditions for planning outdoor activities +3. Provide insights about influential AI builders and their models +4. Search the web for the latest information +5. Maintain conversation context with memory + +With these capabilities, Alfred is ready to ensure your gala is a resounding success, impressing guests with personalized attention and up-to-date information. \ No newline at end of file From c854a6ccb3474051509768ae0eaf2f376dd42575 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:34:09 +0100 Subject: [PATCH 13/46] update conclusion based on changes --- units/en/unit3/agentic-rag/conclusion.mdx | 24 ++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/units/en/unit3/agentic-rag/conclusion.mdx b/units/en/unit3/agentic-rag/conclusion.mdx index 0c139d6..278f105 100644 --- a/units/en/unit3/agentic-rag/conclusion.mdx +++ b/units/en/unit3/agentic-rag/conclusion.mdx @@ -2,17 +2,19 @@ In this unit, we've learned how to create an agentic RAG system to help Alfred, our friendly neighborhood agent, prepare for and manage an extravagant gala. -We've covered: -- Setting up the gala scenario and requirements -- Understanding Agentic Retrieval Augmented Generation (RAG) -- Creating a database for guest information -- Building a custom weather tool for scheduling fireworks -- Implementing web search capabilities for real-time information -- Adding memory to our agent for better conversation flow -- Testing our agent in various scenarios +The combination of RAG with agentic capabilities demonstrates how powerful AI assistants can become when they have: +- Access to structured knowledge (guest information) +- Ability to retrieve real-time information (web search) +- Domain-specific tools (weather information, Hub stats) +- Memory of past interactions -With these tools and capabilities, Alfred is now well-equipped to be the perfect host, able to answer questions about guests, provide up-to-date information, and ensure the gala runs smoothly - even managing the perfect timing for the fireworks display! +With these capabilities, Alfred is now well-equipped to be the perfect host, able to answer questions about guests, provide up-to-date information, and ensure the gala runs smoothly - even managing the perfect timing for the fireworks display! -The combination of RAG with agentic capabilities demonstrates how powerful AI assistants can become when they have access to both structured knowledge and the ability to take actions based on that knowledge. + +Now that you've built a complete agent, you might want to explore: -In the next unit, we'll explore more advanced agent capabilities and continue building on what we've learned. \ No newline at end of file +- Creating more specialized tools for your own use cases +- Implementing more sophisticated RAG systems with embeddings +- Building multi-agent systems where agents can collaborate +- Deploying your agent as a service that others can interact with + From ae0a192c799e59595f65f0354ade6ed1e736702d Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:41:32 +0100 Subject: [PATCH 14/46] fixes from read through review --- units/en/unit3/agentic-rag/agentic-rag.mdx | 4 ++-- units/en/unit3/agentic-rag/gala.mdx | 6 +++--- units/en/unit3/agentic-rag/invitees.mdx | 4 ++-- units/en/unit3/agentic-rag/tools.mdx | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx index d55f877..53f2cfc 100644 --- a/units/en/unit3/agentic-rag/agentic-rag.mdx +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -12,9 +12,9 @@ However, the world knowledge model of LLMs may not always be relevant and up-to- Now, think about how Alfred works: -1. We've ask Alfred to help plan a gala +1. We've asked Alfred to help plan a gala 2. Alfred needs to find the latest news and weather information -3. Alfred needs to strcucture and search the guest information +3. Alfred needs to structure and search the guest information Just as Alfred needs to search through your household information to be helpful, any agent needs a way to find and understand relevant data. **Agentic RAG is a powerful way to use agents to answer questions about your data.** We can pass various tools to Alfred to help him answer questions. diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index fb106c1..1644f4a 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -4,7 +4,7 @@ Now, it's time to get our hands dirty with an actual use case. Let's set the sta **You decided to host the most extravagant and opulent party of the century.** This means, lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. -Alfred, your friendly neighborhood agent, is getting ready to watch over your all of the needs for this party and **Alfred is going to manage everything himself**. In order to do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! +Alfred, your friendly neighborhood agent, is getting ready to watch over all of your needs for this party and **Alfred is going to manage everything himself**. In order to do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! Not only that, but he also needs to make sure that the party is going to be a success, so **he needs to be able to answer any questions about the party during the party**, while also being able to handle any unexpected situations that may arise. @@ -15,10 +15,10 @@ First, let's give him a list of hard requirements for the gala. ## The gala requirements A properly educated person in the age of the **renaissance** needs to have three main traits. -He or she needed to profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a gala that is truly unforgettable. +He or she needed to be profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a gala that is truly unforgettable. Although, to avoid any conflicts, there are some **topics, like politics and religion, that are to be avoided at a gala.** It needs to be an inclusive party without conflicts related to beliefs and ideals. -According to etiquette, **a good host need to be aware of all guest information**, including their life stories and endevours. A good host also gossips and shares stories about the guests with one another. +According to etiquette, **a good host needs to be aware of all guest information**, including their life stories and endeavors. A good host also gossips and shares stories about the guests with one another. Lastly, we need to make sure that we've got **some general knowledge about the weather** to ensure we can continuously find a real-time update to ensure perfect timing to launch the fireworks and end the gala with a bang! 🎆 diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index 53b6039..a7a74f5 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -106,7 +106,7 @@ class GuestInfoRetrieverTool(Tool): return "No matching guest information found." # Initialize the tool -guest_info_tool = GuestInfoRetrieverTool(docs_processed) +guest_info_tool = GuestInfoRetrieverTool(docs) ``` Let's understand this tool. @@ -170,6 +170,6 @@ Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring yo Try extending the `GuestInfoRetrieverTool` to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? -When you're done, implent your guest retriever tool in the `retriever.py` file. +When you're done, implement your guest retriever tool in the `retriever.py` file. diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 714b083..ad340b7 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -132,5 +132,5 @@ By integrating these tools, Alfred is now equipped to handle a variety of tasks, Try implementing a tool that can be used to get the latest news about a specific topic. -When you're done, implement your tool in the `tools.py` file. +When you're done, implement your custom tools in the `tools.py` file. \ No newline at end of file From 626a826eeb83385fa5a399eadc9ed6ef11369483 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Tue, 11 Mar 2025 12:49:11 +0100 Subject: [PATCH 15/46] fix mdx extensions in toc file --- units/en/_toctree.yml | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index cb3efbe..8519a6d 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -92,6 +92,22 @@ title: Quick Quiz 2 - local: unit2/llama-index/conclusion title: Conclusion +- title: Unit 3. Use Case for Agentic RAG + sections: + - local: unit3/agentic-rag/introduction + title: Introduction + - local: unit3/agentic-rag/gala + title: A Gala to Remember + - local: unit3/agentic-rag/agentic-rag + title: Agentic Retrieval Augmented Generation (RAG) + - local: unit3/agentic-rag/invitees + title: Give your agent a database + - local: unit3/agentic-rag/tools + title: Creating a Weather Tool + - local: unit3/agentic-rag/agent + title: Putting your agent to the test + - local: unit3/agentic-rag/conclusion + title: Conclusion - title: Bonus Unit 1. Fine-tuning an LLM for Function-calling sections: - local: bonus-unit1/introduction @@ -102,22 +118,6 @@ title: Let's Fine-Tune your model for Function-calling - local: bonus-unit1/conclusion title: Conclusion -- title: Unit 3. Use Case for Agentic RAG - sections: - - local: unit3/agentic-rag/introduction - title: Introduction - - local: unit3/agentic-rag/gala.mdx - title: A Gala to Remember - - local: unit3/agentic-rag/agentic-rag.mdx - title: Agentic Retrieval Augmented Generation (RAG) - - local: unit3/agentic-rag/invitees.mdx - title: Give your agent a database - - local: unit3/agentic-rag/tools.mdx - title: Creating a Weather Tool - - local: unit3/agentic-rag/agent.mdx - title: Putting your agent to the test - - local: unit3/agentic-rag/conclusion.mdx - title: Conclusion - title: When the next steps are published? sections: - local: communication/next-units From e39bd724df025ded5252fe69bba36ec214e9db23 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 17 Mar 2025 16:05:32 +0100 Subject: [PATCH 16/46] Apply suggestions from code review Co-authored-by: David Berenstein --- units/en/unit3/agentic-rag/actualities.mdx | 1 - units/en/unit3/agentic-rag/agent.mdx | 4 ++-- units/en/unit3/agentic-rag/agentic-rag.mdx | 4 ++-- units/en/unit3/agentic-rag/gala.mdx | 14 ++++++------- units/en/unit3/agentic-rag/introduction.mdx | 2 +- units/en/unit3/agentic-rag/invitees.mdx | 23 ++++++++++++++++----- units/en/unit3/agentic-rag/tools.mdx | 18 ++++++++++++++-- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/units/en/unit3/agentic-rag/actualities.mdx b/units/en/unit3/agentic-rag/actualities.mdx index 1ec0f31..9f32b0a 100644 --- a/units/en/unit3/agentic-rag/actualities.mdx +++ b/units/en/unit3/agentic-rag/actualities.mdx @@ -5,4 +5,3 @@ Remember that we want Alfred to establish his presence as true renaissance host, To do so, we need to make sure that Alfred has access to the latest news and information about the world. Let's start by creating a web search tool for Alfred! - diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index d8c8d99..130735f 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -90,7 +90,7 @@ print(response) Expected output (will vary due to randomness): ``` 🎩 Alfred's Response: -I've checked the weather in Paris for you. Currently, it's Clear with a temperature of 25°C. These conditions are perfect for the fireworks display tonight. The clear skies will provide excellent visibility for the spectacular show, and the comfortable temperature will ensure guests can enjoy the outdoor event without discomfort. +I've checked the weather in Paris for you. Currently, it's clear with a temperature of 25°C. These conditions are perfect for the fireworks display tonight. The clear skies will provide excellent visibility for the spectacular show, and the comfortable temperature will ensure the guests can enjoy the outdoor event without discomfort. ``` ### Example 3: Impressing AI Researchers @@ -108,7 +108,7 @@ print(response) Expected output: ``` 🎩 Alfred's Response: -I've looked up information about Anthropic's models. The most downloaded model by Anthropic is claude-3-haiku-20240307, which has received over 1,200,000 downloads. This is their lightweight, fastest, and most efficient model in the Claude 3 family. It would be a great conversation starter to mention this to your guest from Anthropic, perhaps asking them about their latest research or the capabilities of their Claude models. +I've looked up information about Anthropic's models. The most downloaded model by Anthropic is claude-3-haiku-20240307, which has received over 1,200,000 downloads. This is their lightweight, fastest, and most efficient model in the Claude 3 family. It would be a great conversation starter to mention this to your guests from Anthropic, perhaps asking them about their latest research or the capabilities of their Claude models. ``` ### Example 4: Combining Multiple Tools diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx index 53f2cfc..71f9bc9 100644 --- a/units/en/unit3/agentic-rag/agentic-rag.mdx +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -5,8 +5,8 @@ In this unit, we'll be taking a look at how we can use Agentic RAG to help Alfre We know we've already discussed Retrieval Augmented Generation (RAG) and agentic RAG in the previous unit, so feel free to skip ahead if you're already familiar with the concepts. LLMs are trained on enormous bodies of data to learn general knowledge. -However, the world knowledge model of LLMs may not always be relevant and up-to-date data. -**RAG solves this problem by finding and retrieving relevant information from your data and forward that to the LLM.** +However, the world knowledge model of LLMs may not always be relevant and up-to-date information. +**RAG solves this problem by finding and retrieving relevant information from your data and forwarding that to the LLM.** ![RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/rag.png) diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index 1644f4a..7761664 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -2,9 +2,9 @@ Now, it's time to get our hands dirty with an actual use case. Let's set the stage! -**You decided to host the most extravagant and opulent party of the century.** This means, lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. +**You decided to host the most extravagant and opulent party of the century.** This means lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. -Alfred, your friendly neighborhood agent, is getting ready to watch over all of your needs for this party and **Alfred is going to manage everything himself**. In order to do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! +Alfred, your friendly neighbourhood agent, is getting ready to watch over all of your needs for this party, and **Alfred is going to manage everything himself**. To do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! Not only that, but he also needs to make sure that the party is going to be a success, so **he needs to be able to answer any questions about the party during the party**, while also being able to handle any unexpected situations that may arise. @@ -12,17 +12,17 @@ He can't do this alone, so we need to make sure that Alfred has access to all of First, let's give him a list of hard requirements for the gala. -## The gala requirements +## The Gala Requirements A properly educated person in the age of the **renaissance** needs to have three main traits. -He or she needed to be profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a gala that is truly unforgettable. -Although, to avoid any conflicts, there are some **topics, like politics and religion, that are to be avoided at a gala.** It needs to be an inclusive party without conflicts related to beliefs and ideals. +He or she needed to be profound in the **knowledge of sports, culture, and science**. So, we need to make sure we can impress our guests with our knowledge and provide them with a truly unforgettable gala. +However, to avoid any conflicts, there are some **topics, like politics and religion, that are to be avoided at a gala.** It needs to be an inclusive party without conflicts related to beliefs and ideals. -According to etiquette, **a good host needs to be aware of all guest information**, including their life stories and endeavors. A good host also gossips and shares stories about the guests with one another. +According to etiquette, **a good host needs to be aware of all guest information**, including their life stories and endeavours. A good host also gossips and shares stories about the guests with one another. Lastly, we need to make sure that we've got **some general knowledge about the weather** to ensure we can continuously find a real-time update to ensure perfect timing to launch the fireworks and end the gala with a bang! 🎆 -As you can see, Alfred needs a lot of information to be able to host the gala. +As you can see, Alfred needs a lot of information to host the gala. Luckily, we can help and prepare Alfred by giving him some **Retrieval Augmented Generation (RAG) training!** Let's start by creating the tools that Alfred needs to be able to host the gala! diff --git a/units/en/unit3/agentic-rag/introduction.mdx b/units/en/unit3/agentic-rag/introduction.mdx index 14d71bd..b2b640f 100644 --- a/units/en/unit3/agentic-rag/introduction.mdx +++ b/units/en/unit3/agentic-rag/introduction.mdx @@ -4,5 +4,5 @@ In this unit, we will be using the Agentic RAG to create a tool that can be used This is a 'real-world' use case for Agentic RAG, that you could use in your own projects or workplaces. -Of course, we will frame our project around Alfred, our friendly agent, who is hosting the gala. +Of course, we will frame our project around Alfred, our friendly agent who is hosting the gala. However, you can apply the same workflow to any of your agentic RAG problems! diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index a7a74f5..bcb6acb 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -14,7 +14,7 @@ This is where Retrieval Augmented Generation (RAG) shines! By combining a retrie ## Dataset Overview -Our dataset (`agents-course/unit3-invitees`) contains the following fields for each guest: +Our dataset [`agents-course/unit3-invitees`](https://huggingface.co/datasets/agents-course/unit3-invitees/) contains the following fields for each guest: - **Name**: Guest's full name - **Relation**: How the guest is related to the host @@ -42,6 +42,8 @@ We'll create a custom tool that Alfred can use to quickly retrieve guest informa 2. Create the Retriever Tool 3. Integrate the Tool with Alfred +Let's start with loading and preparing the dataset! + ### Step 1: Load and Prepare the Dataset First, we need to transform our raw guest data into a format that's optimized for retrieval. @@ -58,7 +60,12 @@ guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="tra # Convert dataset entries into Document objects docs = [ Document( - page_content=f"Name: {guest['name']}\nRelation: {guest['relation']}\nDescription: {guest['description']}\nEmail: {guest['email']}", + page_content="\n".join([ + f"Name: {guest['name']}", + f"Relation: {guest['relation']}", + f"Description: {guest['description']}", + f"Email: {guest['email']}" + ]) metadata={"name": guest["name"]} ) for guest in guest_dataset @@ -70,6 +77,8 @@ In the code above, we: - Convert each guest entry into a `Document` object with formatted content - Store the `Document` objects in a list +This means we've got all of our data nicely available so we can get started with configuring our retrieval. + ### Step 2: Create the Retriever Tool Now, let's create a custom tool that Alfred can use to search through our guest information. @@ -109,7 +118,7 @@ class GuestInfoRetrieverTool(Tool): guest_info_tool = GuestInfoRetrieverTool(docs) ``` -Let's understand this tool. +Let's understand this tool step-by-step. - The `name` and `description` help the agent understand when and how to use this tool - The `inputs` define what parameters the tool expects (in this case, a search query) - We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings @@ -134,7 +143,11 @@ response = alfred.run("Tell me about our guest named 'Lady Ada Lovelace'.") print("🎩 Alfred's Response:") print(response) ``` - +Expected output: +```python +🎩 Alfred's Response: +Based on the information I retrieved, Lady Ada Lovelace is an esteemed mathematician and friend. She is renowned for her pioneering work in mathematics and computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email address is ada.lovelace@example.com. +``` What's happening in this final step: - We initialize a Hugging Face model using the `HfApiModel` class - We create our agent (Alfred) as a `CodeAgent`, which can execute Python code to solve problems @@ -164,6 +177,7 @@ Now that Alfred can retrieve guest information, consider how you might enhance t 1. **Improve the retriever** to use a more sophisticated algorithm like [sentence-transformers](https://www.sbert.net/) 2. **Implement a conversation memory** so Alfred remembers previous interactions 3. **Combine with web search** to get the latest information on unfamiliar guests +4. **Integrate multiple indexes** to get more complete information from verified sources Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring your gala is remembered as the most sophisticated and delightful event of the century! @@ -172,4 +186,3 @@ Try extending the `GuestInfoRetrieverTool` to also return conversation starters When you're done, implement your guest retriever tool in the `retriever.py` file. - diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index ad340b7..d10a5ba 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -17,10 +17,13 @@ from smolagents import DuckDuckGoSearchTool search_tool = DuckDuckGoSearchTool() # Example usage -results = search_tool("Who's the current president of France?") +results = search_tool("Who's the current President of France?") print(results) ``` - +Expected output: +```python +The current President of France in Emanual Macron. +``` ## Creating a Custom Tool for Weather Information to Schedule the Fireworks Remember that we need to make sure the fireworks are not cancelled due to bad weather? @@ -99,7 +102,11 @@ hub_stats_tool = HubStatsTool() # Example usage print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook ``` +Expected output: +```Python +The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads." +``` With the Hub Stats Tool, Alfred can now impress influential AI builders by discussing their most popular models. ## Integrating Tools with Alfred @@ -125,6 +132,13 @@ print("🎩 Alfred's Response:") print(response) ``` +Expected output: +```python +🎩 Alfred's Response: +Meta or previously facebook is an American multinational technology conglomerate. +The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads. +``` + ## Conclusion By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. From 6ac4b0cd0e037fa55ac47f575a9fe12772a40a29 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 18 Mar 2025 11:23:04 +0100 Subject: [PATCH 17/46] Updated suggestions for Agentic RAG use case (unit 3) --- units/en/unit3/agentic-rag/actualities.mdx | 7 ------- units/en/unit3/agentic-rag/agent.mdx | 2 +- units/en/unit3/agentic-rag/agentic-rag.mdx | 8 +++++--- units/en/unit3/agentic-rag/conclusion.mdx | 2 +- units/en/unit3/agentic-rag/gala.mdx | 2 +- units/en/unit3/agentic-rag/tools.mdx | 6 ++++-- 6 files changed, 12 insertions(+), 15 deletions(-) delete mode 100644 units/en/unit3/agentic-rag/actualities.mdx diff --git a/units/en/unit3/agentic-rag/actualities.mdx b/units/en/unit3/agentic-rag/actualities.mdx deleted file mode 100644 index 9f32b0a..0000000 --- a/units/en/unit3/agentic-rag/actualities.mdx +++ /dev/null @@ -1,7 +0,0 @@ -# Give your agent access to the web - -Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. - -To do so, we need to make sure that Alfred has access to the latest news and information about the world. - -Let's start by creating a web search tool for Alfred! diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 130735f..cc6eabc 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -9,7 +9,7 @@ In this section, we'll combine the guest information retrieval, web search, weat Instead of reimplementing all the tools we've created in previous sections, we'll import them from their respective modules which we saved in the `tools.py` and `retriever.py` files. -If you haven't implemented the tools yet, go back to the [tools](./tools.mdx) and [retriever](./invitees.mdx) sections implement them, and add them to the `tools.py` and `retriever.py` files. +If you haven't implemented the tools yet, go back to the [tools](./tools.mdx) and [retriever](./invitees.mdx) sections to implement them, and add them to the `tools.py` and `retriever.py` files. Let's import the necessary libraries and tools from the previous sections: diff --git a/units/en/unit3/agentic-rag/agentic-rag.mdx b/units/en/unit3/agentic-rag/agentic-rag.mdx index 71f9bc9..75833f1 100644 --- a/units/en/unit3/agentic-rag/agentic-rag.mdx +++ b/units/en/unit3/agentic-rag/agentic-rag.mdx @@ -8,7 +8,7 @@ LLMs are trained on enormous bodies of data to learn general knowledge. However, the world knowledge model of LLMs may not always be relevant and up-to-date information. **RAG solves this problem by finding and retrieving relevant information from your data and forwarding that to the LLM.** -![RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/rag.png) +![RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/llama-index/rag.png) Now, think about how Alfred works: @@ -20,6 +20,8 @@ Just as Alfred needs to search through your household information to be helpful, **Agentic RAG is a powerful way to use agents to answer questions about your data.** We can pass various tools to Alfred to help him answer questions. However, instead of answering the question on top of documents automatically, Alfred can decide to use any other tool or flow to answer the question. -![Agentic RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/agentic-rag.png) +![Agentic RAG](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/llama-index/agentic-rag.png) -Let's start **creating the web search and weather tools for our agentic RAG workflow!** +Let's start **building our agentic RAG workflow!** + +First, we'll create a RAG tool to retrieve up-to-date details about the invitees. Next, we'll develop tools for web search, weather updates, and Hugging Face Hub model download statistics. Finally, we'll integrate everything to bring our agentic RAG agent to life! diff --git a/units/en/unit3/agentic-rag/conclusion.mdx b/units/en/unit3/agentic-rag/conclusion.mdx index 278f105..06e36d1 100644 --- a/units/en/unit3/agentic-rag/conclusion.mdx +++ b/units/en/unit3/agentic-rag/conclusion.mdx @@ -8,7 +8,7 @@ The combination of RAG with agentic capabilities demonstrates how powerful AI as - Domain-specific tools (weather information, Hub stats) - Memory of past interactions -With these capabilities, Alfred is now well-equipped to be the perfect host, able to answer questions about guests, provide up-to-date information, and ensure the gala runs smoothly - even managing the perfect timing for the fireworks display! +With these capabilities, Alfred is now well-equipped to be the perfect host, able to answer questions about guests, provide up-to-date information, and ensure the gala runs smoothly—even managing the perfect timing for the fireworks display! Now that you've built a complete agent, you might want to explore: diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index 7761664..ec6655b 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -2,7 +2,7 @@ Now, it's time to get our hands dirty with an actual use case. Let's set the stage! -**You decided to host the most extravagant and opulent party of the century.** This means lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display and much more. +**You decided to host the most extravagant and opulent party of the century.** This means lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display, and much more. Alfred, your friendly neighbourhood agent, is getting ready to watch over all of your needs for this party, and **Alfred is going to manage everything himself**. To do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index d10a5ba..aa99a45 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -1,6 +1,7 @@ # Giving your agents access to the web -In this unit, we'll be giving Alfred access to the web to help him find the latest news and information about the world. +In this section, we'll be giving Alfred access to the web to help him find the latest news and information about the world. +Additionally. we will give Alfred acess to weather details and models downloads statistics. ## Give your agent access to the web @@ -22,8 +23,9 @@ print(results) ``` Expected output: ```python -The current President of France in Emanual Macron. +The current President of France in Emmanuel Macron. ``` + ## Creating a Custom Tool for Weather Information to Schedule the Fireworks Remember that we need to make sure the fireworks are not cancelled due to bad weather? From db61aff850c74063b80c6766a951e773850a612a Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 18 Mar 2025 11:40:13 +0100 Subject: [PATCH 18/46] Updated titles and --- units/en/_toctree.yml | 10 +++++----- units/en/unit3/agentic-rag/conclusion.mdx | 1 + units/en/unit3/agentic-rag/introduction.mdx | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index 8519a6d..71d98de 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -95,17 +95,17 @@ - title: Unit 3. Use Case for Agentic RAG sections: - local: unit3/agentic-rag/introduction - title: Introduction + title: Introduction to Use Case for Agentic RAG - local: unit3/agentic-rag/gala - title: A Gala to Remember + title: A Gala to Remember - local: unit3/agentic-rag/agentic-rag title: Agentic Retrieval Augmented Generation (RAG) - local: unit3/agentic-rag/invitees - title: Give your agent a database + title: Creating a RAG Tool for Guest Stories - local: unit3/agentic-rag/tools - title: Creating a Weather Tool + title: Building and Integrating Tools for Your Agent - local: unit3/agentic-rag/agent - title: Putting your agent to the test + title: Creating Your Gala Agent - local: unit3/agentic-rag/conclusion title: Conclusion - title: Bonus Unit 1. Fine-tuning an LLM for Function-calling diff --git a/units/en/unit3/agentic-rag/conclusion.mdx b/units/en/unit3/agentic-rag/conclusion.mdx index 06e36d1..03f2d3d 100644 --- a/units/en/unit3/agentic-rag/conclusion.mdx +++ b/units/en/unit3/agentic-rag/conclusion.mdx @@ -17,4 +17,5 @@ Now that you've built a complete agent, you might want to explore: - Implementing more sophisticated RAG systems with embeddings - Building multi-agent systems where agents can collaborate - Deploying your agent as a service that others can interact with + diff --git a/units/en/unit3/agentic-rag/introduction.mdx b/units/en/unit3/agentic-rag/introduction.mdx index b2b640f..7d8cf76 100644 --- a/units/en/unit3/agentic-rag/introduction.mdx +++ b/units/en/unit3/agentic-rag/introduction.mdx @@ -1,4 +1,4 @@ -# Use Case for Agentic RAG +# Introduction to Use Case for Agentic RAG In this unit, we will be using the Agentic RAG to create a tool that can be used to answer questions about the guests at the gala. From 43603fb7ce121f2b03289e61ef1953f7d38ad4ef Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 18 Mar 2025 12:06:45 +0100 Subject: [PATCH 19/46] Updated titles and --- units/en/unit3/agentic-rag/tools.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index aa99a45..224e8cf 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -1,9 +1,9 @@ -# Giving your agents access to the web +# Building and Integrating Tools for Your Agent In this section, we'll be giving Alfred access to the web to help him find the latest news and information about the world. Additionally. we will give Alfred acess to weather details and models downloads statistics. -## Give your agent access to the web +## Give Your Agent Access to the Web Remember that we want Alfred to establish his presence as a true renaissance host, with a deep knowledge of the world. From b2ed0a8e93e1ea865fb3244372b2c7484ab5e995 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 18 Mar 2025 18:25:06 +0100 Subject: [PATCH 20/46] [WIP] Adding Load and prepare dataset example for LlamaIndex --- units/en/unit3/agentic-rag/actualities.mdx | 7 ---- units/en/unit3/agentic-rag/invitees.mdx | 41 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) delete mode 100644 units/en/unit3/agentic-rag/actualities.mdx diff --git a/units/en/unit3/agentic-rag/actualities.mdx b/units/en/unit3/agentic-rag/actualities.mdx deleted file mode 100644 index 9f32b0a..0000000 --- a/units/en/unit3/agentic-rag/actualities.mdx +++ /dev/null @@ -1,7 +0,0 @@ -# Give your agent access to the web - -Remember that we want Alfred to establish his presence as true renaissance host, with a deep knowledge of the world. - -To do so, we need to make sure that Alfred has access to the latest news and information about the world. - -Let's start by creating a web search tool for Alfred! diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index bcb6acb..b4b808f 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -46,6 +46,8 @@ Let's start with loading and preparing the dataset! ### Step 1: Load and Prepare the Dataset + + First, we need to transform our raw guest data into a format that's optimized for retrieval. We will use the Hugging Face `datasets` library to load the dataset and convert it into a list of `Document` objects from the `langchain.docstore.document` module. @@ -79,6 +81,45 @@ In the code above, we: This means we've got all of our data nicely available so we can get started with configuring our retrieval. + + + +```python +from datasets import Dataset + +dummy_data = { + "name": ["Ada Lovelace", "Dr. Nikola Tesla", "Marie Curie"], + "relation": ["Pioneer in computing", "old friend from university days", "Physicist and chemist"], + "description": [ + "Ada Lovelace is regarded as the first computer programmer and a visionary thinker.", + "Dr. Nikola Tesla is an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk.", + "Marie Curie was a groundbreaking physicist and chemist, famous for her research on radioactivity." + ], + "email": ["ada.lovelace@example.com", "nikola.tesla@gmail.com", "marie.curie@example.com"] +} + +guest_dataset = Dataset.from_dict(dummy_data) +``` + +```python +from llama_index.core.schema import Document + +docs = [ + Document( + text="\n".join([ + f"Name: {guest_dataset['name'][i]}", + f"Relation: {guest_dataset['relation'][i]}", + f"Description: {guest_dataset['description'][i]}", + f"Email: {guest_dataset['email'][i]}" + ]), + metadata={"name": guest_dataset['name'][i]} + ) + for i in range(len(guest_dataset)) +] +``` + + + ### Step 2: Create the Retriever Tool Now, let's create a custom tool that Alfred can use to search through our guest information. From 25393711762a671e2f05b6d871beddc0cd3a7421 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 18 Mar 2025 18:28:22 +0100 Subject: [PATCH 21/46] Added new line --- units/en/unit3/agentic-rag/conclusion.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/units/en/unit3/agentic-rag/conclusion.mdx b/units/en/unit3/agentic-rag/conclusion.mdx index 278f105..7ebce24 100644 --- a/units/en/unit3/agentic-rag/conclusion.mdx +++ b/units/en/unit3/agentic-rag/conclusion.mdx @@ -17,4 +17,5 @@ Now that you've built a complete agent, you might want to explore: - Implementing more sophisticated RAG systems with embeddings - Building multi-agent systems where agents can collaborate - Deploying your agent as a service that others can interact with + From 0dff385e605025e64d8f14a2c80393d9d6f5c1be Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 12:15:58 +0100 Subject: [PATCH 22/46] Updated give your agent a database using LlamaIndex --- units/en/unit3/agentic-rag/invitees.mdx | 116 +++++++++++++++++++----- 1 file changed, 94 insertions(+), 22 deletions(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index b4b808f..d120064 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -46,9 +46,10 @@ Let's start with loading and preparing the dataset! ### Step 1: Load and Prepare the Dataset +First, we need to transform our raw guest data into a format that's optimized for retrieval. + -First, we need to transform our raw guest data into a format that's optimized for retrieval. We will use the Hugging Face `datasets` library to load the dataset and convert it into a list of `Document` objects from the `langchain.docstore.document` module. @@ -74,36 +75,22 @@ docs = [ ] ``` -In the code above, we: -- Load the dataset -- Convert each guest entry into a `Document` object with formatted content -- Store the `Document` objects in a list - -This means we've got all of our data nicely available so we can get started with configuring our retrieval. -```python -from datasets import Dataset +First, we need to transform our raw guest data into a format that's optimized for retrieval. -dummy_data = { - "name": ["Ada Lovelace", "Dr. Nikola Tesla", "Marie Curie"], - "relation": ["Pioneer in computing", "old friend from university days", "Physicist and chemist"], - "description": [ - "Ada Lovelace is regarded as the first computer programmer and a visionary thinker.", - "Dr. Nikola Tesla is an old friend from your university days. He's recently patented a new wireless energy transmission system and would be delighted to discuss it with you. Just remember he's passionate about pigeons, so that might make for good small talk.", - "Marie Curie was a groundbreaking physicist and chemist, famous for her research on radioactivity." - ], - "email": ["ada.lovelace@example.com", "nikola.tesla@gmail.com", "marie.curie@example.com"] -} - -guest_dataset = Dataset.from_dict(dummy_data) -``` +We will use the Hugging Face `datasets` library to load the dataset and convert it into a list of `Document` objects from the `llama_index.core.schema` module. ```python +import datasets from llama_index.core.schema import Document +# Load the dataset +guest_dataset = datasets.load_dataset("sergiopaniego/unit3-invitees", split="train") + +# Convert dataset entries into Document objects docs = [ Document( text="\n".join([ @@ -117,13 +104,24 @@ docs = [ for i in range(len(guest_dataset)) ] ``` + +In the code above, we: +- Load the dataset +- Convert each guest entry into a `Document` object with formatted content +- Store the `Document` objects in a list + +This means we've got all of our data nicely available so we can get started with configuring our retrieval. + ### Step 2: Create the Retriever Tool Now, let's create a custom tool that Alfred can use to search through our guest information. + + + We will use the `BM25Retriever` from the `langchain_community.retrievers` module to create a retriever tool. @@ -165,10 +163,49 @@ Let's understand this tool step-by-step. - We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings - The `forward` method processes the query and returns the most relevant guest information + + + +We will use the `BM25Retriever` from the `llama_index.retrievers.bm25` module to create a retriever tool. + + +The `BM25Retriever` is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from [sentence-transformers](https://www.sbert.net/). + + +```python +from llama_index.core.tools import FunctionTool +from llama_index.retrievers.bm25 import BM25Retriever + +bm25_retriever = BM25Retriever.from_defaults(nodes=docs) + +def get_guest_info_retriever(query: str) -> str: + """Retrieves detailed information about gala guests based on their name or relation.""" + results = bm25_retriever.retrieve(query) + if results: + return "\n\n".join([doc.text for doc in results[:3]]) + else: + return "No matching guest information found." + +# Initialize the tool +guest_info_tool = FunctionTool.from_defaults(get_guest_info_retriever) +``` + +Let's understand this tool step-by-step. +- The docstring help the agent understand when and how to use this tool +- The type decorators define what parameters the tool expects (in this case, a search query) +- We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings +- The method processes the query and returns the most relevant guest information + + + + ### Step 3: Integrate the Tool with Alfred Finally, let's bring everything together by creating our agent and equipping it with our custom tool: + + + ```python from smolagents import CodeAgent, HfApiModel @@ -194,6 +231,41 @@ What's happening in this final step: - We create our agent (Alfred) as a `CodeAgent`, which can execute Python code to solve problems - We ask Alfred to retrieve information about a guest named "Lady Ada Lovelace" + + + +```python +from llama_index.core.agent.workflow import AgentWorkflow +from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI + +# Initialize the Hugging Face model +llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct") + +# Create Alfred, our gala agent, with the guest info tool +alfred = AgentWorkflow.from_tools_or_functions( + [guest_info_tool], + llm=llm, +) + +# Example query Alfred might receive during the gala +response = await alfred.run("Tell me about our guest named 'Lady Ada Lovelace'.") + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: + +```python +🎩 Alfred's Response: +Lady Ada Lovelace is an esteemed mathematician and friend, renowned for her pioneering work in mathematics and computing. She is celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email is ada.lovelace@example.com. +``` + +What's happening in this final step: +- We initialize a Hugging Face model using the `HuggingFaceInferenceAPI` class +- We create our agent (Alfred) as a `AgentWorkflow`, including the tool we just created +- We ask Alfred to retrieve information about a guest named "Lady Ada Lovelace" + ## Example Interaction During the gala, a conversation might flow like this: From ab781d8e267808198680a0d0d6d4c6d8b7e0c5cc Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 12:32:43 +0100 Subject: [PATCH 23/46] Small improvements to LlamaIndex sample --- units/en/unit3/agentic-rag/invitees.mdx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index d120064..861bd55 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -79,8 +79,6 @@ docs = [ -First, we need to transform our raw guest data into a format that's optimized for retrieval. - We will use the Hugging Face `datasets` library to load the dataset and convert it into a list of `Document` objects from the `llama_index.core.schema` module. ```python @@ -125,7 +123,7 @@ Now, let's create a custom tool that Alfred can use to search through our guest We will use the `BM25Retriever` from the `langchain_community.retrievers` module to create a retriever tool. -The `BM25Retriever` is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from [sentence-transformers](https://www.sbert.net/). + The BM25Retriever is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from sentence-transformers. ```python @@ -169,7 +167,7 @@ Let's understand this tool step-by-step. We will use the `BM25Retriever` from the `llama_index.retrievers.bm25` module to create a retriever tool. -The `BM25Retriever` is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from [sentence-transformers](https://www.sbert.net/). + The BM25Retriever is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from sentence-transformers. ```python @@ -266,6 +264,9 @@ What's happening in this final step: - We create our agent (Alfred) as a `AgentWorkflow`, including the tool we just created - We ask Alfred to retrieve information about a guest named "Lady Ada Lovelace" + + + ## Example Interaction During the gala, a conversation might flow like this: From c4daed3dcc2c30981805eb797b61a36af7d25906 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 12:50:23 +0100 Subject: [PATCH 24/46] Updated Tools using LlamaIndex --- units/en/unit3/agentic-rag/tools.mdx | 150 ++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-) diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index d10a5ba..c08e103 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -10,6 +10,9 @@ To do so, we need to make sure that Alfred has access to the latest news and inf Let's start by creating a web search tool for Alfred! + + + ```python from smolagents import DuckDuckGoSearchTool @@ -20,10 +23,43 @@ search_tool = DuckDuckGoSearchTool() results = search_tool("Who's the current President of France?") print(results) ``` + Expected output: + ```python The current President of France in Emanual Macron. ``` + + + + +```python +from llama_index.core.tools import FunctionTool +from duckduckgo_search import DDGS + +def get_search_info(query: str, max_results: int = 10) -> str: + ddgs = DDGS() + results = ddgs.text(query, max_results=max_results) + if len(results) == 0: + raise Exception("No results found! Try a less restrictive/shorter query.") + postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results] + return "## Search Results\n\n" + "\n\n".join(postprocessed_results) + +# Initialize the DuckDuckGo search tool +search_tool = FunctionTool.from_defaults(get_search_info) +# Example usage +print(search_tool("Who's the current President of France?")) +``` + +Expected output: + +```python +The current President of France in Emanual Macron. +``` + + + + ## Creating a Custom Tool for Weather Information to Schedule the Fireworks Remember that we need to make sure the fireworks are not cancelled due to bad weather? @@ -34,6 +70,9 @@ Let's create a custom tool that can be used to call an external weather API and For the sake of simplicity, we're using a dummy weather API for this example. If you want to use a real weather API, you could implement a weather tool that uses the OpenWeatherMap API, like in [Unit 1](../unit1/tutorial.mdx). + + + ```python from smolagents import Tool import random @@ -64,10 +103,41 @@ class WeatherInfoTool(Tool): weather_info_tool = WeatherInfoTool() ``` + + + +```python +import random +from llama_index.core.tools import FunctionTool + +def get_weather_info(location: str) -> str: + """Fetches dummy weather information for a given location.""" + # Dummy weather data + weather_conditions = [ + {"condition": "Rainy", "temp_c": 15}, + {"condition": "Clear", "temp_c": 25}, + {"condition": "Windy", "temp_c": 20} + ] + # Randomly select a weather condition + data = random.choice(weather_conditions) + return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" + +# Initialize the tool +weather_info_tool = FunctionTool.from_defaults(get_weather_info) + +print(weather_info_tool('Paris')) +``` + + + + ## Creating a Hub Stats Tool for Influential AI Builders Alfred can impress influential AI builders by discussing their most popular models. We'll create a tool to fetch model statistics from the Hugging Face Hub based on a username. + + + ```python from smolagents import Tool from huggingface_hub import list_models @@ -100,19 +170,62 @@ class HubStatsTool(Tool): hub_stats_tool = HubStatsTool() # Example usage -print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook +print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook ``` + Expected output: ```Python -The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads." +The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads. ``` + + + + +```python +import random +from llama_index.core.tools import FunctionTool +from huggingface_hub import list_models + +def get_hub_stats(author: str) -> str: + """Fetches the most downloaded model from a specific author on the Hugging Face Hub.""" + try: + # List models from the specified author, sorted by downloads + models = list(list_models(author=author, sort="downloads", direction=-1, limit=1)) + + if models: + model = models[0] + return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads." + else: + return f"No models found for author {author}." + except Exception as e: + return f"Error fetching models for {author}: {str(e)}" + +# Initialize the tool +hub_stats_tool = FunctionTool.from_defaults(get_hub_stats) + +# Example usage +print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook +``` + +Expected output: + +```Python +The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads. +``` + + + + With the Hub Stats Tool, Alfred can now impress influential AI builders by discussing their most popular models. ## Integrating Tools with Alfred Now that we have all the tools, let's integrate them into Alfred's agent: + + + ```python from smolagents import CodeAgent, HfApiModel @@ -133,12 +246,45 @@ print(response) ``` Expected output: + ```python 🎩 Alfred's Response: Meta or previously facebook is an American multinational technology conglomerate. The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads. ``` + + + +```python +from llama_index.core.agent.workflow import AgentWorkflow +from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI + +# Initialize the Hugging Face model +llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct") +# Create Alfred with all the tools +alfred = AgentWorkflow.from_tools_or_functions( + [search_tool, weather_info_tool, hub_stats_tool], + llm=llm +) + +# Example query Alfred might receive during the gala +response = await alfred.run("Who is Meta AI and what's their most popular model?") + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: + +```python +🎩 Alfred's Response: +Meta AI, formerly known as Facebook AI, is a research lab focused on advancing and applying artificial intelligence to solve real-world problems. Their most popular model on the Hugging Face Hub is `facebook/esmfold_v1`, which has been downloaded 12,544,550 times. +``` + + + + ## Conclusion By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. From 50050c03027cbfca9e2d12a3feed96d918304f7b Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 13:13:56 +0100 Subject: [PATCH 25/46] Updated Assembling agent using LlamaIndex --- units/en/unit3/agentic-rag/agent.mdx | 181 +++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 130735f..538e2bb 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -14,6 +14,9 @@ If you haven't implemented the tools yet, go back to the [tools](./tools.mdx) an Let's import the necessary libraries and tools from the previous sections: + + + ```python # Import necessary libraries import random @@ -51,6 +54,46 @@ alfred = CodeAgent( ) ``` + + + +```python +# Import necessary libraries +from llama_index.core.agent.workflow import AgentWorkflow +from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI +``` + +Now, let's combine all these tools into a single agent: + +```python +# Initialize the Hugging Face model +llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct") + +# Initialize the web search tool +search_tool = FunctionTool.from_defaults(get_search_info) + +# Initialize the weather tool +weather_info_tool = FunctionTool.from_defaults(get_weather_info) + +# Initialize the Hub stats tool +hub_stats_tool = FunctionTool.from_defaults(get_hub_stats) + +# Load the guest dataset and initialize the guest info tool +guest_info_tool = FunctionTool.from_defaults(get_guest_info_retriever) + +# Create Alfred with all the tools + + +# Initialize the Hugging Face model +alfred = AgentWorkflow.from_tools_or_functions( + [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool], + llm=llm, +) +``` + + + + Your agent is now ready to use! ## Using Alfred: End-to-End Examples @@ -61,6 +104,9 @@ Now that Alfred is fully equipped with all the necessary tools, let's see how he Let's see how Alfred can help us with our guest information. + + + ```python query = "Tell me about Lady Ada Lovelace. What's her background and how is she related to me?" response = alfred.run(query) @@ -70,15 +116,41 @@ print(response) ``` Expected output: + ``` 🎩 Alfred's Response: Based on the information I retrieved, Lady Ada Lovelace is an esteemed mathematician and friend. She is renowned for her pioneering work in mathematics and computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email address is ada.lovelace@example.com. ``` + + + +```python +query = "Tell me about Lady Ada Lovelace. What's her background?" +response = await alfred.run(query) + +print("🎩 Alfred's Response:") +print(response.response.blocks[0].text) +``` + +Expected output: + +``` +🎩 Alfred's Response: +Lady Ada Lovelace was an English mathematician and writer, best known for her work on Charles Babbage's Analytical Engine. She was the first to recognize that the machine had applications beyond pure calculation. +``` + + + + + ### Example 2: Checking the Weather for Fireworks Let's see how Alfred can help us with the weather. + + + ```python query = "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?" response = alfred.run(query) @@ -93,10 +165,34 @@ Expected output (will vary due to randomness): I've checked the weather in Paris for you. Currently, it's clear with a temperature of 25°C. These conditions are perfect for the fireworks display tonight. The clear skies will provide excellent visibility for the spectacular show, and the comfortable temperature will ensure the guests can enjoy the outdoor event without discomfort. ``` + + + +```python +query = "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?" +response = await alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: + +``` +🎩 Alfred's Response: +The weather in Paris tonight is rainy with a temperature of 15°C. Given the rain, it may not be suitable for a fireworks display. +``` + + + + ### Example 3: Impressing AI Researchers Let's see how Alfred can help us impress AI researchers. + + + ```python query = "One of our guests is from Anthropic. What can you tell me about their most popular model?" response = alfred.run(query) @@ -106,15 +202,40 @@ print(response) ``` Expected output: + ``` 🎩 Alfred's Response: I've looked up information about Anthropic's models. The most downloaded model by Anthropic is claude-3-haiku-20240307, which has received over 1,200,000 downloads. This is their lightweight, fastest, and most efficient model in the Claude 3 family. It would be a great conversation starter to mention this to your guests from Anthropic, perhaps asking them about their latest research or the capabilities of their Claude models. ``` + + + +```python +query = "One of our guests is from Google. What can you tell me about their most popular model?" +response = await alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: + +``` +🎩 Alfred's Response: +The most popular model by Google on the Hugging Face Hub is google/electra-base-discriminator, with 28,546,752 downloads. +``` + + + + ### Example 4: Combining Multiple Tools Let's see how Alfred can help us prepare for a conversation with Dr. Nikola Tesla. + + + ```python query = "I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?" response = alfred.run(query) @@ -124,6 +245,7 @@ print(response) ``` Expected output: + ``` 🎩 Alfred's Response: I've gathered information to help you prepare for your conversation with Dr. Nikola Tesla. @@ -148,10 +270,42 @@ Conversation Starters: This should give you plenty to discuss with Dr. Tesla while demonstrating your knowledge of his interests and recent developments in his field. ``` + + + +```python +query = "I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?" +response = await alfred.run(query) + +print("🎩 Alfred's Response:") +print(response) +``` + +Expected output: + +``` +🎩 Alfred's Response: +Here are some recent advancements in wireless energy that you might find useful for your conversation with Dr. Nikola Tesla: + +1. **Advancements and Challenges in Wireless Power Transfer**: This article discusses the evolution of wireless power transfer (WPT) from conventional wired methods to modern applications, including solar space power stations. It highlights the initial focus on microwave technology and the current demand for WPT due to the rise of electric devices. + +2. **Recent Advances in Wireless Energy Transfer Technologies for Body-Interfaced Electronics**: This article explores wireless energy transfer (WET) as a solution for powering body-interfaced electronics without the need for batteries or lead wires. It discusses the advantages and potential applications of WET in this context. + +3. **Wireless Power Transfer and Energy Harvesting: Current Status and Future Trends**: This article provides an overview of recent advances in wireless power supply methods, including energy harvesting and wireless power transfer. It presents several promising applications and discusses future trends in the field. + +4. **Wireless Power Transfer: Applications, Challenges, Barriers, and the +``` + + + + ## Advanced Features: Conversation Memory To make Alfred even more helpful during the gala, we can enable conversation memory so he remembers previous interactions: + + + ```python # Create Alfred with conversation memory alfred_with_memory = CodeAgent( @@ -173,6 +327,33 @@ print("🎩 Alfred's Second Response:") print(response2) ``` + + + +```python +from llama_index.core.workflow import Context + +alfred = AgentWorkflow.from_tools_or_functions( + [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool], + llm=llm +) + +# Remembering state +ctx = Context(alfred) + +response1 = await alfred.run("Tell me about Lady Ada Lovelace.", ctx=ctx) +print("🎩 Alfred's First Response:") +print(response1) + +# Second interaction (referencing the first) +response2 = await alfred.run("What projects is she currently working on?", ctx=ctx) +print("🎩 Alfred's Second Response:") +print(response2) +``` + + + + ## Conclusion Congratulations! You've successfully built Alfred, a sophisticated agent equipped with multiple tools to help host the most extravagant gala of the century. Alfred can now: From d00680ce1e3c1c57309213b9837ce1a678945e6f Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 13:15:08 +0100 Subject: [PATCH 26/46] Updated Assembling agent using LlamaIndex --- units/en/unit3/agentic-rag/agent.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 538e2bb..45cbbf3 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -341,6 +341,7 @@ alfred = AgentWorkflow.from_tools_or_functions( # Remembering state ctx = Context(alfred) +# First interaction response1 = await alfred.run("Tell me about Lady Ada Lovelace.", ctx=ctx) print("🎩 Alfred's First Response:") print(response1) From b707ea52a67d19ecb663cdfed4c1cd41c076c16d Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 19 Mar 2025 15:02:13 +0100 Subject: [PATCH 27/46] Added details about different frameworks --- units/en/unit3/agentic-rag/introduction.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/units/en/unit3/agentic-rag/introduction.mdx b/units/en/unit3/agentic-rag/introduction.mdx index 7d8cf76..5ac0141 100644 --- a/units/en/unit3/agentic-rag/introduction.mdx +++ b/units/en/unit3/agentic-rag/introduction.mdx @@ -6,3 +6,4 @@ This is a 'real-world' use case for Agentic RAG, that you could use in your own Of course, we will frame our project around Alfred, our friendly agent who is hosting the gala. However, you can apply the same workflow to any of your agentic RAG problems! +You can choose any of the frameworks discussed in the course for this use case. We provide code samples for each in separate tabs. \ No newline at end of file From 48d5b93e73fad7aee9cec60cb7cb5ccb816c7ec7 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Tue, 25 Mar 2025 12:59:50 +0100 Subject: [PATCH 28/46] Added LangGraph workflow for first section --- units/en/unit3/agentic-rag/invitees.mdx | 133 +++++++++++++++++++++++- 1 file changed, 131 insertions(+), 2 deletions(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index 861bd55..92872a8 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -22,9 +22,8 @@ Our dataset [`agents-course/unit3-invitees`](https://huggingface.co/datasets/age - **Email Address**: Contact information for sending invitations or follow-ups Below is a preview of the dataset: - ## Dataset Overview @@ -79,7 +88,7 @@ docs = [ f"Relation: {guest['relation']}", f"Description: {guest['description']}", f"Email: {guest['email']}" - ]) + ]), metadata={"name": guest["name"]} ) for guest in guest_dataset @@ -134,7 +143,8 @@ docs = [ f"Relation: {guest['relation']}", f"Description: {guest['description']}", f"Email: {guest['email']}" - ]) + ]), + metadata={"name": guest["name"]} ) for guest in guest_dataset ] diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 9215ed7..0977780 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -212,7 +212,7 @@ class HubStatsTool(Tool): hub_stats_tool = HubStatsTool() # Example usage -print(hub_stats_tool.invoke("facebook")) # Example: Get the most downloaded model by Facebook +print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook ``` Expected output: From c8ed937c5ec00f11fe15174229eb4d009ce99003 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Wed, 26 Mar 2025 17:01:36 +0100 Subject: [PATCH 35/46] Updated general structure --- units/en/unit3/agentic-rag/agent.mdx | 19 +++++++++++++--- units/en/unit3/agentic-rag/invitees.mdx | 22 ++++++++++-------- units/en/unit3/agentic-rag/tools.mdx | 30 ++++++++++++------------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 06b4ad0..d63b945 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -9,7 +9,7 @@ In this section, we'll combine the guest information retrieval, web search, weat Instead of reimplementing all the tools we've created in previous sections, we'll import them from their respective modules which we saved in the `tools.py` and `retriever.py` files. -If you haven't implemented the tools yet, go back to the [tools](./tools.mdx) and [retriever](./invitees.mdx) sections to implement them, and add them to the `tools.py` and `retriever.py` files. +If you haven't implemented the tools yet, go back to the tools and retriever sections to implement them, and add them to the `tools.py` and `retriever.py` files. Let's import the necessary libraries and tools from the previous sections: @@ -212,7 +212,7 @@ print(response.response.blocks[0].text) Expected output: -```python +``` 🎩 Alfred's Response: Lady Ada Lovelace was an English mathematician and writer, best known for her work on Charles Babbage's Analytical Engine. She was the first to recognize that the machine had applications beyond pure calculation. ``` @@ -229,7 +229,7 @@ print(response["response"]) Expected output: -```python +``` 🎩 Alfred's Response: Based on the information provided, Lady Ada Lovelace is a renowned mathematician and is celebrated as the first computer programmer. She worked on Charles Babbage's Analytical Engine, which is considered a precursor to modern computers. Your relation to her is described as "Pioneer in computing," indicating that she is a significant figure in the history of computing. However, the information provided does not give any specific familial or personal connection to you, only a professional one. Her email is listed as ada.lovelace@example.com, but this is likely a fictional address for illustrative purposes. ``` @@ -517,6 +517,19 @@ print(response2) +```python +response = graph.invoke({"messages": [HumanMessage(content="Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?")]}) + +print("🎩 Alfred's Response:") +print(response["response"]) +print() + +response = graph.invoke({"messages": response["messages"] + [HumanMessage(content="What projects is she currently working on?")]}) + +print("🎩 Alfred's Response:") +print(response["response"]) +``` + diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index a708f41..5f8517c 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -35,7 +35,7 @@ You can directly test the agent below: ## Dataset Overview -Our dataset [`agents-course/unit3-invitees`](https://huggingface.co/datasets/agents-course/unit3-invitees/) contains the following fields for each guest: +Our dataset [`sergiopaniego/unit3-invitees`](https://huggingface.co/datasets/sergiopaniego/unit3-invitees/) contains the following fields for each guest: - **Name**: Guest's full name - **Relation**: How the guest is related to the host @@ -78,7 +78,7 @@ import datasets from langchain.docstore.document import Document # Load the dataset -guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") +guest_dataset = datasets.load_dataset("sergiopaniego/unit3-invitees", split="train") # Convert dataset entries into Document objects docs = [ @@ -246,6 +246,10 @@ Let's understand this tool step-by-step. We will use the `BM25Retriever` from the `langchain_community.retrievers` module to create a retriever tool. + + The BM25Retriever is a great starting point for retrieval, but for more advanced semantic search, you might consider using embedding-based retrievers like those from sentence-transformers. + + ```python from langchain_community.retrievers import BM25Retriever from langchain.tools import Tool @@ -302,7 +306,7 @@ print(response) Expected output: -```python +``` 🎩 Alfred's Response: Based on the information I retrieved, Lady Ada Lovelace is an esteemed mathematician and friend. She is renowned for her pioneering work in mathematics and computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email address is ada.lovelace@example.com. ``` @@ -337,7 +341,7 @@ print(response) Expected output: -```python +``` 🎩 Alfred's Response: Lady Ada Lovelace is an esteemed mathematician and friend, renowned for her pioneering work in mathematics and computing. She is celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. Her email is ada.lovelace@example.com. ``` @@ -422,7 +426,7 @@ print(response['messages'][2].content) Expected output: -```python +``` 🎩 Alfred's Response: Based on the information provided, Lady Ada Lovelace is a notable mathematician and is celebrated for her significant contributions to the field of computing. She is particularly recognized as the first computer programmer for her work on Charles Babbage's Analytical Engine. Her relation to you is described as a friend, and her contact information includes the email address ada.lovelace@example.com. ``` @@ -463,8 +467,8 @@ Now that Alfred can retrieve guest information, consider how you might enhance t Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring your gala is remembered as the most sophisticated and delightful event of the century! - -Try extending the `GuestInfoRetrieverTool` to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? + +Try extending the retriver tool to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? -When you're done, implement your guest retriever tool in the `retriever.py` file. - +When you're done, implement your guest retriever tool in the retriever.py file. + diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 0977780..839ea3a 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -1,7 +1,7 @@ # Building and Integrating Tools for Your Agent -In this section, we'll be giving Alfred access to the web to help him find the latest news and information about the world. -Additionally. we will give Alfred acess to weather details and models downloads statistics. +In this section, we'll grant Alfred access to the web, enabling him to find the latest news and global updates. +Additionally, he'll have access to weather data and model download statistics. ## Give Your Agent Access to the Web @@ -27,7 +27,7 @@ print(results) Expected output: -```python +``` The current President of France in Emmanuel Macron. ``` @@ -50,7 +50,7 @@ print(response) Expected output: -```python +``` [{'title': 'President of France - Wikipedia', 'href': 'https://en.wikipedia.org/wiki/President_of_France', 'body': 'The president of France, officially the president of the French Republic (French: Président de la République française), is the executive head of state of France, and the commander-in-chief of the French Armed Forces.As the presidency is the supreme magistracy of the country, the position is the highest office in France. The powers, functions and duties of prior presidential offices, in ...'}, {'title': 'Emmanuel Macron - Wikipedia', 'href': 'https://en.wikipedia.org/wiki/Emmanuel_Macron', 'body': 'Emmanuel Jean-Michel Frédéric Macron (French: [emanɥɛl makʁɔ̃] ⓘ; born 21 December 1977) is a French politician who has served as President of France since 2017. He was Minister of Economics, Industry and Digital Affairs under President François Hollande between 2014-16. He has been a member of Renaissance since he founded it in 2016.. Born in Amiens, Macron studied philosophy at ...'}, {'title': 'Emmanuel Macron - Wife, Education & Family - Biography', 'href': 'https://www.biography.com/political-figures/emmanuel-macron', 'body': "Former economy minister Emmanuel Macron was elected president of France in 2017, making him the youngest president in the country's history. Search Women's History Month"}, {'title': 'Presidency of Emmanuel Macron - Wikipedia', 'href': 'https://en.wikipedia.org/wiki/Presidency_of_Emmanuel_Macron', 'body': 'Emmanuel Macron is currently serving as the 25th president of France.He took office upon winning the 2017 French presidential election.Macron was the founding member of Renaissance (originally En Marche ! and later La République En Marche !He defeated National Rally nominee Marine Le Pen.He again defeated Le Pen in the 2022 French presidential election.'}, {'title': 'Emmanuel Macron | Biography, Political Party, Age, Presidency, & Facts ...', 'href': 'https://www.britannica.com/biography/Emmanuel-Macron', 'body': "Emmanuel Macron (born December 21, 1977, Amiens, France) is a French banker and politician who was elected president of France in 2017. Macron was the first person in the history of the Fifth Republic to win the presidency without the backing of either the Socialists or the Gaullists, and he was France's youngest head of state since Napoleon I.He was reelected in 2022, becoming the first ..."}, {'title': 'Welcome to the website of the French Presidency - Élysée', 'href': 'https://www.elysee.fr/en/', 'body': "Welcome to the official website of the President of France The President's latest news All news 21 September 2023 State Visit of His Majesty King Charles III and Queen Camilla. 20 March 2025 European Council - 20 & 21 March 2025. 17 March 2025 Working lunch with the ..."}, {'title': 'Emmanuel Macron - Élysée', 'href': 'https://www.elysee.fr/en/emmanuel-macron', 'body': 'Emmanuel Macron is the eighth President of the Fifth Republic of France. He launched the movement « En Marche ! », founded on 6 April 2016, and was its leader until his victory in the presidential election on 7 May 2017. On 24 April 2022, Emmanuel Macron was re-elected President of the Republic.'}, {'title': 'Who is the current president of France? - ump-lycees.fr', 'href': 'https://www.ump-lycees.fr/en/who-is-the-current-president-of-france', 'body': 'Emmanuel Macron, born on December 21, 1977, is the current president of the French Republic.In office since May 14, 2017, he was re-elected on April 24, 2022 with 58.55% of the votes against his competitor Marine Le Pen. With a background in higher public service and investment banking, Macron approaches his mandate with a desire for change and innovation in various fields, including economics ...'}, {'title': 'Who is the President of France? - WorldAtlas', 'href': 'https://www.worldatlas.com/articles/who-is-the-president-of-france.html', 'body': "Emmanuel Macron is the current French President and also Andorra's ex-official Co-Prince. Before joining politics, he worked in various senior positions as a civil servant and he served in the Inspectorate General of Finances as an Inspector of Finances. In 2012, President Francois Hollande selected Macron as the Deputy Secretary-General in ..."}, {'title': 'President of the French Republic - Simple English Wikipedia, the free ...', 'href': 'https://simple.wikipedia.org/wiki/President_of_the_French_Republic', 'body': 'The President of the French Republic is the head of state of France. The current President is Emmanuel Macron since 14 May 2017 defeating Marine Le Pen in the second round of the presidential election on 7 May 2017. List of French presidents (Fifth Republic) N° Portrait Name ...'}] ``` @@ -67,7 +67,7 @@ print(results) Expected output: -```python +``` The current President of France in Emmanuel Macron. ``` @@ -81,7 +81,7 @@ Remember that we need to make sure the fireworks are not cancelled due to bad we Let's create a custom tool that can be used to call an external weather API and get the weather information for a given location. -For the sake of simplicity, we're using a dummy weather API for this example. If you want to use a real weather API, you could implement a weather tool that uses the OpenWeatherMap API, like in [Unit 1](../unit1/tutorial.mdx). +For the sake of simplicity, we're using a dummy weather API for this example. If you want to use a real weather API, you could implement a weather tool that uses the OpenWeatherMap API, like in Unit 1. @@ -217,7 +217,7 @@ print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Fa Expected output: -```Python +``` The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads. ``` @@ -252,7 +252,7 @@ print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Fa Expected output: -```python +``` The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads. ``` @@ -290,7 +290,7 @@ print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Fa Expected output: -```python +``` The most downloaded model by facebook is facebook/esmfold_v1 with 13,109,861 downloads. ``` @@ -327,7 +327,7 @@ print(response) Expected output: -```python +``` 🎩 Alfred's Response: Meta or previously facebook is an American multinational technology conglomerate. The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads. @@ -357,7 +357,7 @@ print(response) Expected output: -```python +``` 🎩 Alfred's Response: Facebook is a social networking service and technology company based in Menlo Park, California. It was founded by Mark Zuckerberg and allows people to create profiles, connect with friends and family, share photos and videos, and join groups based on shared interests. The most popular model by Facebook on the Hugging Face Hub is `facebook/esmfold_v1` with 13,109,861 downloads. ``` @@ -432,7 +432,7 @@ print(response["response"]) Expected output: -```python +``` 🎩 Alfred's Response: According to the company, Meta AI is set to become the world's most widely used AI assistant by the end of the year. Over 400 million people interact with Meta AI monthly, with 185 million using ... Designed to make AI accessible to everyone, the AI model is built on Meta's open-source Llama 3.2 and works across the company's ecosystem allowing users to create, interact, and benefit from AI ... We tried seven of the most popular AI chatbots (ChatGPT, Claude, Grok, Perplexity and Arc Search, Meta AI, Microsoft Copilot) to find out. ... It uses a mix of models to produce its responses ... Yet, the difference is that Meta AI, the chatbot, integrated across Meta's social media platforms, has surpassed 500 million users since its wide release in April 2024. The rise of Meta AI. Meta AI's ascent to 500 million users in just seven months is a testament to its accessibility and utility. Impact of Meta AI on the Metaverse and VR/AR; History of Meta AI. Meta AI started as Facebook AI, focusing on AI research and innovation. In 2021, when Facebook rebranded to Meta, the scope of AI expanded further. Meta AI has made significant advancements in NLP (Natural Language Processing), computer vision, and deep learning. Use Cases of Meta AI ``` @@ -443,8 +443,8 @@ According to the company, Meta AI is set to become the world's most widely used By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. - + Try implementing a tool that can be used to get the latest news about a specific topic. -When you're done, implement your custom tools in the `tools.py` file. - \ No newline at end of file +When you're done, implement your custom tools in the tools.py` file. + \ No newline at end of file From b510c5321c9e2db1658391e8425d0b65823e52ad Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 10:46:56 +0100 Subject: [PATCH 36/46] Updated missing title --- units/en/_toctree.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/units/en/_toctree.yml b/units/en/_toctree.yml index db28632..ffd471c 100644 --- a/units/en/_toctree.yml +++ b/units/en/_toctree.yml @@ -123,6 +123,7 @@ - local: unit3/agentic-rag/agent title: Creating Your Gala Agent - local: unit3/agentic-rag/conclusion + title: Conclusion - title: Bonus Unit 1. Fine-tuning an LLM for Function-calling sections: - local: bonus-unit1/introduction From f5f9d171eb4093568b9b1135efa9ce2c31fb8b19 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 13:05:39 +0100 Subject: [PATCH 37/46] Updated LangGraph --- units/en/unit3/agentic-rag/agent.mdx | 158 +++++++++--------------- units/en/unit3/agentic-rag/invitees.mdx | 104 +++++++--------- units/en/unit3/agentic-rag/tools.mdx | 93 ++++++-------- 3 files changed, 144 insertions(+), 211 deletions(-) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index d63b945..332c3e6 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -93,80 +93,63 @@ alfred = AgentWorkflow.from_tools_or_functions( ```python -from typing import TypedDict, Annotated, Optional -from langchain.llms import HuggingFaceHub +from typing import TypedDict, Annotated from langgraph.graph.message import add_messages -from langgraph.graph import StateGraph from langchain_core.messages import AnyMessage, HumanMessage, AIMessage +from langgraph.prebuilt import ToolNode +from langgraph.graph import START, StateGraph +from langgraph.prebuilt import tools_condition +from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace + +from tools import DuckDuckGoSearchTool, weather_info_tool, hub_stats_tool +from retriever import load_guest_dataset ``` Now, let’s combine all these tools into a single agent: ```python -class AgentState(TypedDict): - response: Optional[str] - messages: Annotated[list[AnyMessage], add_messages] +# Initialize the web search tool +search_tool = DuckDuckGoSearchTool() -llm = HuggingFaceHub( - repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", - huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN +# Load the guest dataset and initialize the guest info tool +guest_info_tool = load_guest_dataset() + +# Generate the chat interface, including the tools +llm = HuggingFaceEndpoint( + repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", + huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN, ) -def tools_node(data): - messages = data["messages"] - query = messages[-1].content.strip() +chat = ChatHuggingFace(llm=llm, verbose=True) +tools = [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool] +chat_with_tools = chat.bind_tools(tools) - guest_info_result = guest_info_tool.invoke(query) - guest_info_result_cleaned = guest_info_result.lower() - - match = re.search(r"'([^']+)'", query.lower()) - guest_name = match.group(1).lower() if match else "" - - if guest_name and guest_name in guest_info_result_cleaned: - response = guest_info_result - elif "weather" in query: - location_match = re.search(r"weather.*in\s+([a-zA-Z\s]+)", query) - response = weather_info_tool.invoke(location_match.group(1).strip()) if location_match else "Location not found." - elif "hugging face" in query: - response = hub_stats_tool.invoke(query) - else: - response = search_tool.invoke(query) - - return {"messages": messages + [AIMessage(content=response)]} - -def generate_response(data): - messages = data["messages"] - - if messages[-1].content == "No matching guest information found.": - return {"messages": messages + [AIMessage(content="I'm sorry, I couldn't find that guest.")]} - - history_text = "\n".join([msg.content for msg in messages]) - prompt = f"{history_text}\nUse only the info retrieved as source.\n" - - response_text = llm.invoke(prompt).strip() - - if response_text.startswith(prompt): - model_response = response_text[len(prompt):].strip() - - messages.append(AIMessage(content=model_response)) - - return {"messages": messages, "response": model_response} +# Generate the AgentState and Agent graph +class AgentState(TypedDict): + messages: Annotated[list[AnyMessage], add_messages] +def assistant(state: AgentState): + return { + "messages": [chat_with_tools.invoke(state["messages"])], + } +## The graph builder = StateGraph(AgentState) -builder.add_node("tools_node", tools_node) -builder.add_node("generate_response", generate_response) +# Define nodes: these do the work +builder.add_node("assistant", assistant) +builder.add_node("tools", ToolNode(tools)) -builder.set_entry_point("tools_node") -builder.add_edge("tools_node", "generate_response") - -graph = builder.compile() - -# Example query Alfred might receive during the gala -response = graph.invoke({"messages": "Who is Meta AI and what's their most popular model?"}) -print("🎩 Alfred's Response:") -print(response["response"]) +# Define edges: these determine how the control flow moves +builder.add_edge(START, "assistant") +builder.add_conditional_edges( + "assistant", + # If the latest message requires a tool, route to tools + # Otherwise, provide a direct response + tools_condition, +) +builder.add_edge("tools", "assistant") +alfred = builder.compile() ``` @@ -221,17 +204,17 @@ Lady Ada Lovelace was an English mathematician and writer, best known for her wo ```python -response = graph.invoke({"messages": "Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?"}) +response = alfred.invoke({"messages": "Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?"}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` Expected output: ``` 🎩 Alfred's Response: -Based on the information provided, Lady Ada Lovelace is a renowned mathematician and is celebrated as the first computer programmer. She worked on Charles Babbage's Analytical Engine, which is considered a precursor to modern computers. Your relation to her is described as "Pioneer in computing," indicating that she is a significant figure in the history of computing. However, the information provided does not give any specific familial or personal connection to you, only a professional one. Her email is listed as ada.lovelace@example.com, but this is likely a fictional address for illustrative purposes. +Ada Lovelace was an English mathematician and writer, known for her work on Charles Babbage's Analytical Engine, which is considered a precursor to the modern computer. She is recognized as the first computer programmer because of her program for calculating Bernoulli numbers. Born as Augusta Ada King, Countess of Lovelace, she married William King, 8th Baron King, in 1835, becoming Lady Lovelace. They had three children together. Ada Lovelace Day is celebrated annually to honor women's contributions to science and technology. However, there is no direct relation provided in the information suggesting a connection to you. ``` @@ -281,17 +264,17 @@ The weather in Paris tonight is rainy with a temperature of 15°C. Given the rai ```python -response = graph.invoke({"messages": "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?"}) +response = alfred.invoke({"messages": "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?"}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` Expected output: ``` 🎩 Alfred's Response: -Based on the information provided, the weather in Paris tonight is windy with a temperature of 20°C. While the temperature seems suitable for a fireworks display, the wind could pose a challenge. It might be advisable to monitor the wind conditions closer to the event time to ensure the safety and quality of the fireworks display. +The weather in Paris tonight is rainy with a temperature of 15°C, which may not be suitable for your fireworks display. ``` @@ -339,17 +322,17 @@ The most popular model by Google on the Hugging Face Hub is google/electra-base- ```python -response = graph.invoke({"messages": "One of our guests is from Anthropic. What can you tell me about their most popular model?"}) +response = alfred.invoke({"messages": "One of our guests is from Qwen. What can you tell me about their most popular model?"}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` Expected output: ``` 🎩 Alfred's Response: -Based on the information provided, Anthropic's most popular model appears to be Claude 3.5 Sonnet. This model is described as being faster and cheaper than their previous flagship model, Claude 3 Opus. It is also noted that Claude 3.5 Sonnet can use a computer like a human would, and it has been released in beta mode. However, there is some confusion in the provided information as it also mentions Claude 3.7 Sonnet, which is described as being able to "think" about questions for as long as users want it to. It is unclear whether Claude 3.5 Sonnet or Claude 3.7 Sonnet is Anthropic's most popular model based on the given information. The most recent and clear information points to Claude 3.5 Sonnet as Anthropic's latest model with improved performance and cost-effectiveness. +The most downloaded model by Qwen is Qwen/Qwen2.5-VL-7B-Instruct with 3,313,345 downloads. ``` @@ -423,40 +406,18 @@ Here are some recent advancements in wireless energy that you might find useful + ```python -response = graph.invoke({"messages":"I need to speak with 'Dr. Nikola Tesla' about recent advancements in wireless energy. Can you help me prepare for this conversation?"}) +response = alfred.invoke({"messages":"I need to speak with 'Dr. Nikola Tesla' about recent advancements in wireless energy. Can you help me prepare for this conversation?"}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` Expected output: ``` -🎩 Alfred's Response: -Certainly! Here’s a draft for your email to Dr. Nikola Tesla: - ---- - -Subject: Exciting Developments in Wireless Energy Transmission - -Dear Dr. Tesla, - -I hope this email finds you well. It’s been so long since we last caught up at the university, and I’ve been following your incredible work with great interest. I recently came across your latest patent on wireless energy transmission and I’m absolutely fascinated by the potential it holds for the future. - -I would love to discuss your recent advancements in more detail. Your innovative approach to solving some of the world’s most pressing energy challenges is truly inspiring. I believe there’s a lot we can learn from each other, and I’d be honored to hear more about your vision and the challenges you’ve faced during the development of this technology. - -On a lighter note, I remember you had a particular fondness for pigeons. I’ve been reading up on some recent studies on pigeon behavior and thought we could perhaps share some interesting insights on that topic as well. It would be a pleasure to catch up on both our professional and personal interests. - -Looking forward to your response. - -Best regards, - -[Your Name] - ---- - -Feel free to adjust the tone and content as you see fit! +Based on the provided information, here are key points to prepare for the conversation with 'Dr. Nikola Tesla' about recent advancements in wireless energy:\n1. **Wireless Power Transmission (WPT):** Discuss how WPT revolutionizes energy transfer by eliminating the need for cords and leveraging mechanisms like inductive and resonant coupling.\n2. **Advancements in Wireless Charging:** Highlight improvements in efficiency, faster charging speeds, and the rise of Qi/Qi2 certified wireless charging solutions.\n3. **5G-Advanced Innovations and NearLink Wireless Protocol:** Mention these as developments that enhance speed, security, and efficiency in wireless networks, which can support advanced wireless energy technologies.\n4. **AI and ML at the Edge:** Talk about how AI and machine learning will rely on wireless networks to bring intelligence to the edge, enhancing automation and intelligence in smart homes and buildings.\n5. **Matter, Thread, and Security Advancements:** Discuss these as key innovations that drive connectivity, efficiency, and security in IoT devices and systems.\n6. **Breakthroughs in Wireless Charging Technology:** Include any recent breakthroughs or studies, such as the one from Incheon National University, to substantiate the advancements in wireless charging. ``` @@ -518,16 +479,19 @@ print(response2) ```python -response = graph.invoke({"messages": [HumanMessage(content="Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?")]}) +# First interaction +response = alfred.invoke({"messages": [HumanMessage(content="Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?")]}) + print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) print() -response = graph.invoke({"messages": response["messages"] + [HumanMessage(content="What projects is she currently working on?")]}) +# Second interaction (referencing the first) +response = alfred.invoke({"messages": response["messages"] + [HumanMessage(content="What projects is she currently working on?")]}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index 5f8517c..d67bea1 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -15,9 +15,10 @@ This is where Retrieval Augmented Generation (RAG) shines! By combining a retrie ## Setting up our application -In this unit, we'll develop our agent within a structured Python project. This approach helps us maintain clean, modular code by organizing different functionalities into separate files. +In this unit, we'll develop our agent within a HF Space, as an structured Python project. This approach helps us maintain clean, modular code by organizing different functionalities into separate files. + +### Project Structure -### Project Structure - **`tools.py`** – Provides auxiliary tools for the agent. - **`retriever.py`** – Implements retrieval functions to support knowledge access. - **`app.py`** – Integrates all components into a fully functional agent, which we'll finalize in the last part of this unit. @@ -236,7 +237,7 @@ guest_info_tool = FunctionTool.from_defaults(get_guest_info_retriever) ``` Let's understand this tool step-by-step. -- The docstring help the agent understand when and how to use this tool +- The docstring helps the agent understand when and how to use this tool - The type decorators define what parameters the tool expects (in this case, a search query) - We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings - The method processes the query and returns the most relevant guest information @@ -272,7 +273,7 @@ guest_info_tool = Tool( ``` Let's understand this tool step-by-step. -- The docstring help the agent understand when and how to use this tool +- The `name` and `description` help the agent understand when and how to use this tool - The type decorators define what parameters the tool expects (in this case, a search query) - We're using a `BM25Retriever`, which is a powerful text retrieval algorithm that doesn't require embeddings - The method processes the query and returns the most relevant guest information @@ -355,85 +356,68 @@ What's happening in this final step: ```python -from typing import TypedDict, Annotated, Optional -from langchain.llms import HuggingFaceHub +from typing import TypedDict, Annotated from langgraph.graph.message import add_messages -from langgraph.graph import StateGraph from langchain_core.messages import AnyMessage, HumanMessage, AIMessage -import re +from langgraph.prebuilt import ToolNode +from langgraph.graph import START, StateGraph +from langgraph.prebuilt import tools_condition +from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace -class AgentState(TypedDict): - response: Optional[str] - messages: Annotated[list[AnyMessage], add_messages] - -llm = HuggingFaceHub( - repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", - huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN +# Generate the chat interface, including the tools +llm = HuggingFaceEndpoint( + repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", + huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN, ) -def tools_node(data): - messages = data["messages"] - query = messages[-1].content.strip() +chat = ChatHuggingFace(llm=llm, verbose=True) +tools = [guest_info_tool] +chat_with_tools = chat.bind_tools(tools) - guest_info_result = guest_info_tool.invoke(query) - guest_info_result_cleaned = guest_info_result.lower() - - match = re.search(r"'([^']+)'", query.lower()) - guest_name = match.group(1).lower() if match else "" - - if guest_name and guest_name in guest_info_result_cleaned: - response = guest_info_result - else: - response = "I'm sorry, I couldn't find that guest." - - return {"messages": messages + [AIMessage(content=response)]} - -def generate_response(data): - messages = data["messages"] - - if messages[-1].content == "No matching guest information found.": - return {"messages": messages + [AIMessage(content="I'm sorry, I couldn't find that guest.")]} - - history_text = "\n".join([msg.content for msg in messages]) - prompt = f"{history_text}\nUse only the info retrieved as source.\n" - - response_text = llm.invoke(prompt).strip() - - if response_text.startswith(prompt): - model_response = response_text[len(prompt):].strip() - - messages.append(AIMessage(content=model_response)) - - return {"messages": messages, "response": model_response} +# Generate the AgentState and Agent graph +class AgentState(TypedDict): + messages: Annotated[list[AnyMessage], add_messages] +def assistant(state: AgentState): + return { + "messages": [chat_with_tools.invoke(state["messages"])], + } +## The graph builder = StateGraph(AgentState) -builder.add_node("tools_node", tools_node) -builder.add_node("generate_response", generate_response) +# Define nodes: these do the work +builder.add_node("assistant", assistant) +builder.add_node("tools", ToolNode(tools)) -builder.set_entry_point("tools_node") -builder.add_edge("tools_node", "generate_response") +# Define edges: these determine how the control flow moves +builder.add_edge(START, "assistant") +builder.add_conditional_edges( + "assistant", + # If the latest message requires a tool, route to tools + # Otherwise, provide a direct response + tools_condition, +) +builder.add_edge("tools", "assistant") +alfred = builder.compile() -graph = builder.compile() - -query = "Tell me about our guest named 'Lady Ada Lovelace'." -response = graph.invoke({"messages": query}) +messages = [HumanMessage(content="Tell me about our guest named 'Lady Ada Lovelace'.")] +response = alfred.invoke({"messages": messages}) print("🎩 Alfred's Response:") -print(response['messages'][2].content) +print(messages['messages'][-1].content) ``` Expected output: ``` 🎩 Alfred's Response: -Based on the information provided, Lady Ada Lovelace is a notable mathematician and is celebrated for her significant contributions to the field of computing. She is particularly recognized as the first computer programmer for her work on Charles Babbage's Analytical Engine. Her relation to you is described as a friend, and her contact information includes the email address ada.lovelace@example.com. +Lady Ada Lovelace is an esteemed mathematician and pioneer in computing, often celebrated as the first computer programmer due to her work on Charles Babbage's Analytical Engine. ``` What's happening in this final step: -- We initialize a Hugging Face model using the `HuggingFaceHub` class -- We create our agent (Alfred) as a `StateGraph`, that combines 2 nodes (`search_guest`, `generate_response`) using an edge +- We initialize a Hugging Face model using the `HuggingFaceEndpoint` class. We also generate a chat interface and append the tools. +- We create our agent (Alfred) as a `StateGraph`, that combines 2 nodes (`assistant`, `tools`) using an edge - We ask Alfred to retrieve information about a guest named "Lady Ada Lovelace" diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 839ea3a..6fc8f0d 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -68,7 +68,7 @@ print(results) Expected output: ``` -The current President of France in Emmanuel Macron. +Emmanuel Macron (born December 21, 1977, Amiens, France) is a French banker and politician who was elected president of France in 2017... ``` @@ -138,8 +138,6 @@ def get_weather_info(location: str) -> str: # Initialize the tool weather_info_tool = FunctionTool.from_defaults(get_weather_info) - -print(weather_info_tool('Paris')) ``` @@ -161,13 +159,12 @@ def get_weather_info(location: str) -> str: data = random.choice(weather_conditions) return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" +# Initialize the tool weather_info_tool = Tool( name="get_weather_info", func=get_weather_info, description="Fetches dummy weather information for a given location." ) - -print(weather_info_tool.invoke('Paris')) ``` @@ -366,75 +363,63 @@ Facebook is a social networking service and technology company based in Menlo Pa ```python -from typing import TypedDict, Annotated, Optional -from langchain.llms import HuggingFaceHub +from typing import TypedDict, Annotated from langgraph.graph.message import add_messages -from langgraph.graph import StateGraph from langchain_core.messages import AnyMessage, HumanMessage, AIMessage +from langgraph.prebuilt import ToolNode +from langgraph.graph import START, StateGraph +from langgraph.prebuilt import tools_condition +from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace -class AgentState(TypedDict): - response: Optional[str] - messages: Annotated[list[AnyMessage], add_messages] - -llm = HuggingFaceHub( - repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", - huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN +# Generate the chat interface, including the tools +llm = HuggingFaceEndpoint( + repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", + huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN, ) -def tools_node(data): - messages = data["messages"] - query = messages[-1].content.strip() - - if "weather" in query: - location_match = re.search(r"weather.*in\s+([a-zA-Z\s]+)", query) - response = weather_info_tool.invoke(location_match.group(1).strip()) if location_match else "Location not found." - elif "hugging face" in query.lower(): - if "facebook" in query.lower(): response = hub_stats_tool.invoke("facebook") - elif "google" in query.lower(): response = hub_stats_tool.invoke("google") - else: response = "Org not found." - else: - response = search_tool.invoke(query) +chat = ChatHuggingFace(llm=llm, verbose=True) +tools = [search_tool, weather_info_tool, hub_stats_tool] +chat_with_tools = chat.bind_tools(tools) - return {"messages": messages + [AIMessage(content=response)]} - - -def generate_response(data): - messages = data["messages"] - - history_text = "\n".join([msg.content for msg in messages]) - prompt = f"{history_text}\nUse only the info retrieved as source.\n" - - response_text = llm.invoke(prompt).strip() - - if response_text.startswith(prompt): - model_response = response_text[len(prompt):].strip() - - messages.append(AIMessage(content=model_response)) - - return {"messages": messages, "response": model_response} +# Generate the AgentState and Agent graph +class AgentState(TypedDict): + messages: Annotated[list[AnyMessage], add_messages] +def assistant(state: AgentState): + return { + "messages": [chat_with_tools.invoke(state["messages"])], + } +## The graph builder = StateGraph(AgentState) -builder.add_node("tools_node", tools_node) -builder.add_node("generate_response", generate_response) +# Define nodes: these do the work +builder.add_node("assistant", assistant) +builder.add_node("tools", ToolNode(tools)) -builder.set_entry_point("tools_node") -builder.add_edge("tools_node", "generate_response") +# Define edges: these determine how the control flow moves +builder.add_edge(START, "assistant") +builder.add_conditional_edges( + "assistant", + # If the latest message requires a tool, route to tools + # Otherwise, provide a direct response + tools_condition, +) +builder.add_edge("tools", "assistant") +alfred = builder.compile() -graph = builder.compile() +messages = [HumanMessage(content="Who is Facebook and what's their most popular model?")] +response = alfred.invoke({"messages": messages}) -# Example query Alfred might receive during the gala -response = graph.invoke({"messages": "What is Facebook and what's their most popular model on the Hugging Face Hub?"}) print("🎩 Alfred's Response:") -print(response["response"]) +print(response['messages'][-1].content) ``` Expected output: ``` 🎩 Alfred's Response: -According to the company, Meta AI is set to become the world's most widely used AI assistant by the end of the year. Over 400 million people interact with Meta AI monthly, with 185 million using ... Designed to make AI accessible to everyone, the AI model is built on Meta's open-source Llama 3.2 and works across the company's ecosystem allowing users to create, interact, and benefit from AI ... We tried seven of the most popular AI chatbots (ChatGPT, Claude, Grok, Perplexity and Arc Search, Meta AI, Microsoft Copilot) to find out. ... It uses a mix of models to produce its responses ... Yet, the difference is that Meta AI, the chatbot, integrated across Meta's social media platforms, has surpassed 500 million users since its wide release in April 2024. The rise of Meta AI. Meta AI's ascent to 500 million users in just seven months is a testament to its accessibility and utility. Impact of Meta AI on the Metaverse and VR/AR; History of Meta AI. Meta AI started as Facebook AI, focusing on AI research and innovation. In 2021, when Facebook rebranded to Meta, the scope of AI expanded further. Meta AI has made significant advancements in NLP (Natural Language Processing), computer vision, and deep learning. Use Cases of Meta AI +Facebook is a social media company known for its social networking site, Facebook, as well as other services like Instagram and WhatsApp. The most downloaded model by Facebook on the Hugging Face Hub is facebook/esmfold_v1 with 13,202,321 downloads. ``` From 835838d03caa9fd070a1cae82832ec67899692d4 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 13:39:14 +0100 Subject: [PATCH 38/46] Improved smolagents tab --- units/en/unit3/agentic-rag/agent.mdx | 11 +++++------ units/en/unit3/agentic-rag/invitees.mdx | 1 + units/en/unit3/agentic-rag/tools.mdx | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 332c3e6..bfdf5d2 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -168,7 +168,7 @@ Let's see how Alfred can help us with our guest information. ```python -query = "Tell me about Lady Ada Lovelace. What's her background and how is she related to me?" +query = "Tell me about 'Lady Ada Lovelace'" response = alfred.run(query) print("🎩 Alfred's Response:") @@ -204,7 +204,7 @@ Lady Ada Lovelace was an English mathematician and writer, best known for her wo ```python -response = alfred.invoke({"messages": "Tell me about 'Lady Ada Lovelace'. What's her background and how is she related to me?"}) +response = alfred.invoke({"messages": "Tell me about 'Lady Ada Lovelace'"}) print("🎩 Alfred's Response:") print(response['messages'][-1].content) @@ -214,7 +214,7 @@ Expected output: ``` 🎩 Alfred's Response: -Ada Lovelace was an English mathematician and writer, known for her work on Charles Babbage's Analytical Engine, which is considered a precursor to the modern computer. She is recognized as the first computer programmer because of her program for calculating Bernoulli numbers. Born as Augusta Ada King, Countess of Lovelace, she married William King, 8th Baron King, in 1835, becoming Lady Lovelace. They had three children together. Ada Lovelace Day is celebrated annually to honor women's contributions to science and technology. However, there is no direct relation provided in the information suggesting a connection to you. +Ada Lovelace, also known as Augusta Ada King, Countess of Lovelace, was an English mathematician and writer. Born on December 10, 1815, and passing away on November 27, 1852, she is renowned for her work on Charles Babbage's Analytical Engine, a proposed mechanical general-purpose computer. Ada Lovelace is celebrated as one of the first computer programmers because she created a program for the Analytical Engine in 1843. She recognized that the machine could be used for more than mere calculation, envisioning its potential in a way that few did at the time. Her contributions to the field of computer science laid the groundwork for future developments. A day in October, designated as Ada Lovelace Day, honors women's contributions to science and technology, inspired by Lovelace's pioneering work. ``` @@ -287,7 +287,7 @@ Let's see how Alfred can help us impress AI researchers. ```python -query = "One of our guests is from Anthropic. What can you tell me about their most popular model?" +query = "One of our guests is from Qwen. What can you tell me about their most popular model?" response = alfred.run(query) print("🎩 Alfred's Response:") @@ -298,8 +298,7 @@ Expected output: ``` 🎩 Alfred's Response: -I've looked up information about Anthropic's models. The most downloaded model by Anthropic is claude-3-haiku-20240307, which has received over 1,200,000 downloads. This is their lightweight, fastest, and most efficient model in the Claude 3 family. It would be a great conversation starter to mention this to your guests from Anthropic, perhaps asking them about their latest research or the capabilities of their Claude models. -``` +The most popular Qwen model is Qwen/Qwen2.5-VL-7B-Instruct with 3,313,345 downloads.``` diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index d67bea1..b974bcf 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -190,6 +190,7 @@ class GuestInfoRetrieverTool(Tool): output_type = "string" def __init__(self, docs): + self.is_initialized = False self.retriever = BM25Retriever.from_documents(docs) def forward(self, query: str): diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index 6fc8f0d..aef7400 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -316,7 +316,7 @@ alfred = CodeAgent( ) # Example query Alfred might receive during the gala -response = alfred.run("Who is Meta AI and what's their most popular model?") +response = alfred.run("What is Facebook and what's their most popular model?") print("🎩 Alfred's Response:") print(response) @@ -326,8 +326,7 @@ Expected output: ``` 🎩 Alfred's Response: -Meta or previously facebook is an American multinational technology conglomerate. -The most downloaded model by facebook is facebook/esmfold_v1 with 12.195.721 downloads. +Facebook is a social networking website where users can connect, share information, and interact with others. The most downloaded model by Facebook on the Hugging Face Hub is ESMFold_v1. ``` From 75d23c24cb5ad58b6d36b1939ebb05ea277881f6 Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 13:44:43 +0100 Subject: [PATCH 39/46] Reviewed for LlamaIndex --- units/en/unit3/agentic-rag/agent.mdx | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index bfdf5d2..04e5696 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -61,6 +61,9 @@ alfred = CodeAgent( # Import necessary libraries from llama_index.core.agent.workflow import AgentWorkflow from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI + +from tools import search_tool, weather_info_tool, hub_stats_tool +from retriever import guest_info_tool ``` Now, let's combine all these tools into a single agent: @@ -69,19 +72,6 @@ Now, let's combine all these tools into a single agent: # Initialize the Hugging Face model llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct") -# Initialize the web search tool -tool_spec = DuckDuckGoSearchToolSpec() -search_tool = FunctionTool.from_defaults(tool_spec.duckduckgo_full_search) - -# Initialize the weather tool -weather_info_tool = FunctionTool.from_defaults(get_weather_info) - -# Initialize the Hub stats tool -hub_stats_tool = FunctionTool.from_defaults(get_hub_stats) - -# Load the guest dataset and initialize the guest info tool -guest_info_tool = FunctionTool.from_defaults(get_guest_info_retriever) - # Create Alfred with all the tools alfred = AgentWorkflow.from_tools_or_functions( [guest_info_tool, search_tool, weather_info_tool, hub_stats_tool], From a12919de2ef4a474564fcde509fbf1f8a5b8fdfd Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 13:55:58 +0100 Subject: [PATCH 40/46] Updated typos --- units/en/unit3/agentic-rag/agent.mdx | 2 +- units/en/unit3/agentic-rag/gala.mdx | 2 +- units/en/unit3/agentic-rag/invitees.mdx | 6 +++--- units/en/unit3/agentic-rag/tools.mdx | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/units/en/unit3/agentic-rag/agent.mdx b/units/en/unit3/agentic-rag/agent.mdx index 04e5696..18437a5 100644 --- a/units/en/unit3/agentic-rag/agent.mdx +++ b/units/en/unit3/agentic-rag/agent.mdx @@ -9,7 +9,7 @@ In this section, we'll combine the guest information retrieval, web search, weat Instead of reimplementing all the tools we've created in previous sections, we'll import them from their respective modules which we saved in the `tools.py` and `retriever.py` files. -If you haven't implemented the tools yet, go back to the tools and retriever sections to implement them, and add them to the `tools.py` and `retriever.py` files. +If you haven't implemented the tools yet, go back to the tools and retriever sections to implement them, and add them to the tools.py and retriever.py files. Let's import the necessary libraries and tools from the previous sections: diff --git a/units/en/unit3/agentic-rag/gala.mdx b/units/en/unit3/agentic-rag/gala.mdx index ec6655b..c98711b 100644 --- a/units/en/unit3/agentic-rag/gala.mdx +++ b/units/en/unit3/agentic-rag/gala.mdx @@ -4,7 +4,7 @@ Now, it's time to get our hands dirty with an actual use case. Let's set the sta **You decided to host the most extravagant and opulent party of the century.** This means lavish feasts, enchanting dancers, renowned DJs, exquisite drinks, a breathtaking fireworks display, and much more. -Alfred, your friendly neighbourhood agent, is getting ready to watch over all of your needs for this party, and **Alfred is going to manage everything himself**. To do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts and much more! +Alfred, your friendly neighbourhood agent, is getting ready to watch over all of your needs for this party, and **Alfred is going to manage everything himself**. To do so, he needs to have access to all of the information about the party, including the menu, the guests, the schedule, weather forecasts, and much more! Not only that, but he also needs to make sure that the party is going to be a success, so **he needs to be able to answer any questions about the party during the party**, while also being able to handle any unexpected situations that may arise. diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index b974bcf..e2e394a 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -8,14 +8,14 @@ Alfred, your trusted agent, is preparing for the most extravagant gala of the ce Imagine Alfred mingling among the guests, needing to recall specific details about each person at a moment's notice. A traditional LLM might struggle with this task because: 1. The guest list is specific to your event and not in the model's training data -2. Guest information may change or update frequently +2. Guest information may change or be updated frequently 3. Alfred needs to retrieve precise details like email addresses This is where Retrieval Augmented Generation (RAG) shines! By combining a retrieval system with an LLM, Alfred can access accurate, up-to-date information about your guests on demand. ## Setting up our application -In this unit, we'll develop our agent within a HF Space, as an structured Python project. This approach helps us maintain clean, modular code by organizing different functionalities into separate files. +In this unit, we'll develop our agent within a HF Space, as a structured Python project. This approach helps us maintain clean, modular code by organizing different functionalities into separate files. ### Project Structure @@ -452,7 +452,7 @@ Now that Alfred can retrieve guest information, consider how you might enhance t Now Alfred is fully equipped to handle guest inquiries effortlessly, ensuring your gala is remembered as the most sophisticated and delightful event of the century! - + Try extending the retriver tool to also return conversation starters based on each guest's interests or background. How would you modify the tool to accomplish this? When you're done, implement your guest retriever tool in the retriever.py file. diff --git a/units/en/unit3/agentic-rag/tools.mdx b/units/en/unit3/agentic-rag/tools.mdx index aef7400..4e96bba 100644 --- a/units/en/unit3/agentic-rag/tools.mdx +++ b/units/en/unit3/agentic-rag/tools.mdx @@ -427,8 +427,8 @@ Facebook is a social media company known for its social networking site, Faceboo By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala. - + Try implementing a tool that can be used to get the latest news about a specific topic. -When you're done, implement your custom tools in the tools.py` file. +When you're done, implement your custom tools in the tools.py file. \ No newline at end of file From da2c3aef724aaaf7bfcaabf254582c2abd5e425e Mon Sep 17 00:00:00 2001 From: sergiopaniego Date: Thu, 27 Mar 2025 14:06:58 +0100 Subject: [PATCH 41/46] Updated Org Names --- units/en/unit3/agentic-rag/invitees.mdx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/units/en/unit3/agentic-rag/invitees.mdx b/units/en/unit3/agentic-rag/invitees.mdx index e2e394a..5f3c251 100644 --- a/units/en/unit3/agentic-rag/invitees.mdx +++ b/units/en/unit3/agentic-rag/invitees.mdx @@ -23,12 +23,12 @@ In this unit, we'll develop our agent within a HF Space, as a structured Python - **`retriever.py`** – Implements retrieval functions to support knowledge access. - **`app.py`** – Integrates all components into a fully functional agent, which we'll finalize in the last part of this unit. -For a hands-on reference, check out [this HF Space](https://huggingface.co/spaces/sergiopaniego/AgenticRAG), where the Agentic RAG developed in this unit is live. Feel free to clone it and experiment! +For a hands-on reference, check out [this HF Space](https://huggingface.co/spaces/agents-course/Unit_3_Agentic_RAG), where the Agentic RAG developed in this unit is live. Feel free to clone it and experiment! You can directly test the agent below: