mchinea commited on
Commit
f8e3605
·
1 Parent(s): fe5f523

Add file to the promt

Browse files
Files changed (2) hide show
  1. agent.py +40 -6
  2. app.py +2 -1
agent.py CHANGED
@@ -55,26 +55,53 @@ def build_agent_graph():
55
  # Compile graph
56
  return builder.compile()
57
 
 
 
 
 
 
 
 
58
 
59
 
60
  class MyGAIAAgent:
61
- def __init__(self):
62
  print("MyAgent initialized.")
63
  self.graph = build_agent_graph()
64
- def __call__(self, question: str) -> str:
65
- print(f"Agent received question (first 50 chars): {question[:50]}...")
 
 
66
  # Wrap the question in a HumanMessage from langchain_core
67
 
68
  messages = [HumanMessage(content=question)]
69
  messages = self.graph.invoke({"messages": messages})
70
  answer = messages['messages'][-1].content
71
- '''
72
  user_input = {"messages": [("user", question)]}
73
  answer1 = self.graph.invoke(user_input)["messages"][-1].content
74
  print (answer)
75
- '''
76
  #print (self._clean_answer(answer))
77
  return self._clean_answer(answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  def _clean_answer(self, answer: any) -> str:
80
  """
@@ -157,5 +184,12 @@ if __name__ == "__main__":
157
  question4 = "How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?"
158
  question6 = "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"
159
  '''
 
 
 
 
 
 
 
160
  agent = MyGAIAAgent()
161
- print(agent(question18))
 
55
  # Compile graph
56
  return builder.compile()
57
 
58
+ def print_stream(stream):
59
+ for s in stream:
60
+ message = s["messages"][-1]
61
+ if isinstance(message, tuple):
62
+ print(message)
63
+ else:
64
+ message.pretty_print()
65
 
66
 
67
  class MyGAIAAgent:
68
+ def __init__(self, verbose: bool = False):
69
  print("MyAgent initialized.")
70
  self.graph = build_agent_graph()
71
+ self.verbose = verbose
72
+ def __call__(self, task: dict) -> str:
73
+
74
+ '''
75
  # Wrap the question in a HumanMessage from langchain_core
76
 
77
  messages = [HumanMessage(content=question)]
78
  messages = self.graph.invoke({"messages": messages})
79
  answer = messages['messages'][-1].content
80
+
81
  user_input = {"messages": [("user", question)]}
82
  answer1 = self.graph.invoke(user_input)["messages"][-1].content
83
  print (answer)
 
84
  #print (self._clean_answer(answer))
85
  return self._clean_answer(answer)
86
+ '''
87
+ question = task["question"]
88
+ task_id = task["task_id"]
89
+ file_name = task.get("file_name")
90
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
91
+ file_ext = None
92
+ user_prompt = question
93
+ if file_name:
94
+ file_ext = os.path.splitext(file_name)[-1].removeprefix(".")
95
+ user_prompt += f"\nTask ID: {task_id}\nFile extension: {file_ext}"
96
+
97
+ user_input = {"messages": [("user", user_prompt)]}
98
+ if self.verbose:
99
+ print_stream(self.graph.stream(user_input, stream_mode="values"))
100
+ else:
101
+ answer = self.graph.invoke(user_input)["messages"][-1].content
102
+ return self._clean_answer(answer)
103
+
104
+
105
 
106
  def _clean_answer(self, answer: any) -> str:
107
  """
 
184
  question4 = "How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?"
185
  question6 = "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"
186
  '''
187
+ task = {
188
+ "task_id": "8e867cd7-cff9-4e6c-867a-ff5ddc2550be",
189
+ "question": "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.",
190
+ "Level": "1",
191
+ "file_name": "",
192
+ }
193
+
194
  agent = MyGAIAAgent()
195
+ print(agent(task))
app.py CHANGED
@@ -73,7 +73,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
73
  print(f"Skipping item with missing task_id or question: {item}")
74
  continue
75
  try:
76
- submitted_answer = agent(question_text)
 
77
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
78
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
79
  except Exception as e:
 
73
  print(f"Skipping item with missing task_id or question: {item}")
74
  continue
75
  try:
76
+ #submitted_answer = agent(question_text)
77
+ submitted_answer = agent(item)
78
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
79
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
80
  except Exception as e: