RuaZhou commited on
Commit
e30d61e
·
verified ·
1 Parent(s): 4c4b402

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -94,11 +94,19 @@ Substract(a: int|float, b: int|float) -> float:
94
 
95
  """
96
  previous_message = state["messages"]
97
- sys_msg = SystemMessage(content=f"You are an helpful agent that can analyse some images and run some computatio without provided tools :\n{textual_description_of_tool} \n You have access to some otpional images. Currently the loaded images is : {image}")
 
 
 
 
 
98
 
99
  return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])], "input_file": state["input_file"]}
100
 
101
  ################ state
 
 
 
102
  class BasicAgent:
103
  def __init__(self):
104
  print("BasicAgent initialized.")
@@ -108,12 +116,29 @@ class BasicAgent:
108
  # self.visionLLM = ChatOpenAI(model="gpt-4o",api_key=api_key) # multi-modal LLM
109
  self.LLM = ChatOpenAI(model="gpt-4o",api_key=api_key) # manager LLM
110
  self.LLM_with_tools = self.LLM.bind_tools(tools, parallel_tool_calls=False)
111
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  def __call__(self, question: str) -> str:
113
  print(f"Agent received question (first 50 chars): {question[:50]}...")
114
- fixed_answer = "This is a default answer."
115
- print(f"Agent returning fixed answer: {fixed_answer}")
116
- return fixed_answer
 
 
 
117
 
118
  def run_and_submit_all( profile: gr.OAuthProfile | None):
119
  """
 
94
 
95
  """
96
  previous_message = state["messages"]
97
+ sys_msg = SystemMessage(content=f"""You are an helpful agent, that can analyse the given quetion and run some computation with provided tools
98
+ :\n{textual_description_of_tool} \n I will ask you a question. Report your thoughts, and finish your answer with the following template: [YOUR FINAL ANSWER].
99
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
100
+ 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.
101
+ 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.
102
+ 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.""")
103
 
104
  return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])], "input_file": state["input_file"]}
105
 
106
  ################ state
107
+ from langgraph.graph import START, StateGraph
108
+ from langgraph.prebulit import ToolNode, tools_conditions
109
+
110
  class BasicAgent:
111
  def __init__(self):
112
  print("BasicAgent initialized.")
 
116
  # self.visionLLM = ChatOpenAI(model="gpt-4o",api_key=api_key) # multi-modal LLM
117
  self.LLM = ChatOpenAI(model="gpt-4o",api_key=api_key) # manager LLM
118
  self.LLM_with_tools = self.LLM.bind_tools(tools, parallel_tool_calls=False)
119
+ # Graph
120
+ self.Builder = StateGraph(AgentState)
121
+ # Define nodes: these do the work
122
+ self.Builder.add_node("assistant", assistant)
123
+ self.Builder.add_node("tools",ToolNode(tools))
124
+ # Define edges: these determine how the control flow moves
125
+ self.Builder.add_edge(START,"assistant")
126
+ # if state says "tools":
127
+ # go to tools node
128
+ # else:
129
+ # go to END node
130
+ self.Builder.add_conditional_edges("assistant",tools_conditions)
131
+ self.Builder.add_edge("tools", "assistant")
132
+
133
+ self.agent = self.Builder.compile()
134
  def __call__(self, question: str) -> str:
135
  print(f"Agent received question (first 50 chars): {question[:50]}...")
136
+ messages = [HumanMessage(content=f"{question}")]
137
+
138
+ messages = react_graph.invoke({"messages": messages})
139
+ answer = f"{messages[-1].content}"
140
+ print(f"Agent returning answer: {answer}")
141
+ return answer
142
 
143
  def run_and_submit_all( profile: gr.OAuthProfile | None):
144
  """