Sinaptik-AI/pandas-ai

Error when installing with docker

Closed this issue ยท 20 comments

System Info

2.2.14, macos

๐Ÿ› Describe the bug

I get following issue when installing with docker:
.06 }
27.06 Get request failed TypeError: fetch failed
27.06 at Object.fetch (node:internal/deps/undici/undici:14152:11)
27.06 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
27.06 at async i (/app/build/server/app/settings/workspaces/editspaces/page.js:1:13704)
27.06 at async p (/app/build/server/app/settings/workspaces/addspaces/page.js:1:12080) {
27.06 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
27.06 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
27.06 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
27.06 errno: -111,
27.06 code: 'ECONNREFUSED',
27.06 syscall: 'connect',
27.06 address: '127.0.0.1',
27.06 port: 8000
27.06 }
27.06 }
27.06 TypeError: fetch failed
27.06 at Object.fetch (node:internal/deps/undici/undici:14152:11)
27.06 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
27.06 at async i (/app/build/server/app/settings/workspaces/editspaces/page.js:1:13704)
27.06 at async p (/app/build/server/app/settings/workspaces/addspaces/page.js:1:12080) {
27.06 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
27.06 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
27.06 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
27.06 errno: -111,
27.06 code: 'ECONNREFUSED',
27.06 syscall: 'connect',
27.06 address: '127.0.0.1',
27.06 port: 8000
27.06 },
27.06 digest: '2837704541'
27.06 }

The error you're encountering indicates that the application is trying to connect to a service at 127.0.0.1:8000, but the connection is being refused. This typically means that the backend server is not running or not accessible at the specified address and port.

Here are a few steps to troubleshoot and resolve this issue:

  1. Ensure Backend Server is Running: Make sure that the backend server is up and running. You can check this by running:

    docker-compose up

    This command should start all the services defined in your docker-compose.yml file, including the backend server.

  2. Check Environment Variables: Verify that the environment variable NEXT_PUBLIC_API_URL is correctly set to http://localhost:8000/ in your .env files. This variable specifies the address and port for the backend service.

  3. Network Configuration: Ensure that the services are correctly connected to the same Docker network. The docker-compose.yml file should have a network configuration like this:

    networks:
      pandabi-network:
        driver: bridge

    All services should be part of this network.

  4. Logs and Errors: Check the logs for the backend server to see if there are any errors preventing it from starting. You can view the logs using:

    docker-compose logs server
  5. Port Conflicts: Ensure that port 8000 is not being used by another application on your host machine. You can check this by running:

    lsof -i :8000

By following these steps, you should be able to identify and resolve the issue preventing the connection to the backend server. If the problem persists, please provide additional details or logs for further assistance.

To continue talking to Dosu, mention @dosu.

I have NEXT_PUBLIC_API_URL='http://localhost:8000/'

still i get:
26.99 TypeError: fetch failed
26.99 at Object.fetch (node:internal/deps/undici/undici:14152:11)
26.99 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
26.99 at async n (/app/build/server/app/settings/workspaces/addspaces/page.js:1:15050)
26.99 at async u (/app/build/server/app/settings/datasets/page.js:1:8848) {
26.99 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
26.99 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
26.99 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
26.99 errno: -111,
26.99 code: 'ECONNREFUSED',
26.99 syscall: 'connect',
26.99 address: '127.0.0.1',
26.99 port: 8000
26.99 },
26.99 digest: '67092955'
26.99 }

Given that the NEXT_PUBLIC_API_URL is correctly set to http://localhost:8000/, the issue likely lies with the backend server not running or not being accessible. Here are some additional steps to troubleshoot:

  1. Verify Backend Server is Running:
    Ensure the backend server is running by executing:

    docker-compose up

    Check the logs to confirm the server has started without errors:

    docker-compose logs server
  2. Health Check Endpoint:
    Verify the backend server's health by accessing the health check endpoint. Open a browser or use curl to access:

    curl http://localhost:8000/health

    This endpoint should return a JSON response indicating the server's status [1].

  3. Check startup.sh Execution:
    Ensure the startup.sh script is executing correctly. This script is responsible for starting the server:

    #!/bin/bash
    
    # Load environment variables from .env file if it exists
    if [ -f .env ]; then
        log "Loading environment variables from .env file"
        export $(cat .env | sed 's/#.*//g' | xargs)
    else
        log ".env file not found, skipping"
    fi
    
    source $(poetry env info --path)/bin/activate
    
    poetry lock --no-update
    
    make install
    
    /bin/sh wait-for-it.sh
    
    # Run database migrations
    make migrate
    
    # Start the server in the background
    make start

    Ensure there are no errors during its execution [2].

  4. Port Conflicts:
    Ensure port 8000 is not being used by another application:

    lsof -i :8000
  5. Network Configuration:
    Confirm that all services are part of the same Docker network:

    networks:
      pandabi-network:
        driver: bridge

    All services should be connected to this network [3].

By following these steps, you should be able to identify and resolve the issue preventing the connection to the backend server. If the problem persists, please provide additional details or logs for further assistance.

To continue talking to Dosu, mention @dosu.

@dosu please name all the files in which i shall place the http://localhost. it seems so far that the setup is failing on 127.0.0.1 while i have set

6.10 }
26.10 TypeError: fetch failed
26.10 at Object.fetch (node:internal/deps/undici/undici:14152:11)
26.10 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
26.10 at async i (/app/build/server/app/settings/datasets/page.js:1:13460)
26.10 at async u (/app/build/server/app/settings/datasets/page.js:1:8848) {
26.10 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
26.10 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
26.10 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
26.10 errno: -111,
26.10 code: 'ECONNREFUSED',
26.10 syscall: 'connect',
26.10 address: '127.0.0.1',
26.10 port: 8000
26.10 },
26.10 digest: '4039937587'
26.10 }

To ensure the setup does not fail on 127.0.0.1, you should place the URL https://localhost in the following files:

  1. server/core/config.py: Update the POSTGRES_URL field.

    POSTGRES_URL: PostgresDsn = "postgresql+asyncpg://user:password@localhost:5432/db-name"
  2. client/.env: Set the NEXT_PUBLIC_API_URL environment variable.

    NEXT_PUBLIC_API_URL=https://localhost
  3. server/.env: Ensure any relevant environment variables point to https://localhost.

These changes should help resolve the connection issues by ensuring all services are correctly pointing to https://localhost instead of 127.0.0.1 [1][2][3].

To continue talking to Dosu, mention @dosu.

@dosu
I have client/.env
NEXT_PUBLIC_API_URL='http://localhost:8000/'

I have server/.env
SERVER_HOST=localhost

I have server/core/config.py i have
"postgresql+asyncpg://user:password@localhost:5432/db-name"

Still the error message is:
28.96 }
28.96 TypeError: fetch failed
28.96 at Object.fetch (node:internal/deps/undici/undici:14152:11)
28.96 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
28.96 at async i (/app/build/server/app/settings/workspaces/editspaces/page.js:1:13704)
28.96 at async p (/app/build/server/app/settings/workspaces/editspaces/page.js:1:12135) {
28.96 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
28.96 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
28.96 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
28.96 errno: -111,
28.96 code: 'ECONNREFUSED',
28.96 syscall: 'connect',
28.96 address: '127.0.0.1',
28.96 port: 8000
28.96 },
28.96 digest: '711901359'
28.96 }

Which other file i shall update?

@dosu
I have client/.env
NEXT_PUBLIC_API_URL='http://localhost:8000/'

I have server/.env
SERVER_HOST=localhost

I have server/core/config.py i have
"postgresql+asyncpg://user:password@localhost:5432/db-name"

Still the error message is:
28.96 }
28.96 TypeError: fetch failed
28.96 at Object.fetch (node:internal/deps/undici/undici:14152:11)
28.96 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
28.96 at async i (/app/build/server/app/settings/workspaces/editspaces/page.js:1:13704)
28.96 at async p (/app/build/server/app/settings/workspaces/editspaces/page.js:1:12135) {
28.96 cause: Error: connect ECONNREFUSED 127.0.0.1:8000
28.96 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
28.96 at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
28.96 errno: -111,
28.96 code: 'ECONNREFUSED',
28.96 syscall: 'connect',
28.96 address: '127.0.0.1',
28.96 port: 8000
28.96 },
28.96 digest: '711901359'
28.96 }

Which other file i shall update?

To ensure all configurations are correctly set to use http://localhost:8000, you should verify and update the following files:

  1. client/.env:

    NEXT_PUBLIC_API_URL='http://localhost:8000/'
  2. server/.env:

    SERVER_HOST=localhost
    SERVER_PORT=8000
  3. docker-compose.yml:
    Ensure the ports are correctly mapped and the environment variables are loaded:

    server:
      container_name: pandabi-backend
      build:
        context: ./server
        dockerfile: Dockerfile
      ports:
        - "8000:8000"
      restart: always
      env_file:
        - ./server/.env
      depends_on:
        - postgresql
      networks:
        - pandabi-network
      command: "/bin/bash startup.sh"
    
    client:
      container_name: pandabi-frontend
      build:
        context: ./client
        dockerfile: Dockerfile
      ports:
        - "3000:3000"
      restart: always
      env_file:
        - ./client/.env
      environment:
        - NODE_ENV=development
      command: npm run start
      networks:
        - pandabi-network
  4. server/main.py:
    Ensure the server is set to use the environment variables:

    import os
    
    if __name__ == "__main__":
        import uvicorn
        uvicorn.run(
            "app.main:app",
            host=os.environ.get("SERVER_HOST", "0.0.0.0"),
            port=int(os.environ.get("SERVER_PORT", 8000)),
            reload=True
        )
  5. server/startup.sh:
    Ensure the script loads the environment variables correctly:

    #!/bin/bash
    
    # Load environment variables from .env file if it exists
    if [ -f .env ]; then
        log "Loading environment variables from .env file"
        export $(cat .env | sed 's/#.*//g' | xargs)
    else
        log ".env file not found, skipping"
    fi
    
    source $(poetry env info --path)/bin/activate
    
    poetry lock --no-update
    
    make install
    
    /bin/sh wait-for-it.sh
    
    # Run database migrations
    make migrate
    
    # Start the server in the background
    make start

By ensuring these configurations are correctly set, you should be able to resolve the connection issue to http://localhost:8000 [1][2][3].

To continue talking to Dosu, mention @dosu.

@dosu
It looks like the backend is not starting up. I check all the files and it is correct. Shall i update the 127.0.0.1 to local host in all occurences?

Updating all occurrences of 127.0.0.1 to localhost is not necessary to ensure the backend starts up correctly. Both 127.0.0.1 and localhost refer to the local machine and should work interchangeably for local development. The configuration using 127.0.0.1 is valid and should work correctly [1].

If the backend is not starting up, you should check the following:

  1. Logs and Errors: Check the logs for any errors that might indicate why the backend is not starting. You can view the logs using:

    docker-compose logs server
  2. Health Check Endpoint: Verify the backend server's health by accessing the health check endpoint:

    curl http://localhost:8000/health

    This endpoint should return a JSON response indicating the server's status [2].

  3. Error Handling Mechanisms: Ensure that the error handling mechanisms in place are not indicating any issues. The on_auth_error and custom_exception_handler functions provide detailed error messages that can help diagnose the problem [3].

  4. Startup Script Execution: Ensure the startup.sh script is executing correctly without errors. This script is responsible for starting the server [4].

By following these steps, you should be able to identify and resolve the issue preventing the backend from starting.

To continue talking to Dosu, mention @dosu.

@dosu
docker-compose logs server is not returning anything

curl http://localhost:8000/health is not found.

The backend cannot start. script.sh runs without errors

If docker-compose logs server is not returning anything and curl http://localhost:8000/health is not found, it indicates that the backend server is not starting at all. Here are some steps to further diagnose and resolve the issue:

  1. Check Docker Compose Configuration:
    Ensure that the docker-compose.yml file is correctly configured to build and start the backend server. The relevant section should look like this:

    server:
      container_name: pandabi-backend
      build:
        context: ./server
        dockerfile: Dockerfile
      ports:
        - "8000:8000"
      restart: always
      env_file:
        - ./server/.env
      depends_on:
        - postgresql
      networks:
        - pandabi-network
      command: "/bin/bash startup.sh"

    Make sure the paths and filenames are correct [1].

  2. Verify Dockerfile:
    Ensure the server/Dockerfile is correctly set up to build the backend server. The Dockerfile should include all necessary dependencies and commands to start the server:

    FROM python:3.11-slim
    
    WORKDIR /app
    
    RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        curl \
        make \
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/*
    
    RUN curl -sSL https://install.python-poetry.org | python3 -
    
    RUN apt-get update && apt-get install netcat-openbsd -y
    
    ENV PATH="/root/.local/bin:$PATH"
    
    COPY . /app
    
    RUN poetry lock --no-update
    
    RUN poetry install --no-root
    
    COPY wait-for-it.sh /wait-for-it.sh
    RUN chmod +x /wait-for-it.sh
    
    EXPOSE 8000
    
    COPY startup.sh /startup.sh
    RUN chmod +x /startup.sh
    
    CMD ["/startup.sh"]

    Ensure there are no errors during the build process [2].

  3. Check startup.sh Execution:
    Ensure the startup.sh script is correctly starting the server. The script should look like this:

    #!/bin/bash
    
    # Load environment variables from .env file if it exists
    if [ -f .env ]; then
        log "Loading environment variables from .env file"
        export $(cat .env | sed 's/#.*//g' | xargs)
    else
        log ".env file not found, skipping"
    fi
    
    source $(poetry env info --path)/bin/activate
    
    poetry lock --no-update
    
    make install
    
    /bin/sh wait-for-it.sh
    
    # Run database migrations
    make migrate
    
    # Start the server in the background
    make start

    Ensure there are no errors during its execution [3].

  4. Check Environment Variables:
    Ensure all required environment variables are correctly set in server/.env:

    POSTGRES_URL=postgresql+asyncpg://pandasai:password123@postgresql:5432/pandasai-db
    TEST_POSTGRES_URL=postgresql+asyncpg://pandasai:password123@postgresql:5432/pandasai-db
    PANDASAI_API_KEY=your_pandasai_api_key_here
    # OPENAI_API_KEY=your_openai_api_key_here (use only if you want to use OpenAI instead of PANDASAI_API_KEY)
    
    ENVIRONMENT=development
    DEBUG=1
    SHOW_SQL_ALCHEMY_QUERIES=0
    SERVER_HOST=localhost
    SERVER_PORT=8000

    Ensure these variables are correctly loaded [4].

  5. Manually Start the Server:
    Try manually starting the server inside the Docker container to see if there are any errors:

    docker-compose run server /bin/bash
    # Inside the container
    python main.py

    This can help identify any issues that occur during the server startup.

By following these steps, you should be able to identify and resolve the issue preventing the backend server from starting.

To continue talking to Dosu, mention @dosu.

@dosu

I have following message:
raceback (most recent call last):
File "/app/main.py", line 2, in
import uvicorn
ModuleNotFoundError: No module named 'uvicorn'

The error ModuleNotFoundError: No module named 'uvicorn' indicates that the uvicorn package is not installed in your environment. Here are the steps to resolve this issue:

  1. Ensure uvicorn is listed in pyproject.toml:
    Verify that uvicorn is included as a dependency in your pyproject.toml file:

    [tool.poetry.dependencies]
    uvicorn = "^0.20.0"
  2. Install Dependencies:
    Ensure all dependencies are installed by running the following command inside the server directory:

    poetry install
  3. Rebuild Docker Image:
    Rebuild the Docker image to ensure all dependencies are included:

    docker-compose build
  4. Start Docker Containers:
    Start the Docker containers again:

    docker-compose up

By following these steps, uvicorn should be installed, and the backend server should start without the ModuleNotFoundError [1].

To continue talking to Dosu, mention @dosu.

@dosu
Now i have following error:
File "/app/main.py", line 7, in
from core.config import config
File "/app/core/config.py", line 3, in
from pydantic import BaseSettings, PostgresDsn
File "/usr/local/lib/python3.11/site-packages/pydantic/init.py", line 402, in getattr
return _getattr_migration(attr_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pydantic/_migration.py", line 296, in wrapper
raise PydanticImportError(
pydantic.errors.PydanticImportError: BaseSettings has been moved to the pydantic-settings package. See https://docs.pydantic.dev/2.9/migration/#basesettings-has-moved-to-pydantic-settings for more details.

To resolve the pydantic.errors.PydanticImportError indicating that BaseSettings has been moved to the pydantic-settings package, you need to update your import statements in the core/config.py file. Specifically, you should import BaseSettings from the pydantic_settings package instead of pydantic.

Here is the updated code:

from enum import Enum

from pydantic import PostgresDsn
from pydantic_settings import BaseSettings


class EnvironmentType(str, Enum):
    DEVELOPMENT = "development"
    PRODUCTION = "production"
    TEST = "test"


class BaseConfig(BaseSettings):
    class Config:
        case_sensitive = True


class Config(BaseConfig):
    DEBUG: int = 0
    DEFAULT_LOCALE: str = "en_US"
    ENVIRONMENT: str = EnvironmentType.DEVELOPMENT
    POSTGRES_URL: PostgresDsn = (
        "postgresql+asyncpg://user:password@localhost:5432/db-name"
    )
    OPENAI_API_KEY: str = None
    RELEASE_VERSION: str = "0.1.0"
    SHOW_SQL_ALCHEMY_QUERIES: int = 0
    SECRET_KEY: str = "super-secret-key"
    JWT_ALGORITHM: str = "HS256"
    JWT_EXPIRE_MINUTES: int = 60 * 24
    EMAIL = "test@pandabi.ai"
    PASSWORD = "12345"
    DEFAULT_ORGANIZATION = "PandaBI"
    DEFAULT_SPACE = "pandasai"


config: Config = Config()

Additionally, make sure to install the pydantic-settings package:

pip install pydantic-settings

This change should resolve the import error [1].

To continue talking to Dosu, mention @dosu.

@dosu
For code
if name == "main":
uvicorn.run(
app="core.server:app",
reload=True if config.ENVIRONMENT != "production" else False,
host=os.environ.get("SERVER_HOST", "0.0.0.0"),
port=os.environ.get("SERVER_PORT", 8000),
workers=1,
)

I have error ERROR: Error loading ASGI app. Could not import module "app.main".

what is the correct code to start it?

The correct code to start the ASGI app and avoid the error "ERROR: Error loading ASGI app. Could not import module 'app.main'" is:

import os
import uvicorn
from dotenv import load_dotenv

load_dotenv()

from core.config import config

if __name__ == "__main__":
    uvicorn.run(
        app="core.server:app",
        reload=True if config.ENVIRONMENT != "production" else False,
        host=os.environ.get("SERVER_HOST", "0.0.0.0"),
        port=os.environ.get("SERVER_PORT", 8000),
        workers=1,
    )

This code specifies the correct module path for the ASGI app as core.server:app, which matches the structure defined in server/core/server.py [1][2].

To continue talking to Dosu, mention @dosu.

@dosu
the code is following:
if name == "main":
uvicorn.run(
app="core.server:app",
reload=True if config.ENVIRONMENT != "production" else False,
host=os.environ.get("SERVER_HOST", "0.0.0.0"),
port=os.environ.get("SERVER_PORT", 8000),
workers=1,
)

Problem persists:
WARNING: StatReload detected changes in 'main.py'. Reloading...
ERROR: Error loading ASGI app. Could not import module "app.main".

The error indicates that the module path specified for the ASGI app is incorrect. The correct module path should be core.server:app, not app.main.

Here is the corrected code for main.py:

import os
import uvicorn
from dotenv import load_dotenv

load_dotenv()

from core.config import config

if __name__ == "__main__":
    uvicorn.run(
        app="core.server:app",
        reload=True if config.ENVIRONMENT != "production" else False,
        host=os.environ.get("SERVER_HOST", "0.0.0.0"),
        port=int(os.environ.get("SERVER_PORT", 8000)),
        workers=1,
    )

Ensure that the core.server:app path is correct and matches the structure defined in server/core/server.py where the app object is created [1][2].

To continue talking to Dosu, mention @dosu.