Agent Tools
This app uses a LangGraph ReAct agent with LangChain tools (defined in backend/chatbot_agent.py
). Tools let the model call out to functions like search, APIs, or utilities.
What you get out-of-the-box
web_search(query)
: fetches web results via Tavily (requiresTAVILY_API_KEY
).
Create the agent:
from chatbot_agent import create_chatbot_agent
agent = create_chatbot_agent(model_name="mistralai/Mistral-Small-24B-Instruct")
Current Implementation
The ReAct agent currently has one tool available:
@tool
def web_search(query: str) -> str:
"""
Do a web search to query additional information for the user.
"""
logger.info(f"Searching web for: {query}")
retriever = TavilySearchAPIRetriever(k=8)
chunks = retriever.invoke(query)
logger.info(chunks)
return "\n\n".join(chunk.page_content for chunk in chunks)
The agent is created with:
return create_react_agent(model=llm, prompt=_prompt, tools=[web_search], state_schema=AgentStatePydantic)
Add a new tool (step‑by‑step)
- Define a function with a helpful docstring and decorate with
@tool
. - Keep inputs simple (prefer a single
query: str
). - Handle timeouts and exceptions; return a readable string on failure.
- Register the tool in
create_chatbot_agent
by adding it to the tools list.
Example – a simple clock tool:
from langchain_core.tools import tool
@tool
def get_current_time(_: str) -> str:
"""Return the current UTC time in ISO 8601 format."""
import datetime
return datetime.datetime.utcnow().isoformat()
# In create_chatbot_agent(...), include `get_current_time` in the tools list.
# Example: tools=[web_search, get_current_time] in backend/chatbot_agent.py
Best practices
- Keep tool outputs short and factual; avoid speculation.
- Validate and sanitize inputs (URLs, IDs).
- Timeouts and retries: be defensive against slow or flaky APIs.
- Don’t log secrets; read them from env (e.g., IONOS_API_KEY, TAVILY_API_KEY).
- Cache where it helps (e.g., web results) to reduce latency and cost.