Sinaptik-AI / pandas-ai

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.

Home Page:https://pandas-ai.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ollama fails: cannot access local variable 'code_to_run' where it is not associated with a value

lucasjohnston opened this issue Β· comments

System Info

macOS 14.4.1
python 3.11.7
pandasai 2.0.35

πŸ› Describe the bug

I'm unable to run pandasai with Ollama (llama3) locally, as I run into the following error per logs:

2024-04-22 21:33:25 [INFO] Question: Which article contained the most women?
2024-04-22 21:33:25 [INFO] Running PandasAI with local LLM...
2024-04-22 21:33:25 [INFO] Prompt ID: 0dcf1541-5e1b-4ae0-bb27-9b3dc9978932
2024-04-22 21:33:25 [INFO] Executing Pipeline: GenerateChatPipeline
2024-04-22 21:33:25 [INFO] Executing Step 0: ValidatePipelineInput
2024-04-22 21:33:25 [INFO] Executing Step 1: CacheLookup
2024-04-22 21:33:25 [INFO] Using cached response
2024-04-22 21:33:25 [INFO] Executing Step 2: PromptGeneration
2024-04-22 21:33:25 [INFO] Executing Step 2: Skipping...
2024-04-22 21:33:25 [INFO] Executing Step 3: CodeGenerator
2024-04-22 21:33:25 [INFO] Executing Step 3: Skipping...
2024-04-22 21:33:25 [INFO] Executing Step 4: CachePopulation
2024-04-22 21:33:25 [INFO] Executing Step 4: Skipping...
2024-04-22 21:33:25 [INFO] Executing Step 5: CodeCleaning
2024-04-22 21:33:25 [ERROR] Pipeline failed on step 5: cannot access local variable 'code_to_run' where it is not associated with a value

Code excerpt:

import os
from sshtunnel import SSHTunnelForwarder
from pandasai.connectors import PostgreSQLConnector
from pandasai import SmartDataframe
from pandasai.llm.local_llm import LocalLLM

...

with SSHTunnelForwarder(
    (ssh_host, ssh_port),
    ssh_username=ssh_username,
    ssh_private_key=ssh_private_key,
    remote_bind_address=(db_host, db_port),
    local_bind_address=('localhost', local_bind_port)
) as tunnel:
    print(f"SSH tunnel established on local port {local_bind_port}")

    ollama_llm = LocalLLM(api_base="http://localhost:11434/v1", model="llama3")

    article_df = SmartDataframe(PostgreSQLConnector(config={
        "host": "localhost",
        "port": local_bind_port,
        "database": db_name,
        "username": db_user,
        "password": db_password,
        "table": 'article'
    }), config={"llm": ollama_llm})

    print(article_df.chat("Which article contained the most women?"))

Runtime logs:

➜  python app.py
SSH tunnel established on local port 6543
Traceback (most recent call last):
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/chat/code_cleaning.py", line 93, in execute
    code_to_run = self.get_code_to_run(input, code_context)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/chat/code_cleaning.py", line 146, in get_code_to_run
    code_to_run = self._clean_code(code, context)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/chat/code_cleaning.py", line 445, in _clean_code
    tree = ast.parse(code)
           ^^^^^^^^^^^^^^^
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 11
    f: COMPLOAD, COMPASS:
               ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/chat/generate_chat_pipeline.py", line 307, in run
    output = (self.code_generation_pipeline | self.code_execution_pipeline).run(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/pipeline.py", line 137, in run
    raise e
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/pipeline.py", line 101, in run
    step_output = logic.execute(
                  ^^^^^^^^^^^^^^
  File "/Users/lucas/.pyenv/versions/3.11.7/lib/python3.11/site-packages/pandasai/pipelines/chat/code_cleaning.py", line 97, in execute
    self.on_failure(code_to_run, traceback_errors)
                    ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'code_to_run' where it is not associated with a value
Unfortunately, I was not able to answer your question, because of the following error:

cannot access local variable 'code_to_run' where it is not associated with a value

Any update on how to work around the above? Thanks!