EastSync-AI / agents /websearch_agent /websearch_agent.py
StanSava's picture
add ability to cancel run and agent communication schema, so they communicate via a well-established contract (#12)
130a664
# agents/websearch_agent.py
from smolagents import ToolCallingAgent, DuckDuckGoSearchTool
from LLM.llm_models import websearch_model
from .get_websearch_prompt import get_websearch_prompt
class WebSearchAgent:
_instance = None
@property
def agent(self):
return self._web_search_agent
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self, step_callbacks, agent_start_callback):
if not hasattr(self, "_initialized"):
self._initialized = True
self._web_search_agent = ToolCallingAgent(
model=websearch_model,
tools=[DuckDuckGoSearchTool()],
step_callbacks=step_callbacks,
name="web_search_agent",
description=(
"An agent that searches for relevant online courses, certifications, "
"and learning paths for employees."
)
)
self._web_search_agent.prompt_templates['managed_agent'] = get_websearch_prompt(
self._web_search_agent.prompt_templates['managed_agent']
)
if agent_start_callback:
self._install_run_patch(agent_start_callback)
def _install_run_patch(self, agent_start_callback):
original_run = self._web_search_agent.run
def patched_run(prompt: str, *args, **kwargs):
if agent_start_callback:
agent_start_callback(
f"🌐 Web Search Agent starting with Web Search",
)
return original_run(prompt, *args, **kwargs)
self._web_search_agent.run = patched_run