-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from rjambrecic/add-langchain
Add langchain tools support
- Loading branch information
Showing
13 changed files
with
1,292 additions
and
1,139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.