current_datetime_tool.py 882 B

12345678910111213141516171819202122232425
  1. from datetime import datetime
  2. from typing import Type
  3. from langchain.tools import BaseTool
  4. from pydantic import Field, BaseModel
  5. class DatetimeToolInput(BaseModel):
  6. type: str = Field(..., description="Type for current time, must be: datetime.")
  7. class DatetimeTool(BaseTool):
  8. """Tool for querying current datetime."""
  9. name: str = "current_datetime"
  10. args_schema: Type[BaseModel] = DatetimeToolInput
  11. description: str = "A tool when you want to get the current date, time, week, month or year, " \
  12. "and the time zone is UTC. Result is \"<date> <time> <timezone> <week>\"."
  13. def _run(self, type: str) -> str:
  14. # get current time
  15. current_time = datetime.utcnow()
  16. return current_time.strftime("%Y-%m-%d %H:%M:%S UTC+0000 %A")
  17. async def _arun(self, tool_input: str) -> str:
  18. raise NotImplementedError()