Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import operator
|
|
| 7 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
from langchain.agents import AgentExecutor
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
-
from langchain_community.tools.Youtube import YouTubeSearchTool
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langgraph.graph import StateGraph, END
|
|
@@ -15,32 +15,32 @@ from langgraph.prebuilt import ToolNode, tools_condition
|
|
| 15 |
|
| 16 |
# --- Main Application Logic ---
|
| 17 |
|
| 18 |
-
#
|
| 19 |
class AgentState(TypedDict):
|
| 20 |
messages: Annotated[Sequence[BaseMessage], operator.add]
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def create_langgraph_agent():
|
| 24 |
print("Initializing Advanced LangGraph Agent...")
|
| 25 |
|
| 26 |
-
# 1.
|
| 27 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 28 |
|
| 29 |
-
# 2.
|
| 30 |
tools = [TavilySearchResults(max_results=3), PythonREPLTool(), YouTubeSearchTool()]
|
| 31 |
llm_with_tools = llm.bind_tools(tools)
|
| 32 |
print("LLM and tools initialized.")
|
| 33 |
|
| 34 |
-
# 3.
|
| 35 |
def agent_node(state):
|
| 36 |
print("Calling agent node...")
|
| 37 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 38 |
|
| 39 |
-
# 4.
|
| 40 |
tool_node = ToolNode(tools)
|
| 41 |
print("Graph nodes defined.")
|
| 42 |
|
| 43 |
-
# 5.
|
| 44 |
graph = StateGraph(AgentState)
|
| 45 |
graph.add_node("agent", agent_node)
|
| 46 |
graph.add_node("tools", tool_node)
|
|
@@ -48,12 +48,12 @@ def create_langgraph_agent():
|
|
| 48 |
graph.add_conditional_edges("agent", tools_condition)
|
| 49 |
graph.add_edge("tools", "agent")
|
| 50 |
|
| 51 |
-
# 6.
|
| 52 |
app = graph.compile(recursion_limit=15)
|
| 53 |
print("LangGraph agent compiled and ready.")
|
| 54 |
return app
|
| 55 |
|
| 56 |
-
#
|
| 57 |
def run_agent(agent_executor, question: str) -> str:
|
| 58 |
print(f"Agent received question: {question}")
|
| 59 |
final_answer = ""
|
|
@@ -74,7 +74,7 @@ def run_agent(agent_executor, question: str) -> str:
|
|
| 74 |
print(f"Agent returning answer: {final_answer}")
|
| 75 |
return str(final_answer)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 79 |
space_id = os.getenv("SPACE_ID")
|
| 80 |
if not profile:
|
|
@@ -122,9 +122,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 122 |
except Exception as e:
|
| 123 |
return f"Error during submission: {e}", pd.DataFrame(answers_payload)
|
| 124 |
|
| 125 |
-
# Gradio
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
-
gr.Markdown("# Agent Evaluation Runner (Advanced Tools)")
|
| 128 |
gr.LoginButton()
|
| 129 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 130 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 7 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
from langchain.agents import AgentExecutor
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
+
from langchain_community.tools.Youtube import YouTubeSearchTool
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langgraph.graph import StateGraph, END
|
|
|
|
| 15 |
|
| 16 |
# --- Main Application Logic ---
|
| 17 |
|
| 18 |
+
# Agentin muisti
|
| 19 |
class AgentState(TypedDict):
|
| 20 |
messages: Annotated[Sequence[BaseMessage], operator.add]
|
| 21 |
|
| 22 |
+
# Agentin rakentajafunktio
|
| 23 |
def create_langgraph_agent():
|
| 24 |
print("Initializing Advanced LangGraph Agent...")
|
| 25 |
|
| 26 |
+
# 1. Kielimalli (GPT-4o on paras valinta)
|
| 27 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 28 |
|
| 29 |
+
# 2. Työkalut: Tavily, PythonREPL ja YouTube
|
| 30 |
tools = [TavilySearchResults(max_results=3), PythonREPLTool(), YouTubeSearchTool()]
|
| 31 |
llm_with_tools = llm.bind_tools(tools)
|
| 32 |
print("LLM and tools initialized.")
|
| 33 |
|
| 34 |
+
# 3. Agentin solmu (kutsuu kielimallia)
|
| 35 |
def agent_node(state):
|
| 36 |
print("Calling agent node...")
|
| 37 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 38 |
|
| 39 |
+
# 4. Työkalusolmu
|
| 40 |
tool_node = ToolNode(tools)
|
| 41 |
print("Graph nodes defined.")
|
| 42 |
|
| 43 |
+
# 5. Graafin määritys
|
| 44 |
graph = StateGraph(AgentState)
|
| 45 |
graph.add_node("agent", agent_node)
|
| 46 |
graph.add_node("tools", tool_node)
|
|
|
|
| 48 |
graph.add_conditional_edges("agent", tools_condition)
|
| 49 |
graph.add_edge("tools", "agent")
|
| 50 |
|
| 51 |
+
# 6. Graafin kääntäminen ja turvarajan asettaminen
|
| 52 |
app = graph.compile(recursion_limit=15)
|
| 53 |
print("LangGraph agent compiled and ready.")
|
| 54 |
return app
|
| 55 |
|
| 56 |
+
# Agentin suoritusfunktio
|
| 57 |
def run_agent(agent_executor, question: str) -> str:
|
| 58 |
print(f"Agent received question: {question}")
|
| 59 |
final_answer = ""
|
|
|
|
| 74 |
print(f"Agent returning answer: {final_answer}")
|
| 75 |
return str(final_answer)
|
| 76 |
|
| 77 |
+
# Evaluaation ajaminen
|
| 78 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 79 |
space_id = os.getenv("SPACE_ID")
|
| 80 |
if not profile:
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
return f"Error during submission: {e}", pd.DataFrame(answers_payload)
|
| 124 |
|
| 125 |
+
# Gradio-käyttöliittymä
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
+
gr.Markdown("# Agent Evaluation Runner (Advanced Tools - Corrected)")
|
| 128 |
gr.LoginButton()
|
| 129 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 130 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|