Spaces:
Runtime error
Runtime error
helper is a tool library
Browse files
helper.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_experimental.utilities import PythonREPL
|
| 2 |
+
from langchain.tools import Tool
|
| 3 |
+
from langchain_community.tools import TavilySearchResults
|
| 4 |
+
|
| 5 |
+
# For newer LangChain versions, sometimes it's directly from langchain.tools.python
|
| 6 |
+
# from langchain.tools.python.tool import PythonREPLTool
|
| 7 |
+
|
| 8 |
+
python_repl = PythonREPL()
|
| 9 |
+
|
| 10 |
+
# Create the Tool object to pass to the agent
|
| 11 |
+
# Make sure to update the name if you change it from 'python_repl'
|
| 12 |
+
repl_tool = Tool(
|
| 13 |
+
name="python_repl",
|
| 14 |
+
description="""
|
| 15 |
+
A Python REPL (Read-Eval-Print Loop) for executing Python code.
|
| 16 |
+
Use this tool for:
|
| 17 |
+
- Performing accurate calculations (arithmetic, complex math).
|
| 18 |
+
- Manipulating and analyzing data (e.g., lists, numbers).
|
| 19 |
+
- Executing small, self-contained Python scripts.
|
| 20 |
+
|
| 21 |
+
**Input Format (CRITICAL):**
|
| 22 |
+
The input MUST be a valid, executable Python code string.
|
| 23 |
+
If you want to see the result of an expression or variable, you MUST explicitly `print()` it.
|
| 24 |
+
Example: `print(2 + 2)`
|
| 25 |
+
Example:
|
| 26 |
+
```python
|
| 27 |
+
x = 10
|
| 28 |
+
y = 20
|
| 29 |
+
print(x * y)
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
**DO NOT:**
|
| 33 |
+
- Ask questions in natural language.
|
| 34 |
+
- Expect implicit output (always use `print()`).
|
| 35 |
+
- Attempt to run interactive commands or scripts that require user input.
|
| 36 |
+
- Write code that requires external files or non-standard libraries unless explicitly provided or permitted.
|
| 37 |
+
- Access the internet or external resources unless the environment allows it.
|
| 38 |
+
|
| 39 |
+
**Output:**
|
| 40 |
+
The tool returns the standard output (stdout) of the executed Python code.
|
| 41 |
+
If there is an error in the code, the error message will be returned instead.
|
| 42 |
+
""",
|
| 43 |
+
func=python_repl.run,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_travily_api_search_tool(tavily_api_key: str) -> Tool:
|
| 48 |
+
"""
|
| 49 |
+
Creates and returns a LangChain Tool for Tavily Search API.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
tavily_api_key: The API key for Tavily Search.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
A LangChain Tool configured for Tavily Search.
|
| 56 |
+
"""
|
| 57 |
+
tavily_search = TavilySearchResults(
|
| 58 |
+
max_results=3, # Limits the number of search results to 3
|
| 59 |
+
include_answer=True, # Prioritizes direct answers from search results
|
| 60 |
+
include_raw_content=False, # Excludes full raw content of pages to save tokens
|
| 61 |
+
include_images=False, # Excludes image results to save tokens
|
| 62 |
+
tavily_api_key=tavily_api_key
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
return Tool(
|
| 66 |
+
name="tavily_search",
|
| 67 |
+
description="""
|
| 68 |
+
A powerful search engine tool for real-time information retrieval from the web.
|
| 69 |
+
Use this tool when you need to:
|
| 70 |
+
- Find up-to-date information, facts, or statistics.
|
| 71 |
+
- Research current events, people, or concepts.
|
| 72 |
+
- Answer questions that require external knowledge not present in the model's training data.
|
| 73 |
+
- Verify information or get factual answers.
|
| 74 |
+
|
| 75 |
+
**Input Format (CRITICAL):**
|
| 76 |
+
The input MUST be a concise, clear, and specific search query string.
|
| 77 |
+
Think of what you would type into a Google search bar.
|
| 78 |
+
Example: "current weather in London"
|
| 79 |
+
Example: "population of India 2024"
|
| 80 |
+
Example: "history of the internet"
|
| 81 |
+
|
| 82 |
+
**DO NOT:**
|
| 83 |
+
- Ask natural language questions that are not search queries.
|
| 84 |
+
- Provide incomplete sentences or ambiguous terms.
|
| 85 |
+
- Include personal information or sensitive data in your queries.
|
| 86 |
+
- Expect direct execution of commands or calculations; this is a search tool.
|
| 87 |
+
|
| 88 |
+
**Output:**
|
| 89 |
+
The tool returns a JSON string containing relevant search results.
|
| 90 |
+
It prioritizes a direct 'answer' if available. Otherwise, it provides a list of
|
| 91 |
+
search results including titles, URLs, and snippets.
|
| 92 |
+
""",
|
| 93 |
+
func=tavily_search.run,
|
| 94 |
+
)
|