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.7 kB

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