Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import pandas as pd
|
|
| 5 |
from typing import TypedDict, Annotated, Sequence
|
| 6 |
import operator
|
| 7 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
-
from langchain_community.tools
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
from langgraph.graph import StateGraph, END
|
| 11 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
@@ -21,26 +21,23 @@ def create_langgraph_agent():
|
|
| 21 |
print("Initializing LangGraph Agent with OpenAI...")
|
| 22 |
|
| 23 |
# 1. Set up the LLM (The "Brain") using OpenAI's GPT-3.5 Turbo
|
| 24 |
-
# The API key is automatically found from the OPENAI_API_KEY secret
|
| 25 |
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
tools = [
|
| 29 |
llm_with_tools = llm.bind_tools(tools)
|
| 30 |
print("LLM and tools initialized.")
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
# The 'agent' node calls the LLM
|
| 34 |
def agent_node(state):
|
| 35 |
print("Calling agent node...")
|
| 36 |
response = llm_with_tools.invoke(state["messages"])
|
| 37 |
return {"messages": [response]}
|
| 38 |
|
| 39 |
-
# The 'tool' node executes the tools
|
| 40 |
tool_node = ToolNode(tools)
|
| 41 |
print("Graph nodes defined.")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
graph = StateGraph(AgentState)
|
| 45 |
graph.add_node("agent", agent_node)
|
| 46 |
graph.add_node("tools", tool_node)
|
|
@@ -49,7 +46,7 @@ def create_langgraph_agent():
|
|
| 49 |
graph.add_conditional_edges("agent", tools_condition)
|
| 50 |
graph.add_edge("tools", "agent")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
app = graph.compile()
|
| 54 |
print("LangGraph agent compiled and ready.")
|
| 55 |
return app
|
|
@@ -74,10 +71,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 74 |
return "Please Login to Hugging Face with the button.", None
|
| 75 |
username = f"{profile.username}"
|
| 76 |
|
| 77 |
-
# Tavily API key check
|
| 78 |
-
if not os.getenv("TAVILY_API_KEY"):
|
| 79 |
-
return "Tavily API key not found. Please set the TAVILY_API_KEY secret in your Space settings.", None
|
| 80 |
-
# OpenAI API key check
|
| 81 |
if not os.getenv("OPENAI_API_KEY"):
|
| 82 |
return "OpenAI API key not found. Please set the OPENAI_API_KEY secret in your Space settings.", None
|
| 83 |
|
|
@@ -122,7 +115,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 122 |
|
| 123 |
# Gradio Interface
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
-
gr.Markdown("# Agent Evaluation Runner (OpenAI +
|
| 126 |
gr.LoginButton()
|
| 127 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 128 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 5 |
from typing import TypedDict, Annotated, Sequence
|
| 6 |
import operator
|
| 7 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
from langgraph.graph import StateGraph, END
|
| 11 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
|
|
| 21 |
print("Initializing LangGraph Agent with OpenAI...")
|
| 22 |
|
| 23 |
# 1. Set up the LLM (The "Brain") using OpenAI's GPT-3.5 Turbo
|
|
|
|
| 24 |
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
| 25 |
|
| 26 |
+
# 2. Define the Tools, now using DuckDuckGo
|
| 27 |
+
tools = [DuckDuckGoSearchRun()]
|
| 28 |
llm_with_tools = llm.bind_tools(tools)
|
| 29 |
print("LLM and tools initialized.")
|
| 30 |
|
| 31 |
+
# 3. Define the Graph Nodes
|
|
|
|
| 32 |
def agent_node(state):
|
| 33 |
print("Calling agent node...")
|
| 34 |
response = llm_with_tools.invoke(state["messages"])
|
| 35 |
return {"messages": [response]}
|
| 36 |
|
|
|
|
| 37 |
tool_node = ToolNode(tools)
|
| 38 |
print("Graph nodes defined.")
|
| 39 |
|
| 40 |
+
# 4. Define the Graph
|
| 41 |
graph = StateGraph(AgentState)
|
| 42 |
graph.add_node("agent", agent_node)
|
| 43 |
graph.add_node("tools", tool_node)
|
|
|
|
| 46 |
graph.add_conditional_edges("agent", tools_condition)
|
| 47 |
graph.add_edge("tools", "agent")
|
| 48 |
|
| 49 |
+
# 5. Compile the graph into a runnable app
|
| 50 |
app = graph.compile()
|
| 51 |
print("LangGraph agent compiled and ready.")
|
| 52 |
return app
|
|
|
|
| 71 |
return "Please Login to Hugging Face with the button.", None
|
| 72 |
username = f"{profile.username}"
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if not os.getenv("OPENAI_API_KEY"):
|
| 75 |
return "OpenAI API key not found. Please set the OPENAI_API_KEY secret in your Space settings.", None
|
| 76 |
|
|
|
|
| 115 |
|
| 116 |
# Gradio Interface
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
+
gr.Markdown("# Agent Evaluation Runner (OpenAI + DuckDuckGo)")
|
| 119 |
gr.LoginButton()
|
| 120 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 121 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|