-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add convenience method to use LangChain community tools #1832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
b3f8bd2
Output handling refactoring borrowed from output_mode PR
DouweM 2ebe6a9
Support functions as output_type, as well as lists of functions and o…
DouweM ab576d7
Fix tests
DouweM 6c4fcec
Make Python 3.9 happy
DouweM 1bd16dc
Support output_type = bound instance method
DouweM 98e64d4
Support RunContext arg on output_type function using same logic as tools
DouweM 60d789e
Improve test coverage
DouweM 14e69d0
Start output tool name disambiguation counter at 2
DouweM ee95a80
Stop requiring explicitly specifying type_ kwarg name on ToolOutput
DouweM f1f093e
Remove runtime assertion from typed_agent.py as the file is only type…
DouweM 66e5405
Add typing tests for Agent(output_type=)
DouweM 53d25f9
Merge remote-tracking branch 'origin/main' into output-type-callable
DouweM 3aad6fc
Update typing test for mypy
DouweM 3ff6e74
Treat str in an output_type list the same as in a union
DouweM a7fd8ac
Set ToolRetryError as cause on UnexpectedModelBehavior when available
DouweM 5399df2
Drop end line from example test parameterized test ID to make it easi…
DouweM e503edb
Document output functions
DouweM f436797
Fix docs
DouweM 046b813
Update output_type typing tests
DouweM dc39e68
Update output_type typing tests
DouweM a578745
Document when automatic output_type type inference may fail
DouweM fc42d69
Suggested code for from_langchain
matthewfranglen 7f897b1
Add optional group of langchain to allow type checking
matthewfranglen e3b3c7f
Fix linting problems
matthewfranglen 8597ae4
Add a test for tool conversion
matthewfranglen 2a92ba0
Drop attempted import of langchain tool
matthewfranglen 2d22c32
Older pythons don't enjoy type unions with |
matthewfranglen 2d6cc1a
Drop unnecessary explicit generic parameter on constructor in typing …
DouweM 43a6247
Merge branch 'main' into langchain-tools
matthewfranglen 6335103
Merge branch 'output-type-callable' into langchain-tools
matthewfranglen 6ef816a
hack in the new FunctionSchema approach
matthewfranglen aefad98
split into from_function and from_langchain
matthewfranglen 1614418
Use the proper schema validator
matthewfranglen 5269059
test the function conversion directly
matthewfranglen 8b020ec
Merge branch 'main' into langchain-tools
matthewfranglen b7478a3
Support async functions for from_function
matthewfranglen 880c629
Fix the type of the schema
matthewfranglen 8407f3b
test the langchain function directly
matthewfranglen ba90237
type: ignore the funny calls
matthewfranglen 6351ffa
call the broken function to get coverage
matthewfranglen 40b5a5c
branch coverage for required/default
matthewfranglen 25df877
test that the default is overridden
matthewfranglen 8121577
Change name of function
matthewfranglen 49d5377
Merge branch 'main' into langchain-tools
matthewfranglen ffb78eb
fix import, address PR feedback
matthewfranglen 8024a80
Self was introduced in python 3.11
matthewfranglen 98ffd4e
Use AgentDepsT
matthewfranglen 44f285e
Import Self from typing_extensions
matthewfranglen f75ea7b
Merge branch 'main' into langchain-tools
matthewfranglen 4af3e4e
remove langchain optional group
matthewfranglen 8bf590b
Make the FunctionSchema arguments defaults
matthewfranglen cd5e6c4
sort the items before showing them
matthewfranglen e1a5562
use dictionary merging
matthewfranglen 9194930
Use JsonSchemaValue to describe arguments
matthewfranglen 9f89e02
Make name and description arguments
matthewfranglen d2462d8
Revert unrelated formatting changes
matthewfranglen 1c99adf
uv run ruff format
matthewfranglen c709402
Handle $refs in the langchain schema
matthewfranglen f075d7d
Replace arg iteration with assertion
matthewfranglen 65a04be
Add flag to allow testing additional properties branch
matthewfranglen b91cd66
Write langchain tool section HEAVILY BASED ON SMOLAGENTS DOCS
matthewfranglen 5300f3f
formatting for docs
matthewfranglen a06f814
Testing imports in docs is neat
matthewfranglen 24507f9
import sorting
matthewfranglen 3ab281f
Rephrase and skip tests over code
matthewfranglen e67b0ab
Clarify the requirements over the function
matthewfranglen d96fb5b
Add section on from_schema
matthewfranglen 47e9d7b
replace double quotes with single quotes, add comma
matthewfranglen 17969fb
docs/tools.md:415:1: I001 [*] Import block is un-sorted or un-formatted
matthewfranglen 77f6a98
Move langchain tool converter
matthewfranglen 0142858
Add example of using tool from Tool.from_schema
matthewfranglen 77a7bee
remove assertions over _function_tools
matthewfranglen 669b299
Satisfy linter
DouweM d83ef10
Rename helper to tool_from_langchain
DouweM fe14acd
Update LangChain example
DouweM e558fc7
Update LangChain example
DouweM 3646fb3
Merge branch 'main' into langchain-tools
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,61 @@ | ||
from typing import Any, Protocol | ||
|
||
from pydantic.json_schema import JsonSchemaValue | ||
|
||
from pydantic_ai.tools import Tool | ||
|
||
|
||
class LangChainTool(Protocol): | ||
# args are like | ||
# {'dir_path': {'default': '.', 'description': 'Subdirectory to search in.', 'title': 'Dir Path', 'type': 'string'}, | ||
# 'pattern': {'description': 'Unix shell regex, where * matches everything.', 'title': 'Pattern', 'type': 'string'}} | ||
@property | ||
def args(self) -> dict[str, JsonSchemaValue]: ... | ||
|
||
def get_input_jsonschema(self) -> JsonSchemaValue: ... | ||
|
||
@property | ||
def name(self) -> str: ... | ||
|
||
@property | ||
def description(self) -> str: ... | ||
|
||
def run(self, *args: Any, **kwargs: Any) -> str: ... | ||
|
||
|
||
__all__ = ('tool_from_langchain',) | ||
|
||
|
||
def tool_from_langchain(langchain_tool: LangChainTool) -> Tool: | ||
"""Creates a Pydantic tool proxy from a LangChain tool. | ||
|
||
Args: | ||
langchain_tool: The LangChain tool to wrap. | ||
|
||
Returns: | ||
A Pydantic tool that corresponds to the LangChain tool. | ||
""" | ||
function_name = langchain_tool.name | ||
function_description = langchain_tool.description | ||
inputs = langchain_tool.args.copy() | ||
required = sorted({name for name, detail in inputs.items() if 'default' not in detail}) | ||
schema: JsonSchemaValue = langchain_tool.get_input_jsonschema() | ||
if 'additionalProperties' not in schema: | ||
schema['additionalProperties'] = False | ||
if required: | ||
schema['required'] = required | ||
|
||
defaults = {name: detail['default'] for name, detail in inputs.items() if 'default' in detail} | ||
|
||
# restructures the arguments to match langchain tool run | ||
def proxy(*args: Any, **kwargs: Any) -> str: | ||
assert not args, 'This should always be called with kwargs' | ||
kwargs = defaults | kwargs | ||
return langchain_tool.run(kwargs) | ||
|
||
return Tool.from_schema( | ||
function=proxy, | ||
name=function_name, | ||
description=function_description, | ||
json_schema=schema, | ||
) |
This file contains hidden or 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
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.