core[patch]: support passing args_schema through as_tool (#24269)

Note: this allows the schema to be passed in positionally.

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.runnables import RunnableLambda


class Add(BaseModel):
    """Add two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


def add(input: dict) -> int:
    return input["a"] + input["b"]


runnable = RunnableLambda(add)
as_tool = runnable.as_tool(Add)
as_tool.args_schema.schema()
```
```
{'title': 'Add',
 'description': 'Add two integers together.',
 'type': 'object',
 'properties': {'a': {'title': 'A',
   'description': 'First integer',
   'type': 'integer'},
  'b': {'title': 'B', 'description': 'Second integer', 'type': 'integer'}},
 'required': ['a', 'b']}
```
This commit is contained in:
ccurme
2024-07-15 10:51:05 -04:00
committed by GitHub
parent ab2d7821a7
commit 888fbc07b5
4 changed files with 62 additions and 10 deletions

View File

@@ -17,7 +17,7 @@ from langchain_core.callbacks import (
CallbackManagerForToolRun,
)
from langchain_core.messages import ToolMessage
from langchain_core.pydantic_v1 import BaseModel, ValidationError
from langchain_core.pydantic_v1 import BaseModel, Field, ValidationError
from langchain_core.runnables import (
Runnable,
RunnableConfig,
@@ -1222,10 +1222,22 @@ def test_convert_from_runnable_dict() -> None:
assert as_tool.name == "my tool"
assert as_tool.description == "test description"
# Dict without typed input-- must supply arg types
# Dict without typed input-- must supply schema
def g(x: Dict[str, Any]) -> str:
return str(x["a"] * max(x["b"]))
# Specify via args_schema:
class GSchema(BaseModel):
"""Apply a function to an integer and list of integers."""
a: int = Field(..., description="Integer")
b: List[int] = Field(..., description="List of ints")
runnable = RunnableLambda(g)
as_tool = runnable.as_tool(GSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})
# Specify via arg_types:
runnable = RunnableLambda(g)
as_tool = runnable.as_tool(arg_types={"a": int, "b": List[int]})
result = as_tool.invoke({"a": 3, "b": [1, 2]})