Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- agent.py +35 -20
- requirements.txt +3 -1
agent.py
CHANGED
|
@@ -2,11 +2,11 @@ from langgraph.graph import StateGraph, END, START
|
|
| 2 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 3 |
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 langgraph.graph.message import add_messages
|
| 8 |
-
# 👇
|
| 9 |
-
from
|
| 10 |
import os
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
|
@@ -22,29 +22,32 @@ RECURSION_LIMIT = 5000
|
|
| 22 |
class AgentState(TypedDict):
|
| 23 |
messages: Annotated[List, add_messages]
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# -------------------------------------------------
|
| 28 |
-
#
|
| 29 |
# -------------------------------------------------
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
print("❌ CRITICAL ERROR: GOOGLE_API_KEY not found in environment variables!")
|
| 35 |
else:
|
| 36 |
-
print(f"✅
|
| 37 |
|
|
|
|
|
|
|
| 38 |
rate_limiter = InMemoryRateLimiter(
|
| 39 |
-
requests_per_second=
|
| 40 |
-
check_every_n_seconds=1,
|
| 41 |
-
max_bucket_size=
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# 👇
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
rate_limiter=rate_limiter,
|
| 49 |
temperature=0
|
| 50 |
).bind_tools(TOOLS)
|
|
@@ -64,6 +67,12 @@ Your job is to:
|
|
| 64 |
5. Read the server response and:
|
| 65 |
- If it contains a new quiz URL → fetch it immediately and continue.
|
| 66 |
- If no new URL is present → return "END".
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
STRICT RULES — FOLLOW EXACTLY:
|
| 69 |
|
|
@@ -74,6 +83,7 @@ GENERAL RULES:
|
|
| 74 |
- NEVER re-submit unless the server explicitly allows or it's within the 3-minute limit.
|
| 75 |
- ALWAYS inspect the server response before deciding what to do next.
|
| 76 |
- ALWAYS use the tools provided to fetch, scrape, download, render HTML, or send requests.
|
|
|
|
| 77 |
|
| 78 |
TIME LIMIT RULES:
|
| 79 |
- Each task has a hard 3-minute limit.
|
|
@@ -109,6 +119,7 @@ llm_with_prompt = prompt | llm
|
|
| 109 |
# AGENT NODE
|
| 110 |
# -------------------------------------------------
|
| 111 |
def agent_node(state: AgentState):
|
|
|
|
| 112 |
result = llm_with_prompt.invoke({"messages": state["messages"]})
|
| 113 |
return {"messages": state["messages"] + [result]}
|
| 114 |
|
|
@@ -136,6 +147,7 @@ def route(state):
|
|
| 136 |
elif isinstance(last, dict):
|
| 137 |
content = last.get("content")
|
| 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):
|
|
@@ -163,9 +175,12 @@ app = graph.compile()
|
|
| 163 |
# TEST FUNCTION
|
| 164 |
# -------------------------------------------------
|
| 165 |
def run_agent(url: str) -> str:
|
| 166 |
-
print(f"🚀 Starting Agent for URL: {url}")
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
| 169 |
config={"recursion_limit": RECURSION_LIMIT},
|
| 170 |
)
|
| 171 |
print("✅ Tasks completed successfully")
|
|
|
|
| 2 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 3 |
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, transcribe_audio
|
| 6 |
from typing import TypedDict, Annotated, List, Any
|
| 7 |
from langgraph.graph.message import add_messages
|
| 8 |
+
# 👇 Switch to Groq
|
| 9 |
+
from langchain_groq import ChatGroq
|
| 10 |
import os
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
|
|
|
| 22 |
class AgentState(TypedDict):
|
| 23 |
messages: Annotated[List, add_messages]
|
| 24 |
|
| 25 |
+
# Define your tools list
|
| 26 |
+
TOOLS = [run_code, get_rendered_html, download_file, post_request, add_dependencies, transcribe_audio]
|
| 27 |
|
| 28 |
# -------------------------------------------------
|
| 29 |
+
# GROQ LLM SETUP
|
| 30 |
# -------------------------------------------------
|
| 31 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 32 |
|
| 33 |
+
if not GROQ_API_KEY:
|
| 34 |
+
print("❌ CRITICAL ERROR: GROQ_API_KEY not found in environment variables!")
|
|
|
|
| 35 |
else:
|
| 36 |
+
print(f"✅ GROQ_API_KEY found")
|
| 37 |
|
| 38 |
+
# Groq Free Tier often allows ~30 requests per minute.
|
| 39 |
+
# We set a limiter to be safe (e.g., 1 request every 2 seconds).
|
| 40 |
rate_limiter = InMemoryRateLimiter(
|
| 41 |
+
requests_per_second=30/60, # 0.5 requests per second
|
| 42 |
+
check_every_n_seconds=0.1,
|
| 43 |
+
max_bucket_size=1
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 👇 Using Llama 3.3 70B (High intelligence, currently free on Groq)
|
| 47 |
+
# If you hit Token Limits (TPM), switch model to "llama-3.1-8b-instant"
|
| 48 |
+
llm = ChatGroq(
|
| 49 |
+
model="llama-3.3-70b-versatile",
|
| 50 |
+
api_key=GROQ_API_KEY,
|
| 51 |
rate_limiter=rate_limiter,
|
| 52 |
temperature=0
|
| 53 |
).bind_tools(TOOLS)
|
|
|
|
| 67 |
5. Read the server response and:
|
| 68 |
- If it contains a new quiz URL → fetch it immediately and continue.
|
| 69 |
- If no new URL is present → return "END".
|
| 70 |
+
|
| 71 |
+
AUDIO TASKS:
|
| 72 |
+
- If you encounter an audio file (mp3, wav), you MUST:
|
| 73 |
+
1. Use 'download_file' to save it.
|
| 74 |
+
2. Use 'transcribe_audio' on the saved filename to get the text.
|
| 75 |
+
3. Use the transcribed text as the answer (or part of the answer).
|
| 76 |
|
| 77 |
STRICT RULES — FOLLOW EXACTLY:
|
| 78 |
|
|
|
|
| 83 |
- NEVER re-submit unless the server explicitly allows or it's within the 3-minute limit.
|
| 84 |
- ALWAYS inspect the server response before deciding what to do next.
|
| 85 |
- ALWAYS use the tools provided to fetch, scrape, download, render HTML, or send requests.
|
| 86 |
+
- **IMPORTANT**: If the HTML content is too large, focus only on the relevant forms and instructions.
|
| 87 |
|
| 88 |
TIME LIMIT RULES:
|
| 89 |
- Each task has a hard 3-minute limit.
|
|
|
|
| 119 |
# AGENT NODE
|
| 120 |
# -------------------------------------------------
|
| 121 |
def agent_node(state: AgentState):
|
| 122 |
+
# Invoke the LLM
|
| 123 |
result = llm_with_prompt.invoke({"messages": state["messages"]})
|
| 124 |
return {"messages": state["messages"] + [result]}
|
| 125 |
|
|
|
|
| 147 |
elif isinstance(last, dict):
|
| 148 |
content = last.get("content")
|
| 149 |
|
| 150 |
+
# Check for END signal
|
| 151 |
if isinstance(content, str) and content.strip() == "END":
|
| 152 |
return END
|
| 153 |
if isinstance(content, list) and len(content) > 0 and isinstance(content[0], dict):
|
|
|
|
| 175 |
# TEST FUNCTION
|
| 176 |
# -------------------------------------------------
|
| 177 |
def run_agent(url: str) -> str:
|
| 178 |
+
print(f"🚀 Starting Groq Agent for URL: {url}")
|
| 179 |
+
# Initialize with user message
|
| 180 |
+
initial_message = {"role": "user", "content": url}
|
| 181 |
+
|
| 182 |
+
app.invoke(
|
| 183 |
+
{"messages": [initial_message]},
|
| 184 |
config={"recursion_limit": RECURSION_LIMIT},
|
| 185 |
)
|
| 186 |
print("✅ Tasks completed successfully")
|
requirements.txt
CHANGED
|
@@ -14,4 +14,6 @@ matplotlib
|
|
| 14 |
pandas
|
| 15 |
pypdf2
|
| 16 |
numpy
|
| 17 |
-
google-genai
|
|
|
|
|
|
|
|
|
| 14 |
pandas
|
| 15 |
pypdf2
|
| 16 |
numpy
|
| 17 |
+
google-genai
|
| 18 |
+
langchain-groq
|
| 19 |
+
langchain-ollama
|