Skip to content

Commit

Permalink
Merge pull request #219 from rjambrecic/add-langchain
Browse files Browse the repository at this point in the history
Add langchain tools support
  • Loading branch information
davorrunje authored Dec 17, 2024
2 parents 296ce69 + d2f0c53 commit 2efbb51
Show file tree
Hide file tree
Showing 13 changed files with 1,292 additions and 1,139 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- name: Install packages and dependencies
run: |
python -m pip install --upgrade pip wheel
pip install -e .[test,cosmosdb,interop-crewai]
pip install -e .[test,cosmosdb,interop-crewai,interop-langchain]
python -c "import autogen"
pip install pytest-cov>=5 mock
- name: Install optional dependencies for code executors
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/openai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
if: matrix.python-version == '3.9'
run: |
pip install docker
pip install -e .[redis,interop-crewai]
pip install -e .[redis,interop-crewai,interop-langchain]
- name: Coverage
if: matrix.python-version == '3.9'
env:
Expand Down
19 changes: 19 additions & 0 deletions autogen/interoperability/langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

import sys

if sys.version_info < (3, 9):
raise ImportError("This submodule is only supported for Python versions 3.9 and above")

try:
import langchain.tools
except ImportError:
raise ImportError(
"Please install `interop-langchain` extra to use this module:\n\n\tpip install ag2[interop-langchain]"
)

from .langchain import LangchainInteroperability

__all__ = ["LangchainInteroperability"]
30 changes: 30 additions & 0 deletions autogen/interoperability/langchain/langchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

from typing import Any

from langchain_core.tools import BaseTool as LangchainTool

from ...tools import Tool
from ..interoperability import Interoperable

__all__ = ["LangchainInteroperability"]


class LangchainInteroperability(Interoperable):
def convert_tool(self, tool: Any) -> Tool:
if not isinstance(tool, LangchainTool):
raise ValueError(f"Expected an instance of `langchain_core.tools.BaseTool`, got {type(tool)}")

# needed for type checking
langchain_tool: LangchainTool = tool # type: ignore[no-any-unimported]

def func(tool_input: langchain_tool.args_schema) -> Any: # type: ignore[no-any-unimported]
return langchain_tool.run(tool_input.model_dump())

return Tool(
name=langchain_tool.name,
description=langchain_tool.description,
func=func,
)
1,855 changes: 928 additions & 927 deletions notebook/autobuild_agent_library.ipynb

Large diffs are not rendered by default.

217 changes: 8 additions & 209 deletions notebook/tools_crewai_tools_integration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"2024-12-16 22:31:29,213 - 139887720012672 - __init__.py-__init__:18 - WARNING: flaml.automl is not available. Please install flaml[automl] to enable AutoML functionalities.\n"
]
}
],
"outputs": [],
"source": [
"import os\n",
"from pathlib import Path\n",
Expand Down Expand Up @@ -134,48 +124,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mUser\u001b[0m (to chatbot):\n",
"\n",
"Write 'Easy Migration :)' into tool_result.txt.\n",
"Use /tmp/tmpt6zkd1db dir.\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\n",
"\u001b[32m***** Suggested tool call (call_UR57tA4Dr5YF0aLInnRGa8eD): File_Writer_Tool *****\u001b[0m\n",
"Arguments: \n",
"{\"args\":{\"filename\":\"tool_result.txt\",\"directory\":\"/tmp/tmpt6zkd1db\",\"content\":\"Easy Migration :)\",\"overwrite\":\"True\"}}\n",
"\u001b[32m*********************************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION File_Writer_Tool...\u001b[0m\n",
"Using Tool: File Writer Tool\n",
"\u001b[33mUser\u001b[0m (to chatbot):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_UR57tA4Dr5YF0aLInnRGa8eD) *****\u001b[0m\n",
"Content successfully written to /tmp/tmpt6zkd1db/tool_result.txt\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\n",
"The message 'Easy Migration :)' has been successfully written to the file `tool_result.txt` in the directory `/tmp/tmpt6zkd1db`.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"outputs": [],
"source": [
"with TemporaryDirectory() as tmpdirname:\n",
" filename = \"tool_result.txt\"\n",
Expand All @@ -188,108 +139,11 @@
" assert Path(tmpdirname, filename).read_text() == \"Easy Migration :)\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scrape the web\n",
"\n",
"Demonstrate scraping a website using the converted CrewAI tool.\n",
"- The `message` instructs the chatbot to scrape the AG2 website.\n",
"- `user_proxy.initiate_chat` starts the interaction, with the chatbot processing the request and using the tool.\n",
"- Finally, we output the summary from the LLM"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mUser\u001b[0m (to chatbot):\n",
"\n",
"Scape the website https://ag2.ai/\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\n",
"\u001b[32m***** Suggested tool call (call_jwhEriZ8xz0sJfbNXZQYeWRH): Read_website_content *****\u001b[0m\n",
"Arguments: \n",
"{\"args\":{\"website_url\":\"https://ag2.ai/\"}}\n",
"\u001b[32m*************************************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION Read_website_content...\u001b[0m\n",
"Using Tool: Read website content\n",
"\u001b[33mUser\u001b[0m (to chatbot):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_jwhEriZ8xz0sJfbNXZQYeWRH) *****\u001b[0m\n",
"\n",
"AgentOS\n",
"Join our growing community of over 20,000 agent builders Join our growing community of over 20,000 agent builders The Open-Source AgentOS Build production-ready multi-agent systems in minutes, not months. Github Discord The End-to-End Platform for Multi-Agent Automation The End-to-End Platform for Multi-Agent Automation Flexible Agent Construction and Orchestration Create specialized agents that work together seamlessly. AG2 makes it easy to define roles, configure behaviors, and orchestrate collaboration - all through simple, intuitive code. → Assistant agents for problem-solving → Executor agents for taking action → Critic agents for validation → Group chat managers for coordination Built-in Conversation Patterns Built-in Conversation Patterns Stop wrestling with agent coordination. AG2 handles message routing, state management, and conversation flow automatically. → Two-agent conversations → Group chats with dynamic speaker selection → Sequential chats with context carryover → Nested conversations for modularity Seamless Human-AI collaboration Seamless Human-AI collaboration Seamlessly integrate human oversight and input into your agent workflows. → Configurable human input modes → Flexible intervention points → Optional human approval workflows → Interactive conversation interfaces → Context-aware human handoff Roadmap AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 STUDIO → Visual agent system design → Real-time testing and debugging → One-click deployment to production → Perfect for prototyping and MVPs AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders AG2 MARKETPLACE → Share and monetize your agents → Discover pre-built solution templates → Quick-start your agent development → Connect with other builders SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls SCALING TOOLS → Zero to production deployment guides → Usage analytics and cost optimization → Team collaboration features → Enterprise-ready security controls Whether you're a solo founder prototyping the next big AI product, or an enterprise team deploying at scale we're building AG2 for you. This is AgentOS - making multi-agent development accessible to everyone. Github Join Our Growing Community Join Our Growing Community → 20,000+ active agent builders → Daily technical discussions → Weekly community calls → Open RFC process → Regular contributor events (Coming soon) Discord Problem Features Roadmap Community Documentation Problem Features Roadmap Community Documentation Problem Features Roadmap Community Documentation\n",
"\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mchatbot\u001b[0m (to User):\n",
"\n",
"I have scraped the content from the website https://ag2.ai/. Here are the key points:\n",
"\n",
"---\n",
"\n",
"**AgentOS**\n",
"- Join a community of over 20,000 agent builders.\n",
"- Build production-ready multi-agent systems quickly.\n",
" \n",
"**Features:**\n",
"- **Flexible Agent Construction and Orchestration**: Define roles, configure behaviors, and orchestrate collaboration through intuitive code.\n",
" - Assistant agents for problem-solving.\n",
" - Executor agents for taking action.\n",
" - Critic agents for validation.\n",
" - Group chat managers for coordination.\n",
"\n",
"- **Built-in Conversation Patterns**: Automatically handles message routing, state management, and conversation flow.\n",
" - Two-agent conversations.\n",
" - Group chats with dynamic speaker selection.\n",
" - Sequential chats with context carryover.\n",
" - Nested conversations for modularity.\n",
"\n",
"- **Seamless Human-AI Collaboration**: Integrate human oversight and input into workflows.\n",
" - Configurable human input modes.\n",
" - Flexible intervention points.\n",
" - Optional human approval workflows.\n",
" - Interactive conversation interfaces.\n",
" - Context-aware human handoff.\n",
"\n",
"**Roadmap:**\n",
"- **AG2 STUDIO**: \n",
" - Visual agent system design.\n",
" - Real-time testing and debugging.\n",
" - One-click deployment to production.\n",
"\n",
"- **AG2 MARKETPLACE**: \n",
" - Share and monetize agents.\n",
" - Discover pre-built solution templates.\n",
"\n",
"- **Scaling Tools**: \n",
" - Zero to production deployment guides.\n",
" - Usage analytics and cost optimization.\n",
" - Team collaboration features.\n",
"\n",
"---\n",
"\n",
"Whether you're a solo founder or an enterprise team, AG2 aims to make multi-agent development accessible for everyone. \n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"outputs": [],
"source": [
"crewai_scrape_tool = ScrapeWebsiteTool()\n",
"ag2_tool = crewai_interop.convert_tool(crewai_scrape_tool)\n",
Expand All @@ -304,64 +158,9 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I have scraped the content from the website https://ag2.ai/. Here are the key points:\n",
"\n",
"---\n",
"\n",
"**AgentOS**\n",
"- Join a community of over 20,000 agent builders.\n",
"- Build production-ready multi-agent systems quickly.\n",
" \n",
"**Features:**\n",
"- **Flexible Agent Construction and Orchestration**: Define roles, configure behaviors, and orchestrate collaboration through intuitive code.\n",
" - Assistant agents for problem-solving.\n",
" - Executor agents for taking action.\n",
" - Critic agents for validation.\n",
" - Group chat managers for coordination.\n",
"\n",
"- **Built-in Conversation Patterns**: Automatically handles message routing, state management, and conversation flow.\n",
" - Two-agent conversations.\n",
" - Group chats with dynamic speaker selection.\n",
" - Sequential chats with context carryover.\n",
" - Nested conversations for modularity.\n",
"\n",
"- **Seamless Human-AI Collaboration**: Integrate human oversight and input into workflows.\n",
" - Configurable human input modes.\n",
" - Flexible intervention points.\n",
" - Optional human approval workflows.\n",
" - Interactive conversation interfaces.\n",
" - Context-aware human handoff.\n",
"\n",
"**Roadmap:**\n",
"- **AG2 STUDIO**: \n",
" - Visual agent system design.\n",
" - Real-time testing and debugging.\n",
" - One-click deployment to production.\n",
"\n",
"- **AG2 MARKETPLACE**: \n",
" - Share and monetize agents.\n",
" - Discover pre-built solution templates.\n",
"\n",
"- **Scaling Tools**: \n",
" - Zero to production deployment guides.\n",
" - Usage analytics and cost optimization.\n",
" - Team collaboration features.\n",
"\n",
"---\n",
"\n",
"Whether you're a solo founder or an enterprise team, AG2 aims to make multi-agent development accessible for everyone. \n",
"\n",
"\n"
]
}
],
"outputs": [],
"source": [
"print(chat_result.summary)"
]
Expand Down
Loading

0 comments on commit 2efbb51

Please sign in to comment.