File size: 3,899 Bytes
d7388f1 9e00b70 10c7cb1 e34a8fa d7388f1 528bce2 5f1c9ec 528bce2 9e00b70 d7388f1 9079ca4 d7388f1 057b801 d7388f1 10c7cb1 9e00b70 10c7cb1 9079ca4 10c7cb1 e34a8fa 9e00b70 e34a8fa 9079ca4 e34a8fa 528bce2 9e00b70 528bce2 6dae357 528bce2 9079ca4 528bce2 5f1c9ec 528bce2 5f1c9ec 528bce2 5f1c9ec 528bce2 5f1c9ec 528bce2 5f1c9ec 2f7b616 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | from langchain_community.document_loaders import WikipediaLoader
from langchain.tools import tool
import arxiv
import os
from tavily import TavilyClient
import requests
from PIL import Image
from io import BytesIO
import pandas as pd
@tool
def wiki_ret(question: str) -> str:
""" Retrieve docs from wikipedia """
print("wiki")
# Search
search_docs = WikipediaLoader(query=question,
load_max_docs=2).load()
# Format
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
for doc in search_docs
]
)
return {"context": formatted_search_docs}
@tool
def arxiv_ret(query: str, max_results: int = 3) -> str:
"""Search arXiv for a given query and return a summary of top results."""
print("arxiv")
search = arxiv.Search(
query=query,
max_results=max_results,
sort_by=arxiv.SortCriterion.Relevance
)
results = []
for result in search.results():
summary = result.summary.replace("\n", " ")
results.append(f"Title: {result.title}\nAuthors: {', '.join(a.name for a in result.authors)}\nSummary: {summary}\nURL: {result.entry_id}\n")
if not results:
return "No relevant papers found."
return "\n\n".join(results)
@tool
def tavily_ret(query: str, max_results: int = 3) -> str:
"""Use Tavily to retrieve web-based information about a topic."""
print("tavily")
api_key = os.getenv("TAVILY_API_KEY")
if not api_key:
return "Tavily API key not found."
client = TavilyClient(api_key=api_key)
results = client.search(query=query, search_depth="basic", max_results=max_results)
if not results["results"]:
return "No relevant information found."
summaries = []
for item in results["results"]:
summaries.append(f"Title: {item['title']}\nURL: {item['url']}\nSnippet: {item['content']}\n")
return "\n\n".join(summaries)
@tool
def handle_file_tool(input: dict) -> str:
"""Reads the file attached to a question"""
question = input.get("question", "")
file_url = input.get("file", "")
print("file_url: ", file_url)
if not file_url:
return "No file provided."
response = requests.get(file_url)
if not response.ok:
return f"Failed to download file: {file_url}"
file_ext = file_url.split("?")[0].split(".")[-1].lower()
# IMAGE
if file_ext in ("png", "jpg", "jpeg"):
image = Image.open(BytesIO(response.content))
return f"Image file received for question: '{question}'. (Insert vision model here.)"
# PDF
elif file_ext == "pdf":
with open("temp.pdf", "wb") as f:
f.write(response.content)
return f"PDF file received. (Insert PDF parsing logic here.)"
# EXCEL
elif file_ext in ("xlsx", "xls"):
df = pd.read_excel(BytesIO(response.content))
summary = f"Excel data preview:\n{df.head().to_string(index=False)}"
return f"Question: {question}\n{summary}"
# PYTHON CODE
elif file_ext == "py":
code = response.content.decode("utf-8")
return f"Question: {question}\nPython code received:\n{code[:500]}..." # Limit preview
else:
return f"Unsupported file type: .{file_ext}"
@tool
def add(a: float, b: float):
"""calculate summation of two numbers"""
return a + b
@tool
def subtract(a: float, b: float):
"""calculate subtraction of two numbers"""
return a - b
@tool
def multiplication(a: float, b: float):
"""calculate multiplication of two numbers"""
return a * b
@tool
def division(a: float, b: float):
"""calculate division of two numbers"""
return a / b
@tool
def mode(a: float, b: float):
"""calculate remainder of two numbers"""
return a % b |