|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import MagicMock |
| 16 | + |
| 17 | +from google.adk.tools.langchain_tool import LangchainTool |
| 18 | +from langchain.tools import tool |
| 19 | +from langchain_core.tools.structured import StructuredTool |
| 20 | +from pydantic import BaseModel |
| 21 | +import pytest |
| 22 | + |
| 23 | + |
| 24 | +@tool |
| 25 | +async def async_add_with_annotation(x, y) -> int: |
| 26 | + """Adds two numbers""" |
| 27 | + return x + y |
| 28 | + |
| 29 | + |
| 30 | +@tool |
| 31 | +def sync_add_with_annotation(x, y) -> int: |
| 32 | + """Adds two numbers""" |
| 33 | + return x + y |
| 34 | + |
| 35 | + |
| 36 | +async def async_add(x, y) -> int: |
| 37 | + return x + y |
| 38 | + |
| 39 | + |
| 40 | +def sync_add(x, y) -> int: |
| 41 | + return x + y |
| 42 | + |
| 43 | + |
| 44 | +class AddSchema(BaseModel): |
| 45 | + x: int |
| 46 | + y: int |
| 47 | + |
| 48 | + |
| 49 | +test_langchain_async_add_tool = StructuredTool.from_function( |
| 50 | + async_add, |
| 51 | + name="add", |
| 52 | + description="Adds two numbers", |
| 53 | + args_schema=AddSchema, |
| 54 | +) |
| 55 | + |
| 56 | +test_langchain_sync_add_tool = StructuredTool.from_function( |
| 57 | + sync_add, |
| 58 | + name="add", |
| 59 | + description="Adds two numbers", |
| 60 | + args_schema=AddSchema, |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.asyncio |
| 65 | +async def test_raw_async_function_works(): |
| 66 | + """Test that passing a raw async function to LangchainTool works correctly.""" |
| 67 | + langchain_tool = LangchainTool(tool=test_langchain_async_add_tool) |
| 68 | + result = await langchain_tool.run_async( |
| 69 | + args={"x": 1, "y": 3}, tool_context=MagicMock() |
| 70 | + ) |
| 71 | + assert result == 4 |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_raw_sync_function_works(): |
| 76 | + """Test that passing a raw sync function to LangchainTool works correctly.""" |
| 77 | + langchain_tool = LangchainTool(tool=test_langchain_sync_add_tool) |
| 78 | + result = await langchain_tool.run_async( |
| 79 | + args={"x": 1, "y": 3}, tool_context=MagicMock() |
| 80 | + ) |
| 81 | + assert result == 4 |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_raw_async_function_with_annotation_works(): |
| 86 | + """Test that passing a raw async function to LangchainTool works correctly.""" |
| 87 | + langchain_tool = LangchainTool(tool=async_add_with_annotation) |
| 88 | + result = await langchain_tool.run_async( |
| 89 | + args={"x": 1, "y": 3}, tool_context=MagicMock() |
| 90 | + ) |
| 91 | + assert result == 4 |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_raw_sync_function_with_annotation_works(): |
| 96 | + """Test that passing a raw sync function to LangchainTool works correctly.""" |
| 97 | + langchain_tool = LangchainTool(tool=sync_add_with_annotation) |
| 98 | + result = await langchain_tool.run_async( |
| 99 | + args={"x": 1, "y": 3}, tool_context=MagicMock() |
| 100 | + ) |
| 101 | + assert result == 4 |
0 commit comments