Saumalya Sarkar
🚧 Trying to achieve 30% using supabase retriever node
6865997
Raw History Blame Contribute Delete
5.35 kB
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)