mrhenu commited on
Commit
e6e5669
·
verified ·
1 Parent(s): 23dcd7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -5,8 +5,8 @@ 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 import DuckDuckGoSearchRun
9
- from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
10
  from langgraph.graph import StateGraph, END
11
  from langgraph.prebuilt import ToolNode, tools_condition
12
 
@@ -16,35 +16,31 @@ from langgraph.prebuilt import ToolNode, tools_condition
16
  class AgentState(TypedDict):
17
  messages: Annotated[Sequence[BaseMessage], operator.add]
18
 
19
- # This function builds our final, robust agent using LangGraph.
20
  def create_langgraph_agent():
21
- print("Initializing LangGraph Agent...")
22
 
23
- # 1. Set up the LLM Endpoint connection
24
- llm_endpoint = HuggingFaceEndpoint(
25
- repo_id="deepseek-ai/deepseek-coder-6.7b-instruct",
26
- task="conversational",
27
- max_new_tokens=1024,
28
- do_sample=False,
29
- )
30
 
31
- # 2. Wrap the endpoint in the ChatHuggingFace class to make it a chat model
32
- # and bind the tools to it.
33
- tools = [DuckDuckGoSearchRun()]
34
- chat_model = ChatHuggingFace(llm=llm_endpoint)
35
- chat_model_with_tools = chat_model.bind_tools(tools)
36
  print("LLM and tools initialized.")
37
 
38
- # 3. Define the agent's logic (the "agent" node)
 
39
  def agent_node(state):
40
  print("Calling agent node...")
41
- response = chat_model_with_tools.invoke(state["messages"])
42
  return {"messages": [response]}
43
 
 
44
  tool_node = ToolNode(tools)
45
  print("Graph nodes defined.")
46
 
47
- # 4. Define the Graph
48
  graph = StateGraph(AgentState)
49
  graph.add_node("agent", agent_node)
50
  graph.add_node("tools", tool_node)
@@ -53,7 +49,7 @@ def create_langgraph_agent():
53
  graph.add_conditional_edges("agent", tools_condition)
54
  graph.add_edge("tools", "agent")
55
 
56
- # 5. Compile the graph into a runnable app
57
  app = graph.compile()
58
  print("LangGraph agent compiled and ready.")
59
  return app
@@ -78,6 +74,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
78
  return "Please Login to Hugging Face with the button.", None
79
  username = f"{profile.username}"
80
 
 
 
 
 
 
 
 
81
  try:
82
  agent_executor = create_langgraph_agent()
83
  except Exception as e:
@@ -119,7 +122,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
119
 
120
  # Gradio Interface
121
  with gr.Blocks() as demo:
122
- gr.Markdown("# Agent Evaluation Runner (DeepSeek + LangGraph)")
123
  gr.LoginButton()
124
  run_button = gr.Button("Run Evaluation & Submit All Answers")
125
  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.tavily_search import TavilySearchResults
9
+ from langchain_openai import ChatOpenAI
10
  from langgraph.graph import StateGraph, END
11
  from langgraph.prebuilt import ToolNode, tools_condition
12
 
 
16
  class AgentState(TypedDict):
17
  messages: Annotated[Sequence[BaseMessage], operator.add]
18
 
19
+ # This function builds our final, robust agent using LangGraph and OpenAI
20
  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
+ # We bind tools to the LLM. It's a modern model that supports this natively.
28
+ tools = [TavilySearchResults(max_results=3)]
29
+ llm_with_tools = llm.bind_tools(tools)
 
 
30
  print("LLM and tools initialized.")
31
 
32
+ # 2. Define the Graph Nodes
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
+ # 3. Define the Graph
44
  graph = StateGraph(AgentState)
45
  graph.add_node("agent", agent_node)
46
  graph.add_node("tools", tool_node)
 
49
  graph.add_conditional_edges("agent", tools_condition)
50
  graph.add_edge("tools", "agent")
51
 
52
+ # 4. Compile the graph into a runnable app
53
  app = graph.compile()
54
  print("LangGraph agent compiled and ready.")
55
  return app
 
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
+
84
  try:
85
  agent_executor = create_langgraph_agent()
86
  except Exception as e:
 
122
 
123
  # Gradio Interface
124
  with gr.Blocks() as demo:
125
+ gr.Markdown("# Agent Evaluation Runner (OpenAI + LangGraph)")
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)