| from langchain.tools import Tool, tool |
| from langchain_community.tools.tavily_search import TavilySearchResults |
| import math |
| import os |
|
|
| |
| search = TavilySearchResults( |
| tavily_api_key=os.getenv("TAVILY_API_KEY"), |
| max_results=3 |
| ) |
| search_tool = Tool( |
| name="web_search", |
| func=lambda q: str(search.invoke(q)), |
| description="Useful for searching the internet for current events, facts, or any real-time information. Input should be a search query." |
| ) |
|
|
| |
| @tool |
| def calculator(expression: str) -> str: |
| """Evaluates a mathematical expression. |
| Input should be a valid math expression like '2 + 2' or 'sqrt(16)'.""" |
| try: |
| allowed = {k: v for k, v in math.__dict__.items() |
| if not k.startswith("__")} |
| result = eval(expression, {"__builtins__": {}}, allowed) |
| return str(result) |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| |
| @tool |
| def summarize_text(text: str) -> str: |
| """Summarizes a long piece of text into key points. |
| Input should be the text you want summarized.""" |
| words = text.split() |
| if len(words) < 50: |
| return "Text is already short: " + text |
| sentences = text.split('.')[:3] |
| return "Key content: " + '. '.join(sentences) |
|
|
| tools = [search_tool, calculator, summarize_text] |