-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
48 lines (40 loc) · 1.77 KB
/
tools.py
File metadata and controls
48 lines (40 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_core.tools import Tool
from datetime import datetime
# ========== Tool: Save to File ==========
def save_to_txt(data: str, filename: str = "research_output.txt") -> str:
"""
Saves the given data to a text file with a timestamp.
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
formatted_text = f"--- Research Output ---\nTimestamp: {timestamp}\n\n{data}\n\n"
with open(filename, "a", encoding="utf-8") as f:
f.write(formatted_text)
return f"✅ Data successfully saved to {filename}"
save_tool = Tool(
name="save_text_to_file",
func=save_to_txt,
description="Use this to save structured research data to a text file. Input should be a full summary or result string."
)
# ========== Tool: Web Search (DuckDuckGo with fallback) ==========
try:
from langchain_community.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
search_func = search.run
except Exception:
# Fallback for Streamlit Cloud where DuckDuckGo might not work
def search_func(query: str) -> str:
return f"Search for '{query}' - DuckDuckGo unavailable. Please use Wikipedia tool for research."
search_tool = Tool(
name="search",
func=search_func,
description="Search the web for up-to-date information using DuckDuckGo. Input should be a search query."
)
# ========== Tool: Wikipedia Search ==========
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=5000)
wiki_tool = Tool(
name="wikipedia_lookup",
func=WikipediaQueryRun(api_wrapper=api_wrapper).run,
description="Search Wikipedia for concise and relevant information. Input should be a topic or keyword."
)