mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-03 21:28:29 +00:00
* move base_tool to main package and consolidate tool desscription generation * update import path * update tests * update doc * add base_tool test * migrate agent delegation tools to use BaseTool * update tests * update import path for tool * fix lint * update param signature * add from_langchain to BaseTool for backwards support of langchain tools * fix the case where StructuredTool doesn't have func --------- Co-authored-by: c0dez <li@vitablehealth.com> Co-authored-by: Brandon Hancock (bhancock_ai) <109994880+bhancockio@users.noreply.github.com>
33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
from typing import Any, Optional, Union
|
|
|
|
from ..tools.cache_tools.cache_tools import CacheTools
|
|
from ..tools.tool_calling import InstructorToolCalling, ToolCalling
|
|
from .cache.cache_handler import CacheHandler
|
|
|
|
|
|
class ToolsHandler:
|
|
"""Callback handler for tool usage."""
|
|
|
|
last_used_tool: ToolCalling = {} # type: ignore # BUG?: Incompatible types in assignment (expression has type "Dict[...]", variable has type "ToolCalling")
|
|
cache: Optional[CacheHandler]
|
|
|
|
def __init__(self, cache: Optional[CacheHandler] = None):
|
|
"""Initialize the callback handler."""
|
|
self.cache = cache
|
|
self.last_used_tool = {} # type: ignore # BUG?: same as above
|
|
|
|
def on_tool_use(
|
|
self,
|
|
calling: Union[ToolCalling, InstructorToolCalling],
|
|
output: str,
|
|
should_cache: bool = True,
|
|
) -> Any:
|
|
"""Run when tool ends running."""
|
|
self.last_used_tool = calling # type: ignore # BUG?: Incompatible types in assignment (expression has type "Union[ToolCalling, InstructorToolCalling]", variable has type "ToolCalling")
|
|
if self.cache and should_cache and calling.tool_name != CacheTools().name:
|
|
self.cache.add(
|
|
tool=calling.tool_name,
|
|
input=calling.arguments,
|
|
output=output,
|
|
)
|