sid385 commited on
Commit
2ceea4d
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -5
app.py CHANGED
@@ -4,20 +4,61 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
 
 
 
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
 
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
  class BasicAgent:
14
  def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
4
  import inspect
5
  import pandas as pd
6
 
7
+
8
+ from langgraph.graph import StateGraph, END
9
+ from langchain_core.runnables import Runnable
10
+ from langchain_core.agents import AgentFinish
11
+ from langchain.agents import create_react_agent
12
+ from langchain.agents.agent import RunnableAgent
13
+ from langchain.agents.tools import Tool
14
+ from langchain_core.messages import HumanMessage
15
+ from langchain_openai import ChatOpenAI
16
+
17
+
18
  # (Keep Constants as is)
19
  # --- Constants ---
20
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
 
22
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
23
+
24
+
25
  # --- Basic Agent Definition ---
26
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
27
  class BasicAgent:
28
  def __init__(self):
29
+ print("LangGraph ReAct Agent initialized.")
30
+ if not OPENAI_API_KEY:
31
+ raise ValueError("OPENAI_API_KEY environment variable not found.")
32
+
33
+ self.llm = ChatOpenAI(model="gpt-4o", api_key=OPENAI_API_KEY)
34
+
35
+ # Example tools
36
+ self.tools = [
37
+ Tool(
38
+ name="DuckDuckGo Search",
39
+ func=lambda q: f"Pretend search result for: {q}",
40
+ description="Search the web using DuckDuckGo. Useful for recent events."
41
+ )
42
+ ]
43
+
44
+ react_agent = create_react_agent(self.llm, self.tools)
45
+
46
+ workflow = StateGraph({"agent_outcome": Runnable})
47
+ workflow.add_node("agent", react_agent)
48
+ workflow.set_entry_point("agent")
49
+ workflow.add_edge("agent", END)
50
+
51
+ app = workflow.compile()
52
+ self.agent = RunnableAgent(app)
53
+
54
  def __call__(self, question: str) -> str:
55
+ result = self.agent.invoke({"input": HumanMessage(content=question)})
56
+ if isinstance(result, AgentFinish):
57
+ return result.return_values["output"]
58
+ elif isinstance(result, dict) and "output" in result:
59
+ return result["output"]
60
+ else:
61
+ return "No valid output returned."
62
 
63
  def run_and_submit_all( profile: gr.OAuthProfile | None):
64
  """