EtienneB commited on
Commit
9945183
·
1 Parent(s): 1e05108

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +25 -7
agent.py CHANGED
@@ -13,11 +13,13 @@ from tools import (absolute, add, analyze_csv_file, analyze_excel_file,
13
  arvix_search, audio_transcription, compound_interest,
14
  convert_temperature, divide, exponential,
15
  extract_text_from_image, factorial, floor_divide,
16
- get_current_time_in_timezone, greatest_common_divisor,
17
- is_prime, least_common_multiple, logarithm, modulus,
18
- multiply, percentage_calculator, power, python_code_parser,
19
- reverse_sentence, roman_calculator_converter, square_root,
20
- subtract, web_content_extract, web_search, wiki_search)
 
 
21
 
22
  # Load Constants
23
  load_dotenv()
@@ -33,7 +35,7 @@ tools = [
33
  is_prime, least_common_multiple, percentage_calculator,
34
  wiki_search, analyze_excel_file, arvix_search,
35
  audio_transcription, python_code_parser, analyze_csv_file,
36
- extract_text_from_image, reverse_sentence, web_content_extract
37
  ]
38
 
39
  # Load system prompt
@@ -45,7 +47,7 @@ If you are asked for a number, don't use a comma to write your number, nor use u
45
  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.
46
  If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
47
  Format your output as: Answers (answers): [{"task_id": ..., "submitted_answer": ...}]
48
- Do not repeat the format or include any nested JSON. Output only one flat list as: Answers (answers): [{...}]
49
  """
50
 
51
  # System message
@@ -82,6 +84,7 @@ def build_graph():
82
  # Get task_id from state or set a placeholder
83
  task_id = state.get("task_id", "1") # Replace with actual logic if needed
84
  formatted = f'Answers (answers): [{{"task_id": "{task_id}", "submitted_answer": "{answer_text}"}}]'
 
85
  return {"messages": [formatted]}
86
 
87
  # --- Graph Definition ---
@@ -121,6 +124,21 @@ def is_valid_agent_output(output):
121
  except Exception:
122
  return False
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # test
125
  if __name__ == "__main__":
126
  question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
 
13
  arvix_search, audio_transcription, compound_interest,
14
  convert_temperature, divide, exponential,
15
  extract_text_from_image, factorial, floor_divide,
16
+ get_current_time_in_timezone,
17
+
18
+ greatest_common_divisor, is_prime, least_common_multiple,
19
+ logarithm, modulus, multiply, percentage_calculator, power,
20
+ python_code_parser, reverse_sentence,
21
+ roman_calculator_converter, square_root, subtract,
22
+ web_content_extract, web_search, wiki_search)
23
 
24
  # Load Constants
25
  load_dotenv()
 
35
  is_prime, least_common_multiple, percentage_calculator,
36
  wiki_search, analyze_excel_file, arvix_search,
37
  audio_transcription, python_code_parser, analyze_csv_file,
38
+ extract_text_from_image, reverse_sentence, web_content_extract,
39
  ]
40
 
41
  # Load system prompt
 
47
  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.
48
  If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
49
  Format your output as: Answers (answers): [{"task_id": ..., "submitted_answer": ...}]
50
+ Do NOT include the format string or any JSON inside the submitted_answer field. Only output a single flat list as: Answers (answers): [{"task_id": ..., "submitted_answer": ...}]
51
  """
52
 
53
  # System message
 
84
  # Get task_id from state or set a placeholder
85
  task_id = state.get("task_id", "1") # Replace with actual logic if needed
86
  formatted = f'Answers (answers): [{{"task_id": "{task_id}", "submitted_answer": "{answer_text}"}}]'
87
+ formatted = extract_flat_answer(formatted)
88
  return {"messages": [formatted]}
89
 
90
  # --- Graph Definition ---
 
124
  except Exception:
125
  return False
126
 
127
+
128
+ def extract_flat_answer(output):
129
+ # Try to find the innermost Answers (answers): [{...}]
130
+ pattern = r'Answers \(answers\): \[(\{.*?\})\]'
131
+ matches = re.findall(pattern, output)
132
+ if matches:
133
+ # Use the last match (innermost)
134
+ try:
135
+ answers_list = json.loads(f'[{matches[-1]}]')
136
+ if isinstance(answers_list, list) and "task_id" in answers_list[0] and "submitted_answer" in answers_list[0]:
137
+ return f'Answers (answers): [{matches[-1]}]'
138
+ except Exception:
139
+ pass
140
+ return output # fallback
141
+
142
  # test
143
  if __name__ == "__main__":
144
  question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"