create agent
#389
by manasajanj - opened
agent
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 6 |
+
from langgraph.constants import END
|
| 7 |
+
from langgraph.prebuilt import ToolNode
|
| 8 |
+
|
| 9 |
+
from prompts import AGENT_SYSTEM_PROMPT, build_question_prompt
|
| 10 |
+
from state import AgentState
|
| 11 |
+
from tools import wikipedia_tool, arxiv_tool, get_current_year, ddg_search_tool
|
| 12 |
+
from langchain_groq import ChatGroq
|
| 13 |
+
|
| 14 |
+
from langgraph.graph import StateGraph
|
| 15 |
+
|
| 16 |
+
load_dotenv()
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
+
HF_USERNAME = os.getenv("HF_USERNAME")
|
| 19 |
+
RECURSION_LIMIT = 50
|
| 20 |
+
AGENT_CODE_URL = os.getenv("AGENT_CODE_URL", "https://huggingface.co/spaces/manasajanj/Final_Assignment_Template/tree/main")
|
| 21 |
+
llm = ChatGroq(model="openai/gpt-oss-safeguard-20b")
|
| 22 |
+
tools_list = [wikipedia_tool, ddg_search_tool, arxiv_tool, get_current_year]
|
| 23 |
+
llm_with_tools = llm.bind_tools(tools_list)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def agent_node(state:AgentState) -> dict:
|
| 27 |
+
messages = state["messages"]
|
| 28 |
+
system_message = SystemMessage(content=AGENT_SYSTEM_PROMPT)
|
| 29 |
+
has_system_message = any(isinstance(m, SystemMessage) for m in messages)
|
| 30 |
+
if not has_system_message:
|
| 31 |
+
messages = [system_message]+messages
|
| 32 |
+
response = llm_with_tools.invoke(messages)
|
| 33 |
+
return {"messages": [response]}
|
| 34 |
+
def finish_node(state:AgentState) -> dict:
|
| 35 |
+
last_message = state["messages"][-1]
|
| 36 |
+
return {"answer": last_message.content.strip()}
|
| 37 |
+
def should_continue(state: AgentState) -> str:
|
| 38 |
+
last = state["messages"][-1]
|
| 39 |
+
if hasattr(last, "tool_calls") and last.tool_calls:
|
| 40 |
+
return "tools"
|
| 41 |
+
return "end"
|
| 42 |
+
def build_graph() -> StateGraph:
|
| 43 |
+
graph = StateGraph(AgentState)
|
| 44 |
+
graph.add_node("agent", agent_node)
|
| 45 |
+
graph.add_node("tools", ToolNode(tools_list))
|
| 46 |
+
graph.add_node("finish", finish_node)
|
| 47 |
+
|
| 48 |
+
graph.set_entry_point("agent")
|
| 49 |
+
graph.add_conditional_edges("agent", should_continue, {"tools":"tools", "end":"finish"})
|
| 50 |
+
|
| 51 |
+
graph.add_edge("tools", "agent") # go back to agent after tools
|
| 52 |
+
|
| 53 |
+
graph.add_edge("finish", END)
|
| 54 |
+
return graph.compile()
|
| 55 |
+
app = build_graph()
|
| 56 |
+
|
| 57 |
+
def solve_question(task: dict) -> str:
|
| 58 |
+
question = task["question"]
|
| 59 |
+
file_name = task.get("file_name") or ""
|
| 60 |
+
user_content = build_question_prompt(
|
| 61 |
+
question=question,
|
| 62 |
+
file_name=file_name)
|
| 63 |
+
initial_state: AgentState = AgentState(
|
| 64 |
+
messages = [HumanMessage(content=user_content)],
|
| 65 |
+
task_id = task["task_id"],
|
| 66 |
+
question = question,
|
| 67 |
+
file_name=file_name or None,
|
| 68 |
+
answer=None,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
result = app.invoke(initial_state, config={"recursion_limit":RECURSION_LIMIT})
|
| 72 |
+
return result['answer']
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|