Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError on Arm64 M3 Mac with blablador backend #197

Closed
thawn opened this issue Aug 12, 2024 · 9 comments · Fixed by #200
Closed

UnboundLocalError on Arm64 M3 Mac with blablador backend #197

thawn opened this issue Aug 12, 2024 · 9 comments · Fixed by #200

Comments

@thawn
Copy link
Contributor

thawn commented Aug 12, 2024

Whenever I submit a prompt to blablador, I get the following error message:

UnboundLocalError: cannot access local variable 'instructions' where it is not associated with a value
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[2], line 1
----> 1 get_ipython().run_cell_magic('bob', '', 'load blobs.tif and show it\n')

File /opt/homebrew/Caskroom/mambaforge/base/envs/bob-env/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2541, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
2539 with self.builtin_trap:
2540 args = (magic_arg_s, cell)
-> 2541 result = fn(*args, **kwargs)
2543 # The code below prevents the output from being displayed
2544 # when using magics with decorator @output_can_be_silenced
2545 # when the last Python token in the expression is a ';'.
2546 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/homebrew/Caskroom/mambaforge/base/envs/bob-env/lib/python3.11/site-packages/bia_bob/_machinery.py:140, in bob(line, cell)
138 text = f"The file has been saved as {filename}. You can open it using:\n\n jupyter lab {filename}\n\n"
139 else:
--> 140 code, text = generate_response_to_user(Context.model, user_input, image)
142 if image is not None:
143 # we need to add this information to the history.
144 Context.chat.append({
145 "role": "user",
146 "content": f"Assume there is an image stored in variable {image_name}. The image can be described like this: {response}. Just confirm this with 'ok'."
147 })

File /opt/homebrew/Caskroom/mambaforge/base/envs/bob-env/lib/python3.11/site-packages/bia_bob/_utilities.py:27, in generate_response_to_user(model, user_prompt, image, additional_system_prompt, max_number_attempts, system_prompt)
25 for attempt in range(1, max_number_attempts + 1):
26 if system_prompt is None:
---> 27 system_prompt = create_system_prompt()
28 if additional_system_prompt is not None:
29 system_prompt += "\n" + additional_system_prompt

File /opt/homebrew/Caskroom/mambaforge/base/envs/bob-env/lib/python3.11/site-packages/bia_bob/_utilities.py:231, in create_system_prompt(reusable_variables_block)
228 else:
229 warnings.warn("Deprecated use of create_system_prompt with reusable_variables_block. Do not pass this parameter to make your code work mid/long-term.")
--> 231 snippets, additional_snippets = generate_code_samples()
232 libraries = Context.libraries
234 system_prompt = f"""
235 You are a extremely talented bioimage analyst and you use Python to solve your tasks unless stated otherwise.
236 If there are several ways to solve the task, chose the option with the least amount of code.
(...)
278 Make sure to keep your answer concise and to the point. Make sure the code you write is correct and can be executed.
279 """

File /opt/homebrew/Caskroom/mambaforge/base/envs/bob-env/lib/python3.11/site-packages/bia_bob/_utilities.py:211, in generate_code_samples()
209 pass
210 # special treatment for code snippets from stackview, as it won't work with the custom kernel
--> 211 if "stackview" not in instructions or "stackview" in Context.libraries:
212 additional_instructions.append(instructions)
214 additional_snippets = "\n".join(additional_instructions)

UnboundLocalError: cannot access local variable 'instructions' where it is not associated with a value

minimal notebook:

biabob.ipynb.zip

steps to reproduce:

mamba env create -f https://github.com/haesleinhuepf/bia-bob/raw/main/environment.yml
mamba activate bob-env
mamba env config vars set BLABLADOR_API_KEY=<my API key>
mamba activate bob-env #to enable the BLABLADOR_API_KEY env variable
jupyter lab
@haesleinhuepf
Copy link
Owner

haesleinhuepf commented Aug 12, 2024

Hi @thawn ,

thanks for the report. Unfortunately, I cannot reproduce this on my PC:

image

As a hardware-related issue is unlikely, I presume it has something to do with your conda environment. Could you share the output of conda list?

Thanks again!

@haesleinhuepf
Copy link
Owner

Ah, I presume this is related to #147 and the related fix. I'll do a little modification in a minute...

@haesleinhuepf
Copy link
Owner

You know what, I think git-bob can do this.

git-bob think about this!

Copy link
Contributor

This message was generated by git-bob (version: 0.2.3, model: gpt-4o-2024-08-06), an experimental AI-based assistant. It can make mistakes and has limitations. Check its messages carefully.

Hi @thawn,

The error UnboundLocalError: cannot access local variable 'instructions' where it is not associated with a value in generate_code_samples indicates that the instructions variable is being referenced before assignment in certain conditions. To resolve this, make sure the instructions variable is initialized before checking or using it.

Here's a minimal fix in the generate_code_samples function to ensure instructions is initialized:

# Initialize instructions before the try block
instructions = ""
try:
    instructions = func()
except:
    pass

This should prevent the error by ensuring instructions always has a default empty string value. Let me know if the issue persists after applying the fix.

Best,
git-bob

@haesleinhuepf
Copy link
Owner

Fantastic. git-bob implement exactly this!

@haesleinhuepf
Copy link
Owner

Hm, let's add the statement before the try: statement.

git-bob solve

@haesleinhuepf
Copy link
Owner

Ok, git-bob didn't manage, but I released bia-bob 0.21.2 Would you mind checking if it works for you @thawn ?

pip install bia-bob==0.21.2

@thawn
Copy link
Contributor Author

thawn commented Aug 12, 2024

@haesleinhuepf thanks a lot for the lightning-fast fix! I just tested it on my machine and it works perfectly.

FYI, I had to switch to the model 'alias-large' to get a useful response.

@haesleinhuepf
Copy link
Owner

Awesome, thanks for the feedback!

I must admit, I haven't used blablador for a while (happy that it still works) and get the most ouf of bia-bob using claude-3.5-sonnet these days. The new gpt4o-2024-08-06 has the longest output (16k tokens) btw in case you're generating really long code snippets ;-)

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment