| """LangGraph Agent – versione senza Supabase""" |
|
|
| import os |
| from dotenv import load_dotenv |
| from langgraph.graph import START, StateGraph, MessagesState |
| from langgraph.prebuilt import ToolNode, tools_condition |
|
|
| |
| 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 |
|
|
| load_dotenv() |
|
|
| |
| |
| |
| @tool |
| def multiply(a: int, b: int) -> int: |
| """Multiply two integers and return the product.""" |
| return a * b |
|
|
|
|
| @tool |
| def add(a: int, b: int) -> int: |
| """Add two integers and return the sum.""" |
| return a + b |
|
|
|
|
| @tool |
| def subtract(a: int, b: int) -> int: |
| """Subtract the second integer from the first and return the difference.""" |
| return a - b |
|
|
|
|
| @tool |
| def divide(a: int, b: int) -> float: |
| """Divide a by b and return the quotient (error if b == 0).""" |
| if b == 0: |
| raise ValueError("Cannot divide by zero.") |
| return a / b |
|
|
|
|
| @tool |
| def modulus(a: int, b: int) -> int: |
| """Return the remainder of the division of a by b.""" |
| return a % b |
|
|
|
|
| |
| |
| |
| @tool |
| def wiki_search(query: str) -> str: |
| """Search Wikipedia (max 2 docs) and return formatted content.""" |
| docs = WikipediaLoader(query=query, load_max_docs=2).load() |
| return "\n\n---\n\n".join( |
| f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n' |
| f"{d.page_content}\n</Document>" |
| for d in docs |
| ) |
|
|
|
|
| |
| |
| |
| @tool |
| def web_search(query: str) -> str: |
| """Perform a web search with Tavily (max 3 docs) and return formatted content.""" |
| docs = TavilySearchResults(max_results=3).invoke(query=query) |
| return "\n\n---\n\n".join( |
| f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n' |
| f"{d.page_content}\n</Document>" |
| for d in docs |
| ) |
|
|
|
|
| |
| |
| |
| @tool |
| def arxiv_search(query: str) -> str: |
| """Search ArXiv (max 3 docs) and return first 1000 characters per paper.""" |
| docs = ArxivLoader(query=query, load_max_docs=3).load() |
| return "\n\n---\n\n".join( |
| f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n' |
| f"{d.page_content[:1000]}\n</Document>" |
| for d in docs |
| ) |
|
|
|
|
| |
| |
| |
| 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, |
| arxiv_search, |
| ] |
|
|
| |
| |
| |
| def build_graph(provider: str = "groq"): |
| """Return a LangGraph graph without Supabase dependencies.""" |
| |
| if provider == "google": |
| llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0) |
| elif provider == "groq": |
| llm = ChatGroq(model="qwen-qwq-32b", temperature=0) |
| 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): |
| """Invoke LLM with system prompt prepended.""" |
| messages = [sys_msg] + state["messages"] |
| return {"messages": [llm_with_tools.invoke(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__": |
| graph = build_graph(provider="groq") |
| question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?" |
| messages = [HumanMessage(content=question)] |
| result = graph.invoke({"messages": messages}) |
| for m in result["messages"]: |
| m.pretty_print() |
|
|