Naming the iterable in the guidance string `items` breaks guidance in 0.64
Naming the iterable in the guidance string items breaks guidance.
E.g,: using {{~#each items}} -> results in an error.
This is probably due to some naming collision upstream related to a dictionary. The stacktrace is:
Traceback (most recent call last):
File "C:\Miniconda3\envs\py10\lib\site-packages\guidance\library\_each.py", line 40, in each
iter(list)
TypeError: 'builtin_function_or_method' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Miniconda3\envs\py10\lib\site-packages\guidance\_program_executor.py", line 109, in run
await self.visit(self.parse_tree, VariableStack([self.program._variables], self))
File "C:\Miniconda3\envs\py10\lib\site-packages\guidance\_program_executor.py", line 559, in visit
visited_children.append(await self.visit(child, variable_stack, inner_next_node, inner_next_next_node, inner_prev_node, node, parent_node))
File "C:\Miniconda3\envs\py10\lib\site-packages\guidance\_program_executor.py", line 524, in visit
command_output = await command_function(*positional_args, **named_args)
File "C:\Miniconda3\envs\py10\lib\site-packages\guidance\library\_each.py", line 42, in each
raise TypeError("The #each command cannot iterate over a non-iterable value: " + str(list))
TypeError: The #each command cannot iterate over a non-iterable value: <built-in method items of dict object at 0x00000264C84C9B40>
I can't easily fix it without grokking handlebars/the parse tree, but it's easy to reproduce.
Note that this was new from 0.62 -> 0.64 and was a pain to debug.
To Reproduce
Just rename examples -> items in the example.
import guidance
# set the default language model used to execute guidance programs
guidance.llm = guidance.llms.OpenAI("text-davinci-003")
# define the few shot examples
examples = [
{'input': 'I wrote about shakespeare',
'entities': [{'entity': 'I', 'time': 'present'}, {'entity': 'Shakespeare', 'time': '16th century'}],
'reasoning': 'I can write about Shakespeare because he lived in the past with respect to me.',
'answer': 'No'},
{'input': 'Shakespeare wrote about me',
'entities': [{'entity': 'Shakespeare', 'time': '16th century'}, {'entity': 'I', 'time': 'present'}],
'reasoning': 'Shakespeare cannot have written about me, because he died before I was born',
'answer': 'Yes'}
]
# define the guidance program
structure_program = guidance(
'''Given a sentence tell me whether it contains an anachronism (i.e. whether it could have happened or not based on the time periods associated with the entities).
----
{{~! display the few-shot examples ~}}
{{~#each items}}
Sentence: {{this.input}}
Entities and dates:{{#each this.entities}}
{{this.entity}}: {{this.time}}{{/each}}
Reasoning: {{this.reasoning}}
Anachronism: {{this.answer}}
---
{{~/each}}
{{~! place the real question at the end }}
Sentence: {{input}}
Entities and dates:
{{gen "entities"}}
Reasoning:{{gen "reasoning"}}
Anachronism:{{#select "answer"}} Yes{{or}} No{{/select}}''')
# execute the program
out = structure_program(
items=examples,
input='The T-rex bit my dog'
)
Python 3.10.11 Windows 10/11
Hello,
just noticed this myself too. Took me a while to figure it out ^^ This also happens with "geneach" block.
Code to reproduce
guidance.llm = guidance.llms.OpenAI("text-davinci-003")
prompt = guidance("""
Shopping List:
{{#geneach 'items'}}
{{gen 'this' stop='\n'}}
{{~/geneach}}}}
""")
res = prompt()
print(res)
# print(res["items"])
Traceback
Traceback (most recent call last):
File "/home/simon/workspaces/master/llm-thought-framework/venv/lib/python3.10/site-packages/guidance/_program_executor.py", line 109, in run
await self.visit(self.parse_tree, VariableStack([self.program._variables], self))
File "/home/simon/workspaces/master/llm-thought-framework/venv/lib/python3.10/site-packages/guidance/_program_executor.py", line 559, in visit
visited_children.append(await self.visit(child, variable_stack, inner_next_node, inner_next_next_node, inner_prev_node, node, parent_node))
File "/home/simon/workspaces/master/llm-thought-framework/venv/lib/python3.10/site-packages/guidance/_program_executor.py", line 524, in visit
command_output = await command_function(*positional_args, **named_args)
File "/home/simon/workspaces/master/llm-thought-framework/venv/lib/python3.10/site-packages/guidance/library/_geneach.py", line 129, in geneach
variable_stack[list_name] = variable_stack.get(list_name, []) + [block_variables]
TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'list'
So this bug also spills over to pretty much anything in the parser. I've been trying to add/fix tool calling with array parameters as some APIs require those and guidance can't do arrays. As I went to fix it, it seems to be converting the {{> partial tooldef to a ```typescript definition, then back to a json schema, then extracting it, or some jumble of those 4 steps that I as a junior dev can't understand why. long story, but I'd already fixed one conversion, only to find out that the second conversion doesn't work because it won't read "items" at all.
Changing my version to 0.0.57 fixed this issue for me.
Minor update: Guidance was a great project and really clean.
(Unfortunately, I haven't been able to do much coding recently for good reasons) but from what I can tell, there haven't been many updates or clear direction from the owners.
Because of this and due to the importance of the new OpenAI function calling, which doesn't seem supported well in Guidance, I ended up going back to just plain Python or my own abstractions to orchestrate calls.
I still use old versions of Guidance though.
Please checkout our new release, where we did away with handlebars and everything looks more like pure python :)