mrhenu commited on
Commit
0bae48f
·
verified ·
1 Parent(s): ffa2354

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -4,12 +4,13 @@ import requests
4
  import pandas as pd
5
  from typing import TypedDict, Annotated, Sequence
6
  import operator
7
- from langchain_core.messages import BaseMessage, HumanMessage, ToolMessage
8
  from langchain.agents import AgentExecutor
9
- from langchain_community.tools.tavily_search import TavilySearchResults
10
  from langchain_huggingface import HuggingFaceEndpoint
11
  from langgraph.graph import StateGraph, END
12
  from langgraph.prebuilt import ToolNode, tools_condition
 
13
 
14
  # --- Main Application Logic ---
15
 
@@ -21,28 +22,27 @@ class AgentState(TypedDict):
21
  def create_langgraph_agent():
22
  print("Initializing LangGraph Agent...")
23
 
24
- # 1. Set up the LLM (The "Brain")
25
- # We use the powerful Mistral model with the correct 'conversational' task
26
  llm = HuggingFaceEndpoint(
27
- repo_id="mistralai/Mistral-7B-Instruct-v0.2",
28
  task="conversational",
29
- max_new_tokens=512,
30
  do_sample=False,
31
  )
32
- # We bind the tools to the LLM so it knows about them
33
- tools = [TavilySearchResults(max_results=3)]
34
- llm_with_tools = llm.bind_tools(tools)
 
35
  print("LLM and tools initialized.")
36
 
37
- # 2. Define the Graph Nodes
38
- # The 'agent' node calls the LLM
39
  def agent_node(state):
40
  print("Calling agent node...")
41
  response = llm_with_tools.invoke(state["messages"])
42
  return {"messages": [response]}
43
 
44
- # The 'tool' node executes the tools
45
- tool_node = ToolNode(tools)
46
  print("Graph nodes defined.")
47
 
48
  # 3. Define the Graph
@@ -51,12 +51,7 @@ def create_langgraph_agent():
51
  graph.add_node("tools", tool_node)
52
 
53
  graph.set_entry_point("agent")
54
-
55
- # This conditional edge decides whether to call a tool or end
56
- graph.add_conditional_edges(
57
- "agent",
58
- tools_condition,
59
- )
60
  graph.add_edge("tools", "agent")
61
 
62
  # 4. Compile the graph into a runnable app
@@ -125,7 +120,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
125
 
126
  # Gradio Interface
127
  with gr.Blocks() as demo:
128
- gr.Markdown("# Agent Evaluation Runner (Final LangGraph v2)")
129
  gr.LoginButton()
130
  run_button = gr.Button("Run Evaluation & Submit All Answers")
131
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
4
  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.agents import AgentExecutor
9
+ from langchain_community.tools import DuckDuckGoSearchRun
10
  from langchain_huggingface import HuggingFaceEndpoint
11
  from langgraph.graph import StateGraph, END
12
  from langgraph.prebuilt import ToolNode, tools_condition
13
+ from langchain_core.prompts import ChatPromptTemplate
14
 
15
  # --- Main Application Logic ---
16
 
 
22
  def create_langgraph_agent():
23
  print("Initializing LangGraph Agent...")
24
 
25
+ # 1. Set up the LLM (The "Brain") using the DeepSeek Coder model
 
26
  llm = HuggingFaceEndpoint(
27
+ repo_id="deepseek-ai/deepseek-coder-6.7b-instruct",
28
  task="conversational",
29
+ max_new_tokens=1024, # Increased tokens for better reasoning
30
  do_sample=False,
31
  )
32
+
33
+ # We use a wrapper to make the model compatible with LangChain's tool calling
34
+ from langchain_huggingface.chat_models import HuggingFaceChat
35
+ llm_with_tools = HuggingFaceChat(endpoint=llm).bind_tools([DuckDuckGoSearchRun()])
36
  print("LLM and tools initialized.")
37
 
38
+ # 2. Define the agent's logic (the "agent" node)
39
+ # We define the graph nodes and edges for the agent's reasoning process
40
  def agent_node(state):
41
  print("Calling agent node...")
42
  response = llm_with_tools.invoke(state["messages"])
43
  return {"messages": [response]}
44
 
45
+ tool_node = ToolNode([DuckDuckGoSearchRun()])
 
46
  print("Graph nodes defined.")
47
 
48
  # 3. Define the Graph
 
51
  graph.add_node("tools", tool_node)
52
 
53
  graph.set_entry_point("agent")
54
+ graph.add_conditional_edges("agent", tools_condition)
 
 
 
 
 
55
  graph.add_edge("tools", "agent")
56
 
57
  # 4. Compile the graph into a runnable app
 
120
 
121
  # Gradio Interface
122
  with gr.Blocks() as demo:
123
+ gr.Markdown("# Agent Evaluation Runner (DeepSeek + DuckDuckGo)")
124
  gr.LoginButton()
125
  run_button = gr.Button("Run Evaluation & Submit All Answers")
126
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)