Spaces:
Sleeping
Sleeping
Upload agent.py
Browse files
agent.py
CHANGED
|
@@ -4,39 +4,50 @@ from langgraph.prebuilt import ToolNode
|
|
| 4 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 5 |
from tools import get_rendered_html, download_file, post_request, run_code, add_dependencies
|
| 6 |
from typing import TypedDict, Annotated, List, Any
|
| 7 |
-
from langchain.chat_models import init_chat_model
|
| 8 |
from langgraph.graph.message import add_messages
|
|
|
|
|
|
|
| 9 |
import os
|
| 10 |
from dotenv import load_dotenv
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
EMAIL = os.getenv("EMAIL")
|
| 14 |
SECRET = os.getenv("SECRET")
|
| 15 |
-
RECURSION_LIMIT =
|
|
|
|
| 16 |
# -------------------------------------------------
|
| 17 |
# STATE
|
| 18 |
# -------------------------------------------------
|
| 19 |
class AgentState(TypedDict):
|
| 20 |
messages: Annotated[List, add_messages]
|
| 21 |
|
| 22 |
-
|
| 23 |
TOOLS = [run_code, get_rendered_html, download_file, post_request, add_dependencies]
|
| 24 |
|
| 25 |
-
|
| 26 |
# -------------------------------------------------
|
| 27 |
-
# GEMINI LLM
|
| 28 |
# -------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
rate_limiter = InMemoryRateLimiter(
|
| 30 |
requests_per_second=9/60,
|
| 31 |
check_every_n_seconds=1,
|
| 32 |
max_bucket_size=9
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
# -------------------------------------------------
|
|
@@ -107,7 +118,8 @@ def agent_node(state: AgentState):
|
|
| 107 |
# -------------------------------------------------
|
| 108 |
def route(state):
|
| 109 |
last = state["messages"][-1]
|
| 110 |
-
|
|
|
|
| 111 |
tool_calls = None
|
| 112 |
if hasattr(last, "tool_calls"):
|
| 113 |
tool_calls = getattr(last, "tool_calls", None)
|
|
@@ -116,7 +128,8 @@ def route(state):
|
|
| 116 |
|
| 117 |
if tool_calls:
|
| 118 |
return "tools"
|
| 119 |
-
|
|
|
|
| 120 |
content = None
|
| 121 |
if hasattr(last, "content"):
|
| 122 |
content = getattr(last, "content", None)
|
|
@@ -125,16 +138,17 @@ def route(state):
|
|
| 125 |
|
| 126 |
if isinstance(content, str) and content.strip() == "END":
|
| 127 |
return END
|
| 128 |
-
if isinstance(content, list) and content[0]
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
return "agent"
|
|
|
|
| 131 |
graph = StateGraph(AgentState)
|
| 132 |
|
| 133 |
graph.add_node("agent", agent_node)
|
| 134 |
graph.add_node("tools", ToolNode(TOOLS))
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
graph.add_edge(START, "agent")
|
| 139 |
graph.add_edge("tools", "agent")
|
| 140 |
graph.add_conditional_edges(
|
|
@@ -146,12 +160,12 @@ app = graph.compile()
|
|
| 146 |
|
| 147 |
|
| 148 |
# -------------------------------------------------
|
| 149 |
-
# TEST
|
| 150 |
# -------------------------------------------------
|
| 151 |
def run_agent(url: str) -> str:
|
|
|
|
| 152 |
app.invoke({
|
| 153 |
"messages": [{"role": "user", "content": url}]},
|
| 154 |
config={"recursion_limit": RECURSION_LIMIT},
|
| 155 |
)
|
| 156 |
-
print("Tasks completed
|
| 157 |
-
|
|
|
|
| 4 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 5 |
from tools import get_rendered_html, download_file, post_request, run_code, add_dependencies
|
| 6 |
from typing import TypedDict, Annotated, List, Any
|
|
|
|
| 7 |
from langgraph.graph.message import add_messages
|
| 8 |
+
# 👇 सीधे क्लास इम्पोर्ट करें (init_chat_model की जगह)
|
| 9 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
import os
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
+
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
EMAIL = os.getenv("EMAIL")
|
| 16 |
SECRET = os.getenv("SECRET")
|
| 17 |
+
RECURSION_LIMIT = 5000
|
| 18 |
+
|
| 19 |
# -------------------------------------------------
|
| 20 |
# STATE
|
| 21 |
# -------------------------------------------------
|
| 22 |
class AgentState(TypedDict):
|
| 23 |
messages: Annotated[List, add_messages]
|
| 24 |
|
|
|
|
| 25 |
TOOLS = [run_code, get_rendered_html, download_file, post_request, add_dependencies]
|
| 26 |
|
|
|
|
| 27 |
# -------------------------------------------------
|
| 28 |
+
# GEMINI LLM SETUP (FIXED)
|
| 29 |
# -------------------------------------------------
|
| 30 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 31 |
+
|
| 32 |
+
# Debugging check to ensure key exists
|
| 33 |
+
if not GOOGLE_API_KEY:
|
| 34 |
+
print("❌ CRITICAL ERROR: GOOGLE_API_KEY not found in environment variables!")
|
| 35 |
+
else:
|
| 36 |
+
print(f"✅ GOOGLE_API_KEY found (Length: {len(GOOGLE_API_KEY)})")
|
| 37 |
+
|
| 38 |
rate_limiter = InMemoryRateLimiter(
|
| 39 |
requests_per_second=9/60,
|
| 40 |
check_every_n_seconds=1,
|
| 41 |
max_bucket_size=9
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
# 👇 Direct Instantiation to fix Auth Error
|
| 45 |
+
llm = ChatGoogleGenerativeAI(
|
| 46 |
+
model="gemini-2.5-flash", # Stable model version
|
| 47 |
+
google_api_key=GOOGLE_API_KEY, # Explicitly passing key param
|
| 48 |
+
rate_limiter=rate_limiter,
|
| 49 |
+
temperature=0
|
| 50 |
+
).bind_tools(TOOLS)
|
| 51 |
|
| 52 |
|
| 53 |
# -------------------------------------------------
|
|
|
|
| 118 |
# -------------------------------------------------
|
| 119 |
def route(state):
|
| 120 |
last = state["messages"][-1]
|
| 121 |
+
|
| 122 |
+
# Robust tool call check
|
| 123 |
tool_calls = None
|
| 124 |
if hasattr(last, "tool_calls"):
|
| 125 |
tool_calls = getattr(last, "tool_calls", None)
|
|
|
|
| 128 |
|
| 129 |
if tool_calls:
|
| 130 |
return "tools"
|
| 131 |
+
|
| 132 |
+
# Robust content check
|
| 133 |
content = None
|
| 134 |
if hasattr(last, "content"):
|
| 135 |
content = getattr(last, "content", None)
|
|
|
|
| 138 |
|
| 139 |
if isinstance(content, str) and content.strip() == "END":
|
| 140 |
return END
|
| 141 |
+
if isinstance(content, list) and len(content) > 0 and isinstance(content[0], dict):
|
| 142 |
+
if content[0].get("text", "").strip() == "END":
|
| 143 |
+
return END
|
| 144 |
+
|
| 145 |
return "agent"
|
| 146 |
+
|
| 147 |
graph = StateGraph(AgentState)
|
| 148 |
|
| 149 |
graph.add_node("agent", agent_node)
|
| 150 |
graph.add_node("tools", ToolNode(TOOLS))
|
| 151 |
|
|
|
|
|
|
|
| 152 |
graph.add_edge(START, "agent")
|
| 153 |
graph.add_edge("tools", "agent")
|
| 154 |
graph.add_conditional_edges(
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
# -------------------------------------------------
|
| 163 |
+
# TEST FUNCTION
|
| 164 |
# -------------------------------------------------
|
| 165 |
def run_agent(url: str) -> str:
|
| 166 |
+
print(f"🚀 Starting Agent for URL: {url}")
|
| 167 |
app.invoke({
|
| 168 |
"messages": [{"role": "user", "content": url}]},
|
| 169 |
config={"recursion_limit": RECURSION_LIMIT},
|
| 170 |
)
|
| 171 |
+
print("✅ Tasks completed successfully")
|
|
|