zou-group / textgrad

TextGrad: Automatic ''Differentiation'' via Text -- using large language models to backpropagate textual gradients.
http://textgrad.com/
MIT License
1.61k stars 130 forks source link

Help Needed for IndexError: list index out of range in optimizer.py #15

Closed pipi8399 closed 3 months ago

pipi8399 commented 3 months ago

Description: I encountered an IndexError while running a script. The error traceback is as follows:

Traceback (most recent call last): File "/home/wj/textgradmain/test.py", line 186, in optimizer.step() File "/home/wj/textgradmain/textgrad/optimizer/optimizer.py", line 141, in step parameter.set_value(new_text.split(self.new_variable_tags[0])[1].split(self.new_variable_tags[1])[0].strip()) IndexError: list index out of range.

Could you help me

pipi8399 commented 3 months ago

Just need chenge requires_grad=False to requires_grad=True

dtanow commented 3 months ago

@pipi8399 I tried with both requires_grad=False and requires_grad=True, still same error:

INFO:textgrad:_backward_through_llm gradient
INFO:textgrad:_backward_through_llm prompt
INFO:textgrad:_backward_through_llm gradient
INFO:textgrad:TextualGradientDescent prompt for update
INFO:textgrad:TextualGradientDescent optimizer response
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
[<ipython-input-17-6e2eb4938a66>](https://localhost:8080/#) in <cell line: 108>()
    125 
    126     # Update code
--> 127     optimizer.step()
    128 
    129     print("\n" + "="*50 + "\n")

[/usr/local/lib/python3.10/dist-packages/textgrad/optimizer/optimizer.py](https://localhost:8080/#) in step(self)
    139             new_text = self.engine(prompt_update_parameter, system_prompt=self.optimizer_system_prompt)
    140             logger.info(f"TextualGradientDescent optimizer response", extra={"optimizer.response": new_text})
--> 141             parameter.set_value(new_text.split(self.new_variable_tags[0])[1].split(self.new_variable_tags[1])[0].strip())
    142             logger.info(f"TextualGradientDescent updated text", extra={"parameter.value": parameter.value})
    143             if self.verbose:

IndexError: list index out of range
dtanow commented 3 months ago

My assumption is this happens with longer or more complicated initial prompt. once I made it simpler and shorter, the error disappeared.

Mr-Rosan commented 2 months ago

My assumption is this happens with longer or more complicated initial prompt. once I made it simpler and shorter, the error disappeared.

So how that happened? and how to fix it? Now I really want to start with an long prompt.

mertyg commented 2 months ago

Hi @Mr-Rosan ! This can happen when the optimizer engine fails to follow the instructions and produce a new state of the variable that we can parse. In the bleeding edge version, we have this error message describing the issue.

You can try using a stronger model as the engine to the optimizer, or somehow reduce the size of the context hoping that it will help simplify things for the optimizer.