Spaces:
Build error
Build error
| import os | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| from langgraph.graph import START, StateGraph, MessagesState | |
| from langgraph.prebuilt import tools_condition | |
| from langgraph.prebuilt import ToolNode | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain_core.messages import SystemMessage, HumanMessage | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import SupabaseVectorStore | |
| from langchain.tools.retriever import create_retriever_tool | |
| from supabase.client import Client, create_client | |
| from tools.math_tools import add, subtract, multiply, divide, modulus, power, sqrt | |
| from tools.search_tools import search_wikipedia, web_search, arxiv_search | |
| from tools.image_video_tools import query_image | |
| from tools.file_tools import analyze_excel_file, execute_python_code | |
| system_prompt = Path("system_prompt.txt").read_text() | |
| # embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768 | |
| # supabase: Client = create_client( | |
| # os.environ.get("SUPABASE_URL"), | |
| # os.environ.get("SUPABASE_SERVICE_KEY")) | |
| # vector_store = SupabaseVectorStore( | |
| # client=supabase, | |
| # embedding= embeddings, | |
| # table_name="documents", | |
| # query_name="match_documents_langchain", | |
| # ) | |
| # retriever_tool = create_retriever_tool( | |
| # retriever=vector_store.as_retriever(), | |
| # name="Question Search", | |
| # description="A tool to retrieve similar questions from a vector store.", | |
| # ) | |
| def build_graph(): | |
| llm = ChatGoogleGenerativeAI( | |
| model="gemini-2.0-flash-001", | |
| temperature=0.8, | |
| max_tokens=None, | |
| timeout=None, | |
| max_retries=2, | |
| google_api_key=os.getenv("GOOGLE_API_KEY") # Get API key from environment variable | |
| ) | |
| tools = [add, subtract, multiply, divide, modulus, power, sqrt, web_search, arxiv_search, search_wikipedia, query_image, analyze_excel_file, execute_python_code] | |
| llm_with_tools = llm.bind_tools(tools) | |
| def assistant(state: MessagesState): | |
| """Assistant node for invoking the LLM.""" | |
| messages = state["messages"] | |
| # Add system message if not present | |
| if not any(isinstance(m, SystemMessage) for m in messages): | |
| messages = [SystemMessage(content=system_prompt)] + messages | |
| response = llm_with_tools.invoke(messages) | |
| return {"messages": [response]} | |
| # def retriever(state: MessagesState): | |
| # """Retriever node""" | |
| # # Add system message if not present | |
| # messages = state["messages"] | |
| # if not any(isinstance(m, SystemMessage) for m in messages): | |
| # messages = [SystemMessage(content="You are a helpful AI assistant. Use the available tools to answer questions accurately. When providing your final answer, use the format: FINAL ANSWER: [your answer]")] + messages | |
| # similar_question = vector_store.similarity_search(state["messages"][0].content) | |
| # example_msg = HumanMessage( | |
| # content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}", | |
| # ) | |
| # return {"messages": messages + [example_msg]} | |
| builder = StateGraph(MessagesState) | |
| # builder.add_node("retriever", retriever) | |
| builder.add_node("assistant", assistant) | |
| builder.add_node("tools", ToolNode(tools)) | |
| builder.add_edge(START, "assistant") | |
| # builder.add_edge("retriever", "assistant") | |
| builder.add_conditional_edges( | |
| "assistant", | |
| tools_condition, | |
| ) | |
| builder.add_edge("tools", "assistant") | |
| # Compile graph | |
| return builder.compile() | |
| if __name__ == "__main__": | |
| question = "On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?" | |
| # Build the graph | |
| graph = build_graph() | |
| # Run the graph | |
| messages = [HumanMessage(content=question)] | |
| messages = graph.invoke({"messages": messages}) | |
| for m in messages["messages"]: | |
| m.pretty_print() | |