Skip to content

Commit

Permalink
Update langchain tools integration notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Dec 17, 2024
1 parent f7b8b8b commit 3d759ee
Showing 1 changed file with 89 additions and 21 deletions.
110 changes: 89 additions & 21 deletions notebook/tools_langchain_tools_integration.ipynb
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integrating LangChain Tools with the AG2 Framework\n",
"\n",
"In this tutorial, we demonstrate how to integrate [LangChain Tools](https://python.langchain.com/v0.1/docs/modules/tools) into the AG2 framework. This process enables smooth interoperability between the two systems, allowing developers to leverage LangChain's powerful tools within AG2's flexible agent-based architecture. By the end of this guide, you will understand how to configure agents, convert LangChain tools for use in AG2, and validate the integration with a practical example.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation\n",
"To integrate LangChain tools into the AG2 framework, install the required dependencies:\n",
"\n",
"```bash\n",
"pip install ag2[interop-langchain]\n",
"```\n",
"\n",
"Additionally, this notebook uses LangChain's [Wikipedia Tool](https://api.python.langchain.com/en/latest/tools/langchain_community.tools.wikipedia.tool.WikipediaQueryRun.html), which requires the `wikipedia` package. Install it with:\n",
"\n",
"```bash\n",
"pip install wikipedia\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# ! pip install wikipedia"
"## Imports\n",
"\n",
"Import necessary modules and tools.\n",
"- `os`: For accessing environment variables.\n",
"- `WikipediaQueryRun` and `WikipediaAPIWrapper`: Tools for querying Wikipedia.\n",
"- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the Autogen framework.\n",
"- `LangchainInteroperability`: A bridge for integrating LangChain tools with the Autogen framework."
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,9 +55,21 @@
"from autogen.interoperability.langchain import LangchainInteroperability"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Agent Configuration\n",
"\n",
"Configure the agents for the interaction.\n",
"- `config_list` defines the LLM configurations, including the model and API key.\n",
"- `UserProxyAgent` simulates user inputs without requiring actual human interaction (set to `NEVER`).\n",
"- `AssistantAgent` represents the AI agent, configured with the LLM settings."
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -42,32 +85,50 @@
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tool Integration\n",
"\n",
"- Initialize and register the LangChain tool with AG2.\n",
"- `WikipediaAPIWrapper`: Configured to fetch the top 1 result from Wikipedia with a maximum of 1000 characters per document.\n",
"- `WikipediaQueryRun`: A LangChain tool that executes Wikipedia queries.\n",
"- `LangchainInteroperability`: Converts the LangChain tool into a format compatible with the AG2 framework.\n",
"- `ag2_tool.register_for_execution(user_proxy)`: Registers the tool for use by the user_proxy agent.\n",
"- `ag2_tool.register_for_llm(chatbot)`: Registers the tool for integration with the chatbot agent.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)\n",
"langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n",
"\n",
"langchain_interop = LangchainInteroperability()\n",
"ag2_tool = langchain_interop.convert_tool(langchain_tool)\n"
"ag2_tool = langchain_interop.convert_tool(langchain_tool)\n",
"\n",
"ag2_tool.register_for_execution(user_proxy)\n",
"ag2_tool.register_for_llm(chatbot)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"ag2_tool.register_for_execution(user_proxy)\n",
"ag2_tool.register_for_llm(chatbot)"
"## Wikipedia Browsing\n",
"\n",
"- `user_proxy` queries the `chatbot`, which uses a Wikipedia tool to retrieve information.\n",
"- The `chatbot` identifies the query's intent and fetches a summary from Wikipedia.\n",
"- Tool execution returns a concise response from the relevant Wikipedia page."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -94,30 +155,30 @@
"\u001b[32m***** Response from calling tool (call_gBcszkAyMnQ5z1InEf4aEUmn) *****\u001b[0m\n",
"Page: History of the United States\n",
"Summary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.\n",
"In 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. George Washington, the first president, and his adviser Alexander Hamilton cr\n",
"In 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. Washington, the first president, and his adviser Alexander Hamilton created a\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\n",
"The history of the United States began with the arrival of Native Americans in North America around 15,000 BC. European colonization of North America started in the late 15th century, which severely affected Indigenous societies through wars and epidemics.\n",
"The history of the United States started with the arrival of the first people in the Americas around 15,000 BC. The European colonization of North America began in the late 15th century, which significantly impacted Indigenous societies through wars and epidemics. By the 1760s, thirteen British colonies had been established. The Southern Colonies developed an agricultural system reliant on slave labor, bringing millions of enslaved Africans to America.\n",
"\n",
"By the 1760s, thirteen British colonies had been established. The Southern Colonies developed an economy heavily reliant on slave labor, enslaving millions of people brought from Africa. The British victory over France led to increased British taxation of the colonies, leading to resistance from the colonists, exemplified by events like the Boston Tea Party in 1773.\n",
"Tensions rose between the colonies and British Parliament due to a series of taxes, with the Boston Tea Party in 1773 marking a major protest against these impositions. This led to the Intolerable Acts aimed at suppressing colonial self-governance.\n",
"\n",
"The growing tensions resulted in the American colonies declaring independence in 1776. With General George Washington leading the Continental Army, the United States won the Revolutionary War against Britain in 1783. The U.S. Constitution was adopted in 1789, establishing the foundation of the federal government, and the Bill of Rights was added in 1791 to ensure individual liberties. George Washington became the nation’s first president, with Alexander Hamilton among his key advisers.\n",
"In 1776, the United States declared independence. Under the leadership of General George Washington, the U.S. emerged victorious in the Revolutionary War by 1783. The U.S. Constitution was adopted in 1789, followed by the Bill of Rights in 1791, which secured fundamental rights for its citizens. Washington became the first president, with Alexander Hamilton as a significant adviser, laying the country's early governmental foundations. \n",
"\n",
"This marks the early formation and foundation of the United States as an independent nation. If you need more detailed information, let me know! TERMINATE\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
},
{
"data": {
"text/plain": [
"ChatResult(chat_id=None, chat_history=[{'content': 'Tell me about the history of the United States', 'role': 'assistant', 'name': 'User'}, {'tool_calls': [{'id': 'call_gBcszkAyMnQ5z1InEf4aEUmn', 'function': {'arguments': '{\"tool_input\":{\"query\":\"History of the United States\"}}', 'name': 'wikipedia'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}, {'content': 'Page: History of the United States\\nSummary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.\\nIn 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. George Washington, the first president, and his adviser Alexander Hamilton cr', 'tool_responses': [{'tool_call_id': 'call_gBcszkAyMnQ5z1InEf4aEUmn', 'role': 'tool', 'content': 'Page: History of the United States\\nSummary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.\\nIn 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. George Washington, the first president, and his adviser Alexander Hamilton cr'}], 'role': 'tool', 'name': 'User'}, {'content': 'The history of the United States began with the arrival of Native Americans in North America around 15,000 BC. European colonization of North America started in the late 15th century, which severely affected Indigenous societies through wars and epidemics.\\n\\nBy the 1760s, thirteen British colonies had been established. The Southern Colonies developed an economy heavily reliant on slave labor, enslaving millions of people brought from Africa. The British victory over France led to increased British taxation of the colonies, leading to resistance from the colonists, exemplified by events like the Boston Tea Party in 1773.\\n\\nThe growing tensions resulted in the American colonies declaring independence in 1776. With General George Washington leading the Continental Army, the United States won the Revolutionary War against Britain in 1783. The U.S. Constitution was adopted in 1789, establishing the foundation of the federal government, and the Bill of Rights was added in 1791 to ensure individual liberties. George Washington became the nation’s first president, with Alexander Hamilton among his key advisers.\\n\\nThis marks the early formation and foundation of the United States as an independent nation. If you need more detailed information, let me know! TERMINATE', 'role': 'user', 'name': 'chatbot'}], summary='The history of the United States began with the arrival of Native Americans in North America around 15,000 BC. European colonization of North America started in the late 15th century, which severely affected Indigenous societies through wars and epidemics.\\n\\nBy the 1760s, thirteen British colonies had been established. The Southern Colonies developed an economy heavily reliant on slave labor, enslaving millions of people brought from Africa. The British victory over France led to increased British taxation of the colonies, leading to resistance from the colonists, exemplified by events like the Boston Tea Party in 1773.\\n\\nThe growing tensions resulted in the American colonies declaring independence in 1776. With General George Washington leading the Continental Army, the United States won the Revolutionary War against Britain in 1783. The U.S. Constitution was adopted in 1789, establishing the foundation of the federal government, and the Bill of Rights was added in 1791 to ensure individual liberties. George Washington became the nation’s first president, with Alexander Hamilton among his key advisers.\\n\\nThis marks the early formation and foundation of the United States as an independent nation. If you need more detailed information, let me know! ', cost={'usage_including_cached_inference': {'total_cost': 0.00601, 'gpt-4o-2024-08-06': {'cost': 0.00601, 'prompt_tokens': 1356, 'completion_tokens': 262, 'total_tokens': 1618}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
"ChatResult(chat_id=None, chat_history=[{'content': 'Tell me about the history of the United States', 'role': 'assistant', 'name': 'User'}, {'tool_calls': [{'id': 'call_gBcszkAyMnQ5z1InEf4aEUmn', 'function': {'arguments': '{\"tool_input\":{\"query\":\"History of the United States\"}}', 'name': 'wikipedia'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}, {'content': 'Page: History of the United States\\nSummary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.\\nIn 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. Washington, the first president, and his adviser Alexander Hamilton created a', 'tool_responses': [{'tool_call_id': 'call_gBcszkAyMnQ5z1InEf4aEUmn', 'role': 'tool', 'content': 'Page: History of the United States\\nSummary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.\\nIn 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. Washington, the first president, and his adviser Alexander Hamilton created a'}], 'role': 'tool', 'name': 'User'}, {'content': \"The history of the United States started with the arrival of the first people in the Americas around 15,000 BC. The European colonization of North America began in the late 15th century, which significantly impacted Indigenous societies through wars and epidemics. By the 1760s, thirteen British colonies had been established. The Southern Colonies developed an agricultural system reliant on slave labor, bringing millions of enslaved Africans to America.\\n\\nTensions rose between the colonies and British Parliament due to a series of taxes, with the Boston Tea Party in 1773 marking a major protest against these impositions. This led to the Intolerable Acts aimed at suppressing colonial self-governance.\\n\\nIn 1776, the United States declared independence. Under the leadership of General George Washington, the U.S. emerged victorious in the Revolutionary War by 1783. The U.S. Constitution was adopted in 1789, followed by the Bill of Rights in 1791, which secured fundamental rights for its citizens. Washington became the first president, with Alexander Hamilton as a significant adviser, laying the country's early governmental foundations. \\n\\nTERMINATE\", 'role': 'user', 'name': 'chatbot'}], summary=\"The history of the United States started with the arrival of the first people in the Americas around 15,000 BC. The European colonization of North America began in the late 15th century, which significantly impacted Indigenous societies through wars and epidemics. By the 1760s, thirteen British colonies had been established. The Southern Colonies developed an agricultural system reliant on slave labor, bringing millions of enslaved Africans to America.\\n\\nTensions rose between the colonies and British Parliament due to a series of taxes, with the Boston Tea Party in 1773 marking a major protest against these impositions. This led to the Intolerable Acts aimed at suppressing colonial self-governance.\\n\\nIn 1776, the United States declared independence. Under the leadership of General George Washington, the U.S. emerged victorious in the Revolutionary War by 1783. The U.S. Constitution was adopted in 1789, followed by the Bill of Rights in 1791, which secured fundamental rights for its citizens. Washington became the first president, with Alexander Hamilton as a significant adviser, laying the country's early governmental foundations. \\n\\n\", cost={'usage_including_cached_inference': {'total_cost': 0.00589, 'gpt-4o-2024-08-06': {'cost': 0.00589, 'prompt_tokens': 1356, 'completion_tokens': 250, 'total_tokens': 1606}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
]
},
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -126,6 +187,13 @@
"message = \"Tell me about the history of the United States\"\n",
"user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 3d759ee

Please sign in to comment.