tools.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from langchain_core.tools import tool
  2. from typing import Annotated
  3. from langchain_core.messages import ToolMessage
  4. from langchain_core.tools import InjectedToolCallId, tool
  5. from langgraph.types import Command, interrupt
  6. # Define tools
  7. @tool
  8. def multiply(a: int, b: int) -> int:
  9. """Multiply a and b.
  10. Args:
  11. a: first int
  12. b: second int
  13. """
  14. return a * b
  15. @tool
  16. def add(a: int, b: int) -> int:
  17. """Adds a and b.
  18. Args:
  19. a: first int
  20. b: second int
  21. """
  22. return a + b
  23. @tool
  24. def divide(a: int, b: int) -> float:
  25. """Divide a and b.
  26. Args:
  27. a: first int
  28. b: second int
  29. """
  30. return a / b
  31. @tool
  32. def human_assistance(
  33. name: str, birthday: str, tool_call_id: Annotated[str, InjectedToolCallId]
  34. ) -> str:
  35. """Request assistance from a human."""
  36. human_response = interrupt(
  37. {
  38. "question": "Is this correct?",
  39. "name": name,
  40. "birthday": birthday,
  41. },
  42. )
  43. if human_response.get("correct", "").lower().startswith("y"):
  44. verified_name = name
  45. verified_birthday = birthday
  46. response = "Correct"
  47. else:
  48. verified_name = human_response.get("name", name)
  49. verified_birthday = human_response.get("birthday", birthday)
  50. response = f"Made a correction: {human_response}"
  51. state_update = {
  52. "name": verified_name,
  53. "birthday": verified_birthday,
  54. "messages": [ToolMessage(response, tool_call_id=tool_call_id)],
  55. }
  56. return Command(update=state_update)