luke9705 commited on
Commit
856ab04
·
1 Parent(s): 0601340

Enhance check_format function to disallow nested lists (let LLM self-correct) and clarify return types in documentation.

Browse files
Files changed (2) hide show
  1. app.py +18 -17
  2. system_prompt.txt +4 -3
app.py CHANGED
@@ -47,28 +47,21 @@ def load_file(path: str) -> list | dict:
47
  else:
48
  return {"raw document text": text, "file path": path}
49
 
50
- def check_format(answer: str | list, *args, **kwargs ) -> list:
51
- """ Check if the answer is a list. """
52
-
53
  print("Checking format of the answer:", answer)
54
- def flatten(lst):
55
- for item in lst:
56
  if isinstance(item, list):
57
- yield from flatten(item)
58
- else:
59
- yield item
60
- try:
61
- if isinstance(answer, list):
62
- flat = list(flatten(answer))
63
- return flat
64
- except Exception as e:
65
- if isinstance(answer, dict):
66
- raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format. Error: {e}")
67
 
68
 
69
 
70
-
71
-
72
  ## tools definition
73
  @tool
74
  def download_images(image_urls: str) -> list:
@@ -151,6 +144,14 @@ def generate_image(prompt: str, neg_prompt: str) -> Image.Image:
151
 
152
  return gr.Image(value=image, label="Generated Image")
153
 
 
 
 
 
 
 
 
 
154
 
155
  ## agent definition
156
  class Agent:
 
47
  else:
48
  return {"raw document text": text, "file path": path}
49
 
50
+ def check_format(answer: str | list, *args, **kwargs) -> list:
51
+ """Check if the answer is a list and not a nested list."""
 
52
  print("Checking format of the answer:", answer)
53
+ if isinstance(answer, list):
54
+ for item in answer:
55
  if isinstance(item, list):
56
+ print("list detected")
57
+ raise TypeError("Nested lists are not allowed in the final answer.")
58
+ elif isinstance(answer, str):
59
+ return [answer]
60
+ elif isinstance(answer, dict):
61
+ raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format. Error: {e}")
 
 
 
 
62
 
63
 
64
 
 
 
65
  ## tools definition
66
  @tool
67
  def download_images(image_urls: str) -> list:
 
144
 
145
  return gr.Image(value=image, label="Generated Image")
146
 
147
+ """@tool
148
+ def generate_audio(prompt: str) -> object:
149
+ space = smolagents.load_tool(
150
+
151
+ )"""
152
+
153
+
154
+
155
 
156
  ## agent definition
157
  class Agent:
system_prompt.txt CHANGED
@@ -320,10 +320,11 @@ It is MANDATORY to use these rules for the 'final_answer' tool:
320
  3. Any text or explanation must come after the component, as string elements in the same list.
321
  4. If there is no component to return, return a list whose only element is the text.
322
  5. Examples of valid returns:
323
- • [gr.Image(value=“output.png”), “Here is the chart.”]
324
- • [gr.File(value=“report.pdf”), “Download the report.”]
325
  7. Any deviation (returning a dict, tuple, raw PIL image, etc.) is invalid.
326
- 8. Always put generated images in the list, do NOT create external link.
 
327
 
328
  Now Begin!
329
 
 
320
  3. Any text or explanation must come after the component, as string elements in the same list.
321
  4. If there is no component to return, return a list whose only element is the text.
322
  5. Examples of valid returns:
323
+ • [gr.Image(value=“output.png”), “Here is the chart.”]
324
+ • [gr.File(value=“report.pdf”), “Download the report.”]
325
  7. Any deviation (returning a dict, tuple, raw PIL image, etc.) is invalid.
326
+ 8. Nested list are forbidden.
327
+ 9. Always put generated images in the list, do NOT create external link.
328
 
329
  Now Begin!
330