Spaces:
Sleeping
Sleeping
introduce two agent type , react and normal. Change the graph
Browse files- Agents/agent.py +12 -4
- app.py +22 -2
Agents/agent.py
CHANGED
|
@@ -11,6 +11,7 @@ from langchain.tools.retriever import create_retriever_tool
|
|
| 11 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 12 |
from langfuse.langchain import CallbackHandler
|
| 13 |
from langgraph.graph import END
|
|
|
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
@@ -65,6 +66,16 @@ tools = [
|
|
| 65 |
remainder
|
| 66 |
]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def build_graph(provider: str="groq"):
|
| 69 |
if provider== "groq":
|
| 70 |
print(f"Running llm groq")
|
|
@@ -117,10 +128,7 @@ def build_graph(provider: str="groq"):
|
|
| 117 |
builder.add_conditional_edges(
|
| 118 |
"assistant",
|
| 119 |
tools_condition,
|
| 120 |
-
|
| 121 |
-
True: "tools",
|
| 122 |
-
False: END
|
| 123 |
-
}
|
| 124 |
)
|
| 125 |
builder.add_edge("tools","assistant")
|
| 126 |
|
|
|
|
| 11 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 12 |
from langfuse.langchain import CallbackHandler
|
| 13 |
from langgraph.graph import END
|
| 14 |
+
from langgraph.prebuilt import create_react_agent
|
| 15 |
|
| 16 |
load_dotenv()
|
| 17 |
|
|
|
|
| 66 |
remainder
|
| 67 |
]
|
| 68 |
|
| 69 |
+
def use_react_agent():
|
| 70 |
+
graph = create_react_agent(
|
| 71 |
+
"anthropic:claude-3-7-sonnet-latest",
|
| 72 |
+
tools=[check_weather],
|
| 73 |
+
prompt="You are a helpful assistant",
|
| 74 |
+
)
|
| 75 |
+
inputs = {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
|
| 76 |
+
for chunk in graph.stream(inputs, stream_mode="updates"):
|
| 77 |
+
print(chunk)
|
| 78 |
+
|
| 79 |
def build_graph(provider: str="groq"):
|
| 80 |
if provider== "groq":
|
| 81 |
print(f"Running llm groq")
|
|
|
|
| 128 |
builder.add_conditional_edges(
|
| 129 |
"assistant",
|
| 130 |
tools_condition,
|
| 131 |
+
{"tools": "tools", "__end__": "__end__"}
|
|
|
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
builder.add_edge("tools","assistant")
|
| 134 |
|
app.py
CHANGED
|
@@ -4,8 +4,9 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from langchain_core.messages import HumanMessage
|
| 7 |
-
from Agents.agent import build_graph
|
| 8 |
from langfuse.langchain import CallbackHandler
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
@@ -13,9 +14,25 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
langfuse_handler = CallbackHandler()
|
| 14 |
testMode = bool(os.getenv("TestMode"))
|
| 15 |
langFuseOn = bool(os.getenv("LangFuseOn"))
|
|
|
|
| 16 |
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
print("BasicAgent initialized.")
|
|
@@ -63,7 +80,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 63 |
|
| 64 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 65 |
try:
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
print(f"Error instantiating agent: {e}")
|
| 69 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from langchain_core.messages import HumanMessage
|
| 7 |
+
from Agents.agent import build_graph, tools,systemPrompt
|
| 8 |
from langfuse.langchain import CallbackHandler
|
| 9 |
+
from langgraph.prebuilt import create_react_agent
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 14 |
langfuse_handler = CallbackHandler()
|
| 15 |
testMode = bool(os.getenv("TestMode"))
|
| 16 |
langFuseOn = bool(os.getenv("LangFuseOn"))
|
| 17 |
+
agentType = os.getenv("AgentType")
|
| 18 |
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
+
class ReactAgent:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
print(f"tools: {tools}")
|
| 24 |
+
print(f"prompt : {systemPrompt}")
|
| 25 |
+
graph = create_react_agent(
|
| 26 |
+
"anthropic:claude-3-7-sonnet-latest",
|
| 27 |
+
tools=tools,
|
| 28 |
+
prompt=systemPrompt,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def __call__(self,question:str) -> str:
|
| 32 |
+
inputs = {"messages": [{"role": "user", "content": "{question}"}]}
|
| 33 |
+
for chunk in self.graph.stream(inputs, stream_mode="updates"):
|
| 34 |
+
print(chunk)
|
| 35 |
+
|
| 36 |
class BasicAgent:
|
| 37 |
def __init__(self):
|
| 38 |
print("BasicAgent initialized.")
|
|
|
|
| 80 |
|
| 81 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 82 |
try:
|
| 83 |
+
if(agentType == "React"):
|
| 84 |
+
agent = ReactAgent()
|
| 85 |
+
else:
|
| 86 |
+
agent = BasicAgent()
|
| 87 |
except Exception as e:
|
| 88 |
print(f"Error instantiating agent: {e}")
|
| 89 |
return f"Error initializing agent: {e}", None
|