File size: 5,346 Bytes
ae1d341 3f26185 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 ae1d341 5c314c0 3f26185 ae1d341 3f26185 | 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | from vector_store import get_supabase_client, get_gaia_data_vector_store
from langchain_core.tools import tool
from langchain_tavily import TavilySearch
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
@tool
def divide(a: int, b: int) -> float:
"""Divide a and b."""
return a / b
@tool
def sum(a: int, b: int) -> float:
"""Sum a and b."""
return a + b
@tool
def multiply(a: int, b: int) -> float:
"""Multiply a and b."""
return a * b
@tool
def substract(a: int, b: int) -> float:
"""Substract a and b."""
return a - b
@tool
def wiki_search(query: str) -> str:
"""Search Wikipedia for a query and return maximum 2 results.
Args:
query: The search query."""
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
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 {"wiki_results": formatted_search_docs}
@tool
def web_search(query: str) -> str:
"""Search Tavily for a query and return maximum 3 results.
Args:
query: The search query."""
search_docs = TavilySearch(max_results=3).invoke({"query": query})
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.get("title", "")}" page="{doc.get("url", "")}"/>\n{doc.get("content", "")[:1000]}\n</Document>'
for doc in search_docs['results']
])
return {"web_results": formatted_search_docs}
@tool
def arvix_search(query: str) -> str:
"""Search Arxiv for a query and return maximum 3 result.
Args:
query: The search query."""
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document Titla="{doc.metadata["Title"]}" Authors="{doc.metadata.get("Authors", "")} Published="{doc.metadata.get("Published", "")}"/>\n{doc.metadata.get("Summary", "")[:1000]}\n</Document>'
for doc in search_docs
])
return {"arvix_results": formatted_search_docs}
@tool
def gaia_database_search(query: str) -> str:
"""
Search the Supabase vector store for documents relevant to the query.
Returns a formatted string with the top results.
"""
s_client = get_supabase_client()
retriever = get_gaia_data_vector_store(s_client).as_retriever()
docs = retriever.get_relevant_documents(query)
# docs is a list of Document objects
if not docs:
return {"retrieval_results": "No relevant documents found."}
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata.get("task_id", "")}"/>\n{doc.page_content}\n</Document>'
for doc in docs[:3]
])
return {"retrieval_results": formatted_search_docs}
# @tool
# def read_python(file_path):
# """
# Reads a Python file and returns its content as a string.
# Args:
# file_path (str): The path to the Python file.
# Returns:
# str: The content of the Python file.
# """
# try:
# if not os.path.exists(file_path):
# return f"Error: File not found at {file_path}"
# with open(file_path, "r", encoding="utf-8") as file:
# content = file.read()
# return content
# except Exception as e:
# return f"Error reading Python file: {str(e)}"
# @tool
# def read_excel(file_path):
# """
# Reads an Excel file and returns its content as a string.
# Args:
# file_path (str): The path to the Excel file.
# Returns:
# str: The content of the Excel file.
# """
# if not file_path.endswith(('.xls', '.xlsx')):
# return "Error: File is not an Excel file."
# try:
# if not os.path.exists(file_path):
# return f"Error: File not found at {file_path}"
# import pandas as pd
# df = pd.read_excel(file_path)
# return df.to_string()
# except Exception as e:
# return f"Error reading Excel file: {str(e)}"
# @tool
# def transcribe_audio(file_path):
# """
# Transcribes an audio file and returns its content as a string.
# Args:
# file_path (str): The path to the audio file.
# Returns:
# str: The transcribed text from the audio file.
# """
# if not file_path.endswith(('.wav', '.mp3', '.m4a')):
# return "Error: File is not an audio file."
# try:
# if not os.path.exists(file_path):
# return f"Error: File not found at {file_path}"
# import whisper
# model = whisper.load_model("base")
# result = model.transcribe(file_path, language="en")
# text = result["text"]
# return text
# except Exception as e:
# return f"Error transcribing audio file: {str(e)}"
if __name__ == '__main__':
response = gaia_database_search("Question : On a leap day before the year 2008, a joke was removed from the Wikipedia page for “Dragon”. What was the phrase that was removed? Give the phrase as it appeared on the page, but without punctuation.")
print(response) |