Create gaia_agent.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200from langchain_ollama import ChatOllamafrom langchain_core.messages import SystemMessage, HumanMessagefrom langgraph.graph import START, StateGraph, MessagesStatefrom langgraph.prebuilt import ToolNode, tools_conditionfrom langchain_community.tools import DuckDuckGoSearchRunfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_community.document_loaders import WikipediaLoaderfrom langchain_community.document_loaders import ArxivLoaderfrom langchain_core.tools import toolfrom langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFacefrom math import sqrt
### =============== MATHEMATICAL TOOLS =============== ###
@tooldef multiply(a: float, b: float) -> float: """ Multiplies two numbers. Args: a (float): the first number b (float): the second number """ return a * b
@tooldef add(a: float, b: float) -> float: """ Adds two numbers. Args: a (float): the first number b (float): the second number """ return a + b
@tooldef subtract(a: float, b: float) -> int: """ Subtracts two numbers. Args: a (float): the first number b (float): the second number """ return a - b
@tooldef divide(a: float, b: float) -> float: """ Divides two numbers. Args: a (float): the first float number b (float): the second float number """ if b == 0: raise ValueError("Cannot divided by zero.") return a / b
@tooldef modulus(a: int, b: int) -> int: """ Get the modulus of two numbers. Args: a (int): the first number b (int): the second number """ return a % b
@tooldef power(a: float, b: float) -> float: """ Get the power of two numbers. Args: a (float): the first number b (float): the second number """ return a**b
@tooldef square_root(a: float) -> float | complex: """ Get the square root of a number. Args: a (float): the number to get the square root of """ if a >= 0: return a**0.5 return sqrt(a)
### =============== BROWSER TOOLS =============== ###
search_tool = DuckDuckGoSearchRun()
@tooldef 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}
@tooldef web_search(query: str) -> str: """Search Tavily for a query and return maximum 3 results. Args: query: The search query.""" print(f"I'm running web_search with {query = }") search_docs = TavilySearchResults(max_results=3).invoke(query=query) 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 {"web_results": formatted_search_docs}
@tooldef arxiv_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 source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>' for doc in search_docs ] ) return {"arxiv_results": formatted_search_docs}
tools = [multiply, add, subtract, divide, modulus, power, square_root, web_search, arxiv_search, wiki_search]
GAIA_SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""sys_msg = SystemMessage(content=GAIA_SYSTEM_PROMPT)
# Build graph functiondef build_graph(provider: str = "ollama"): """Build the graph""" # Load environment variables from .env file if provider == "ollama": chat = ChatOllama(model="llama3.1") elif provider == "huggingface": llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-Coder-32B-Instruct" )
chat = ChatHuggingFace(llm=llm, verbose=True) else: raise ValueError("Invalid provider. Choose 'ollama' or 'huggingface'.") # Bind tools to LLM chat_with_tools = chat.bind_tools(tools)
# Node def assistant(state: MessagesState): """Assistant node""" print([sys_msg] + state["messages"]) return {"messages": [chat_with_tools.invoke([sys_msg] + state["messages"])]}
builder = StateGraph(MessagesState) builder.add_node("assistant", assistant) builder.add_node("tools", ToolNode(tools)) builder.add_edge(START, "assistant") builder.add_conditional_edges( "assistant", tools_condition, ) builder.add_edge("tools", "assistant")
return builder.compile()
# testif __name__ == "__main__": question = "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"" # fixed_answer = "extremely" graph = build_graph(provider="huggingface") messages = [HumanMessage(content=question)] messages = graph.invoke({"messages": messages}) for m in messages["messages"]: m.pretty_print()gaia_agent.pyAdded+200