Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""LangGraph Agent (No Supabase)"""
|
| 2 |
import os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 5 |
from langgraph.prebuilt import tools_condition, ToolNode
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
@@ -11,38 +10,30 @@ from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
|
| 11 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 12 |
from langchain_core.tools import tool
|
| 13 |
|
| 14 |
-
load_dotenv()
|
| 15 |
-
|
| 16 |
@tool
|
| 17 |
def multiply(a: int, b: int) -> int:
|
| 18 |
-
"""Multiply two integers and return the result."""
|
| 19 |
return a * b
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def add(a: int, b: int) -> int:
|
| 23 |
-
"""Add two integers and return the result."""
|
| 24 |
return a + b
|
| 25 |
|
| 26 |
@tool
|
| 27 |
def subtract(a: int, b: int) -> int:
|
| 28 |
-
"""Subtract b from a and return the result."""
|
| 29 |
return a - b
|
| 30 |
|
| 31 |
@tool
|
| 32 |
def divide(a: int, b: int) -> float:
|
| 33 |
-
"""Divide a by b and return the result."""
|
| 34 |
if b == 0:
|
| 35 |
raise ValueError("Cannot divide by zero.")
|
| 36 |
return a / b
|
| 37 |
|
| 38 |
@tool
|
| 39 |
def modulus(a: int, b: int) -> int:
|
| 40 |
-
"""Return the modulus of a and b."""
|
| 41 |
return a % b
|
| 42 |
|
| 43 |
@tool
|
| 44 |
def wiki_search(query: str) -> dict:
|
| 45 |
-
"""Search Wikipedia for a query and return up to 2 results."""
|
| 46 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 47 |
results = "\n\n---\n\n".join(
|
| 48 |
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs
|
|
@@ -51,7 +42,6 @@ def wiki_search(query: str) -> dict:
|
|
| 51 |
|
| 52 |
@tool
|
| 53 |
def web_search(query: str) -> dict:
|
| 54 |
-
"""Search the web via Tavily and return up to 3 results."""
|
| 55 |
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 56 |
results = "\n\n---\n\n".join(
|
| 57 |
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs
|
|
@@ -60,7 +50,6 @@ def web_search(query: str) -> dict:
|
|
| 60 |
|
| 61 |
@tool
|
| 62 |
def arvix_search(query: str) -> dict:
|
| 63 |
-
"""Search Arxiv and return up to 3 truncated results."""
|
| 64 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 65 |
results = "\n\n---\n\n".join(
|
| 66 |
f"<Document>\n{doc.page_content[:500]}\n</Document>" for doc in search_docs
|
|
@@ -83,7 +72,10 @@ def build_graph(provider: str = "groq"):
|
|
| 83 |
if provider == "google":
|
| 84 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 85 |
elif provider == "groq":
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
elif provider == "huggingface":
|
| 88 |
llm = ChatHuggingFace(
|
| 89 |
llm=HuggingFaceEndpoint(
|
|
|
|
| 1 |
"""LangGraph Agent (No Supabase)"""
|
| 2 |
import os
|
|
|
|
| 3 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
from langgraph.prebuilt import tools_condition, ToolNode
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
| 10 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 11 |
from langchain_core.tools import tool
|
| 12 |
|
|
|
|
|
|
|
| 13 |
@tool
|
| 14 |
def multiply(a: int, b: int) -> int:
|
|
|
|
| 15 |
return a * b
|
| 16 |
|
| 17 |
@tool
|
| 18 |
def add(a: int, b: int) -> int:
|
|
|
|
| 19 |
return a + b
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def subtract(a: int, b: int) -> int:
|
|
|
|
| 23 |
return a - b
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def divide(a: int, b: int) -> float:
|
|
|
|
| 27 |
if b == 0:
|
| 28 |
raise ValueError("Cannot divide by zero.")
|
| 29 |
return a / b
|
| 30 |
|
| 31 |
@tool
|
| 32 |
def modulus(a: int, b: int) -> int:
|
|
|
|
| 33 |
return a % b
|
| 34 |
|
| 35 |
@tool
|
| 36 |
def wiki_search(query: str) -> dict:
|
|
|
|
| 37 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 38 |
results = "\n\n---\n\n".join(
|
| 39 |
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs
|
|
|
|
| 42 |
|
| 43 |
@tool
|
| 44 |
def web_search(query: str) -> dict:
|
|
|
|
| 45 |
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 46 |
results = "\n\n---\n\n".join(
|
| 47 |
f"<Document>\n{doc.page_content}\n</Document>" for doc in search_docs
|
|
|
|
| 50 |
|
| 51 |
@tool
|
| 52 |
def arvix_search(query: str) -> dict:
|
|
|
|
| 53 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 54 |
results = "\n\n---\n\n".join(
|
| 55 |
f"<Document>\n{doc.page_content[:500]}\n</Document>" for doc in search_docs
|
|
|
|
| 72 |
if provider == "google":
|
| 73 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 74 |
elif provider == "groq":
|
| 75 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 76 |
+
if not groq_api_key:
|
| 77 |
+
raise ValueError("GROQ_API_KEY is not set in the environment.")
|
| 78 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0, api_key=groq_api_key)
|
| 79 |
elif provider == "huggingface":
|
| 80 |
llm = ChatHuggingFace(
|
| 81 |
llm=HuggingFaceEndpoint(
|