You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_tools.py 3.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from typing import Optional, Type, cast
  2. import pytest
  3. from autogen_core import CancellationToken
  4. from autogen_core.tools import Tool
  5. from autogen_ext.tools.langchain import LangChainToolAdapter # type: ignore
  6. from langchain_core.callbacks.manager import AsyncCallbackManagerForToolRun, CallbackManagerForToolRun
  7. from langchain_core.tools import BaseTool as LangChainTool
  8. from langchain_core.tools import tool # pyright: ignore
  9. from pydantic import BaseModel, Field
  10. @tool # type: ignore
  11. def add(a: int, b: int) -> int:
  12. """Add two numbers"""
  13. return a + b
  14. class CalculatorInput(BaseModel):
  15. a: int = Field(description="first number")
  16. b: int = Field(description="second number")
  17. class CustomCalculatorTool(LangChainTool):
  18. name: str = "Calculator"
  19. description: str = "useful for when you need to answer questions about math"
  20. args_schema: Type[BaseModel] = CalculatorInput
  21. return_direct: bool = True
  22. def _run(self, a: int, b: int, run_manager: Optional[CallbackManagerForToolRun] = None) -> int:
  23. """Use the tool."""
  24. return a * b
  25. async def _arun(
  26. self,
  27. a: int,
  28. b: int,
  29. run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
  30. ) -> int:
  31. """Use the tool asynchronously."""
  32. return self._run(a, b, run_manager=run_manager.get_sync() if run_manager else None)
  33. @pytest.mark.asyncio
  34. async def test_langchain_tool_adapter() -> None:
  35. # Create a LangChain tool
  36. langchain_tool = add # type: ignore
  37. # Create an adapter
  38. adapter = cast(Tool, LangChainToolAdapter(langchain_tool)) # type: ignore
  39. # Test schema generation
  40. schema = adapter.schema
  41. assert schema["name"] == "add"
  42. assert "description" in schema
  43. assert schema["description"] == "Add two numbers"
  44. assert "parameters" in schema
  45. assert schema["parameters"]["type"] == "object"
  46. assert "properties" in schema["parameters"]
  47. assert "a" in schema["parameters"]["properties"]
  48. assert "b" in schema["parameters"]["properties"]
  49. assert schema["parameters"]["properties"]["a"]["type"] == "integer"
  50. assert schema["parameters"]["properties"]["b"]["type"] == "integer"
  51. assert "required" in schema["parameters"]
  52. assert set(schema["parameters"]["required"]) == {"a", "b"}
  53. assert len(schema["parameters"]["properties"]) == 2
  54. # Test run method
  55. result = await adapter.run_json({"a": 2, "b": 3}, CancellationToken())
  56. assert result == 5
  57. # Test that the adapter's run method can be called multiple times
  58. result = await adapter.run_json({"a": 5, "b": 7}, CancellationToken())
  59. assert result == 12
  60. # Test CustomCalculatorTool
  61. custom_langchain_tool = CustomCalculatorTool()
  62. custom_adapter = LangChainToolAdapter(custom_langchain_tool) # type: ignore
  63. # Test schema generation for CustomCalculatorTool
  64. custom_schema = custom_adapter.schema
  65. assert custom_schema["name"] == "Calculator"
  66. assert custom_schema["description"] == "useful for when you need to answer questions about math" # type: ignore
  67. assert "parameters" in custom_schema
  68. assert custom_schema["parameters"]["type"] == "object"
  69. assert "properties" in custom_schema["parameters"]
  70. assert "a" in custom_schema["parameters"]["properties"]
  71. assert "b" in custom_schema["parameters"]["properties"]
  72. assert custom_schema["parameters"]["properties"]["a"]["type"] == "integer"
  73. assert custom_schema["parameters"]["properties"]["b"]["type"] == "integer"
  74. assert "required" in custom_schema["parameters"]
  75. assert set(custom_schema["parameters"]["required"]) == {"a", "b"}
  76. # Test run method for CustomCalculatorTool
  77. custom_result = await custom_adapter.run_json({"a": 3, "b": 4}, CancellationToken())
  78. assert custom_result == 12