current_datetime_tool.py 858 B

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