Skip to content

Commit

Permalink
Fix toctree and multiagent rag system
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-roucher committed Jan 6, 2025
1 parent 7a81cb9 commit b75779e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
2 changes: 0 additions & 2 deletions notebooks/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@
title: Build an agent with tool-calling superpowers using Transformers Agents
- local: agent_rag
title: Agentic RAG - turbocharge your RAG with query reformulation and self-query
- local: agent_change_llm
title: Create a Transformers Agent from any LLM inference provider
- local: agent_text_to_sql
title: Agent for Text-to-SQL with automatic error correction
- local: agent_data_analyst
Expand Down
79 changes: 50 additions & 29 deletions notebooks/en/multiagent_rag_system.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8rVK5wUFrNUI",
"outputId": "acfadab7-6fbf-4b8e-e6ce-7597a1b9687d"
},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
"To disable this warning, you can either:\n",
"\t- Avoid using `tokenizers` before the fork if possible\n",
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
]
}
],
"source": [
"!pip install -q git+https://github.com/huggingface/transformers.git#egg=transformers[agents]"
"!pip install -q smolagents"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "62DKn6XJB0MW",
"outputId": "c03b6378-fac9-466b-941b-590b2a401aa6"
},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
"To disable this warning, you can either:\n",
"\t- Avoid using `tokenizers` before the fork if possible\n",
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
]
}
],
"source": [
"!pip install markdownify duckduckgo-search spaces gradio-tools langchain langchain-community langchain-huggingface faiss-cpu --upgrade -q"
]
Expand Down Expand Up @@ -150,7 +172,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -160,10 +182,10 @@
},
"outputs": [],
"source": [
"from transformers.agents import HfApiEngine\n",
"from smolagents import HfApiModel\n",
"\n",
"llm_model = \"Qwen/Qwen2.5-72B-Instruct\"\n",
"llm_engine = HfApiEngine(llm_model)"
"model_id = \"Qwen/Qwen2.5-72B-Instruct\"\n",
"model = HfApiModel(model_id)"
]
},
{
Expand Down Expand Up @@ -203,9 +225,9 @@
"source": [
"### 2.1.1 Build our multi-tool web agent 🤖\n",
"\n",
"Now that we've set up the basic search and webpage tools, let's build our **multi-tool web agent**. This agent will combine several tools to perform more complex tasks, leveraging the capabilities of the `ReactJsonAgent`.\n",
"Now that we've set up the basic search and webpage tools, let's build our **multi-tool web agent**. This agent will combine several tools to perform more complex tasks, leveraging the capabilities of the `ToolCallingAgent`.\n",
"\n",
"The `ReactJsonAgent` is particularly well-suited for web search tasks because its JSON action formulation requires only simple arguments and works seamlessly in sequential chains of single actions. This makes it an excellent choice for scenarios where we need to search the web for relevant information and retrieve detailed content from specific web pages. In contrast, `CodeAgent` action formulation is better suited for scenarios involving numerous or parallel tool calls.\n",
"The `ToolCallingAgent` is particularly well-suited for web search tasks because its JSON action formulation requires only simple arguments and works seamlessly in sequential chains of single actions. This makes it an excellent choice for scenarios where we need to search the web for relevant information and retrieve detailed content from specific web pages. In contrast, `CodeAgent` action formulation is better suited for scenarios involving numerous or parallel tool calls.\n",
"\n",
"By integrating multiple tools, we can ensure that our agent interacts with the web in a sophisticated and efficient manner.\n",
"\n",
Expand All @@ -215,18 +237,17 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"metadata": {
"id": "NVnfGll5B0Ma"
},
"outputs": [],
"source": [
"from transformers.agents import ReactCodeAgent, ReactJsonAgent, ManagedAgent\n",
"from transformers.agents.search import DuckDuckGoSearchTool, VisitWebpageTool\n",
"from smolagents import CodeAgent, ToolCallingAgent, ManagedAgent, DuckDuckGoSearchTool, VisitWebpageTool\n",
"\n",
"web_agent = ReactJsonAgent(\n",
"web_agent = ToolCallingAgent(\n",
" tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],\n",
" llm_engine=llm_engine\n",
" model=model\n",
")"
]
},
Expand All @@ -241,15 +262,15 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"metadata": {
"id": "igUZsVP3B0Mb"
},
"outputs": [],
"source": [
"managed_web_agent = ManagedAgent(\n",
" agent=web_agent,\n",
" name=\"search\",\n",
" name=\"search_agent\",\n",
" description=\"Runs web searches for you. Give it your query as an argument.\",\n",
")"
]
Expand Down Expand Up @@ -296,7 +317,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
Expand Down Expand Up @@ -682,8 +703,8 @@
},
"outputs": [],
"source": [
"retriever_agent = ReactJsonAgent(\n",
" tools=[huggingface_doc_retriever_tool, peft_issues_retriever_tool], llm_engine=llm_engine, max_iterations=4, verbose=2\n",
"retriever_agent = ToolCallingAgent(\n",
" tools=[huggingface_doc_retriever_tool, peft_issues_retriever_tool], model=model, max_iterations=4, verbose=2\n",
")"
]
},
Expand All @@ -697,7 +718,7 @@
"source": [
"managed_retriever_agent = ManagedAgent(\n",
" agent=retriever_agent,\n",
" name=\"retriever\",\n",
" name=\"retriever_agent\",\n",
" description=\"Retrieves documents from the knowledge base for you that are close to the input query. Give it your query as an argument. The knowledge base includes Hugging Face documentation and PEFT issues.\",\n",
")"
]
Expand Down Expand Up @@ -773,8 +794,8 @@
},
"outputs": [],
"source": [
"image_generation_tool = load_tool(\"m-ric/text-to-image\", cache=False)\n",
"image_generation_agent = CodeAgent(tools=[prompt_generator_tool, image_generation_tool], llm_engine=llm_engine)"
"image_generation_tool = load_tool(\"m-ric/text-to-image\", trust_remote_code=True)\n",
"image_generation_agent = CodeAgent(tools=[prompt_generator_tool, image_generation_tool], model=model)"
]
},
{
Expand All @@ -796,7 +817,7 @@
"source": [
"managed_image_generation_agent = ManagedAgent(\n",
" agent=image_generation_agent,\n",
" name=\"image_generation\",\n",
" name=\"image_generation_agent\",\n",
" description=\"Generates images from text prompts. Give it your prompt as an argument.\",\n",
" additional_prompting=\"\\n\\nYour final answer MUST BE only the generated image location.\"\n",
")"
Expand Down Expand Up @@ -828,9 +849,9 @@
},
"outputs": [],
"source": [
"manager_agent = ReactCodeAgent(\n",
"manager_agent = CodeAgent(\n",
" tools=[],\n",
" llm_engine=llm_engine,\n",
" model=model,\n",
" managed_agents=[managed_web_agent, managed_retriever_agent, managed_image_generation_agent],\n",
" additional_authorized_imports=[\"time\", \"datetime\", \"PIL\"],\n",
")"
Expand Down Expand Up @@ -1551,9 +1572,9 @@
"provenance": []
},
"kernelspec": {
"display_name": "cookbook2",
"display_name": "test2",
"language": "python",
"name": "cookbook2"
"name": "test2"
},
"language_info": {
"codemirror_mode": {
Expand Down
4 changes: 1 addition & 3 deletions notebooks/zh-CN/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@
- local: agents
title: 使用 Transformers Agents 构建具有工具调用超能力的智能体
- local: agent_rag
title: 智能体 RAG 通过查询重构和自查询来增强你的 RAG
- local: agent_change_llm
title: 从任意的 LLM 推理提供商中创建一个 Transformers 智能体
title: 智能体 RAG 通过查询重构和自查询来增强你的 RAG

- title: 企业级使用指南 (Cookbook)
isExpanded: True
Expand Down

0 comments on commit b75779e

Please sign in to comment.