|
|
"""LangGraph Agent (No Supabase)""" |
|
|
import os |
|
|
from langgraph.graph import START, StateGraph, MessagesState |
|
|
from langgraph.prebuilt import tools_condition, ToolNode |
|
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
|
from langchain_groq import ChatGroq |
|
|
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint |
|
|
from langchain_community.tools.tavily_search import TavilySearchResults |
|
|
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader |
|
|
from langchain_core.messages import SystemMessage, HumanMessage |
|
|
from langchain_core.tools import tool |
|
|
|
|
|
@tool |
|
|
def multiply(a: int, b: int) -> int: |
|
|
"""Multiply two integers and return the result.""" |
|
|
return a * b |
|
|
|
|
|
@tool |
|
|
def add(a: int, b: int) -> int: |
|
|
"""Add two integers and return the result.""" |
|
|
return a + b |
|
|
|
|
|
@tool |
|
|
def subtract(a: int, b: int) -> int: |
|
|
"""Subtract b from a and return the result.""" |
|
|
return a - b |
|
|
|
|
|
@tool |
|
|
def divide(a: int, b: int) -> float: |
|
|
"""Divide a by b and return the result. Raises an error if b is zero.""" |
|
|
if b == 0: |
|
|
raise ValueError("Cannot divide by zero.") |
|
|
return a / b |
|
|
|
|
|
@tool |
|
|
def modulus(a: int, b: int) -> int: |
|
|
"""Return the modulus (remainder) of a divided by b.""" |
|
|
return a % b |
|
|
|
|
|
@tool |
|
|
def wiki_search(query: str) -> dict: |
|
|
"""Search Wikipedia for a query and return up to 2 results.""" |
|
|
search_docs = WikipediaLoader(query=query, load_max_docs=2).load() |
|
|
results = "\n\n---\n\n".join( |
|
|
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs |
|
|
) |
|
|
return {"wiki_results": results} |
|
|
|
|
|
@tool |
|
|
def web_search(query: str) -> dict: |
|
|
"""Search the web via Tavily and return up to 3 results.""" |
|
|
search_docs = TavilySearchResults(max_results=3).invoke(query=query) |
|
|
results = "\n\n---\n\n".join( |
|
|
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs |
|
|
) |
|
|
return {"web_results": results} |
|
|
|
|
|
@tool |
|
|
def arvix_search(query: str) -> dict: |
|
|
"""Search Arxiv and return up to 3 truncated results.""" |
|
|
search_docs = ArxivLoader(query=query, load_max_docs=3).load() |
|
|
results = "\n\n---\n\n".join( |
|
|
f"<Document>\n{doc.page_content[:500]}\n</Document>" for doc in search_docs |
|
|
) |
|
|
return {"arvix_results": results} |
|
|
|
|
|
|
|
|
with open("system_prompt.txt", "r", encoding="utf-8") as f: |
|
|
system_prompt = f.read() |
|
|
|
|
|
sys_msg = SystemMessage(content=system_prompt) |
|
|
|
|
|
tools = [ |
|
|
multiply, add, subtract, divide, modulus, |
|
|
wiki_search, web_search, arvix_search |
|
|
] |
|
|
|
|
|
def build_graph(provider: str = "groq"): |
|
|
"""Build the LangGraph agent with selected LLM provider.""" |
|
|
if provider == "google": |
|
|
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0) |
|
|
elif provider == "groq": |
|
|
groq_api_key = os.environ.get("GROQ_API_KEY") |
|
|
if not groq_api_key: |
|
|
raise ValueError("GROQ_API_KEY is not set in the environment.") |
|
|
llm = ChatGroq(model="qwen-qwq-32b", temperature=0, api_key=groq_api_key) |
|
|
elif provider == "huggingface": |
|
|
llm = ChatHuggingFace( |
|
|
llm=HuggingFaceEndpoint( |
|
|
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf", |
|
|
temperature=0, |
|
|
) |
|
|
) |
|
|
else: |
|
|
raise ValueError("Invalid provider: choose 'google', 'groq' or 'huggingface'.") |
|
|
|
|
|
llm_with_tools = llm.bind_tools(tools) |
|
|
|
|
|
def assistant(state: MessagesState): |
|
|
return {"messages": [llm_with_tools.invoke(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() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
from langchain_core.messages import HumanMessage |
|
|
question = "What is the capital of France and its population?" |
|
|
graph = build_graph() |
|
|
messages = [HumanMessage(content=question)] |
|
|
result = graph.invoke({"messages": messages}) |
|
|
for msg in result["messages"]: |
|
|
print(msg.content) |