naman1102 commited on
Commit
6c9ca3f
Β·
1 Parent(s): 4586c7f
Files changed (3) hide show
  1. agent.py +6 -3
  2. app.py +3 -6
  3. state.py +10 -18
agent.py CHANGED
@@ -18,9 +18,12 @@ from tools import (
18
  )
19
 
20
  # ─────────────────────────── Configuration ───────────────────────────────
21
-
 
 
22
  MAX_TOOL_CALLS = 5
23
 
 
24
  # ─────────────────────────── Helper utilities ────────────────────────────
25
 
26
  # ─────────────────────────── Agent state ⬇ ───────────────────────────────
@@ -33,7 +36,7 @@ MAX_TOOL_CALLS = 5
33
  # ─────────────────────────── Graph wiring ───────────────────────────────
34
 
35
  def build_graph():
36
- graph = StateGraph(AgentState)
37
 
38
  llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.3)
39
 
@@ -45,6 +48,6 @@ def build_graph():
45
  analyze_code_tool,
46
  ]
47
  # llm = llm.bind_tools(llm_tools)
48
- agent = create_react_agent(llm, llm_tools)
49
 
50
  return agent
 
18
  )
19
 
20
  # ─────────────────────────── Configuration ───────────────────────────────
21
+ SYSTEM_PROMPT = """
22
+ You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
23
+ """
24
  MAX_TOOL_CALLS = 5
25
 
26
+
27
  # ─────────────────────────── Helper utilities ────────────────────────────
28
 
29
  # ─────────────────────────── Agent state ⬇ ───────────────────────────────
 
36
  # ─────────────────────────── Graph wiring ───────────────────────────────
37
 
38
  def build_graph():
39
+
40
 
41
  llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.3)
42
 
 
48
  analyze_code_tool,
49
  ]
50
  # llm = llm.bind_tools(llm_tools)
51
+ agent = create_react_agent(model=llm, tools=llm_tools, prompt=SYSTEM_PROMPT)
52
 
53
  return agent
app.py CHANGED
@@ -33,12 +33,9 @@ class BasicAgent:
33
 
34
  # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
35
  # not the object we pass in. Use the returned value to fetch final_answer.
36
- out_state = self.graph.invoke(init_state)
37
-
38
- if isinstance(out_state, dict): # AddableValuesDict behaves like a dict
39
- return out_state.get("final_answer", "No answer.")
40
- else: # If future versions return the dataclass
41
- return getattr(out_state, "final_answer", "No answer.")
42
 
43
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
33
 
34
  # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
35
  # not the object we pass in. Use the returned value to fetch final_answer.
36
+ out_state = self.graph.invoke({"messages": [HumanMessage(content=question)]})
37
+ print("out_state: ", out_state)
38
+ return out_state
 
 
 
39
 
40
 
41
  def run_and_submit_all( profile: gr.OAuthProfile | None):
state.py CHANGED
@@ -1,24 +1,16 @@
1
- from dataclasses import dataclass, field
2
- from typing import List, Dict, Any, Optional
3
  import json
4
- from dataclasses import dataclass, field, asdict
5
  from langchain.schema import SystemMessage, HumanMessage, AIMessage, BaseMessage
 
6
 
7
 
8
- @dataclass
9
- class AgentState:
10
  """Single source‑of‑truth context for one user query run."""
11
-
12
  user_question: str
13
- task_id: Optional[str] = None
14
- messages: List[BaseMessage] = field(default_factory=list)
15
-
16
- next_action: Optional[str] = None # wiki | ocr | audio | final
17
- query: Optional[str] = None # wiki search term
18
- snippet: Optional[str] = None # code snippet
19
- tool_calls: int = 0
20
-
21
- final_answer: Optional[str] = None
22
-
23
- def add(self, *msgs: BaseMessage):
24
- self.messages.extend(msgs)
 
1
+ from typing import List, Dict, Any, Optional, TypedDict, Annotated
 
2
  import json
 
3
  from langchain.schema import SystemMessage, HumanMessage, AIMessage, BaseMessage
4
+ from langgraph.graph.message import add_messages
5
 
6
 
7
+ class AgentState(TypedDict):
 
8
  """Single source‑of‑truth context for one user query run."""
 
9
  user_question: str
10
+ task_id: Optional[str]
11
+ messages: Annotated[List[BaseMessage], add_messages]
12
+ next_action: Optional[str] # wiki | ocr | audio | final
13
+ query: Optional[str] # wiki search term
14
+ snippet: Optional[str] # code snippet
15
+ tool_calls: int
16
+ final_answer: Optional[str]