run-llama / sec-insights

A real world full-stack application using LlamaIndex
https://www.secinsights.ai/
MIT License
2.32k stars 631 forks source link

codespaces backend issues #45

Open mattarderne opened 11 months ago

mattarderne commented 11 months ago

I followed the instructions on the latest PR merge, but still bumping into a few issues with codespaces. The first make run worked, but then on following further instructions, it starts throwing issues. The only change I have made is the the .env adding OPENAI_API_KEY

make seed_db_local

 ✔ Container localstack_main  Running                                                                                                                  0.0s 
docker compose start localstack
echo "Waiting for localstack to start..."
Waiting for localstack to start...
sleep 3
# Check that S3_ASSET_BUCKET_NAME is set
if [ -z llama-app-web-assets-local ]; then \
        echo "S3_ASSET_BUCKET_NAME is not set. Please set it and try again."; \
        exit 1; \
fi
awslocal s3 mb s3://llama-app-web-assets-local
make[1]: awslocal: No such file or directory
make[1]: *** [Makefile:44: setup_localstack] Error 127
make[1]: Leaving directory '/workspaces/sec-insights/backend'
make: *** [Makefile:81: seed_db_local] Error 2

make refresh_db

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/__main__.py", line 4, in <module>
    main(prog="alembic")
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/config.py", line 630, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/config.py", line 624, in main
    self.run_cmd(cfg, options)
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/config.py", line 601, in run_cmd
    fn(
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/command.py", line 385, in upgrade
    script.run_env()
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/script/base.py", line 578, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 109, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/workspaces/sec-insights/backend/alembic/env.py", line 98, in <module>
    run_migrations_online()
  File "/workspaces/sec-insights/backend/alembic/env.py", line 92, in run_migrations_online
    asyncio.run(run_async_migrations())
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/workspaces/sec-insights/backend/alembic/env.py", line 83, in run_async_migrations
    async with connectable.connect() as connection:
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/base.py", line 127, in __aenter__
    return await self.start(is_ctxmanager=True)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/engine.py", line 268, in start
    await greenlet_spawn(self.sync_engine.connect)
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 190, in greenlet_spawn
    result = context.throw(*sys.exc_info())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 3264, in connect
    return self._connection_cls(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 3288, in raw_connection
    return self.pool.connect()
           ^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 452, in connect
    return _ConnectionFairy._checkout(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 1267, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 716, in checkout
    rec = pool._do_get()
          ^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/impl.py", line 284, in _do_get
    return self._create_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 393, in _create_connection
    return _ConnectionRecord(self)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 678, in __init__
    self.__connect()
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 902, in __connect
    with util.safe_reraise():
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/pool/base.py", line 898, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 637, in connect
    return dialect.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 615, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 917, in connect
    await_only(creator_fn(*arg, **kw)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 125, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 185, in greenlet_spawn
    value = await result
            ^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connection.py", line 2092, in connect
    return await connect_utils._connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 895, in _connect
    raise last_error
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 881, in _connect
    return await _connect_addr(
           ^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 773, in _connect_addr
    return await __connect_addr(params, timeout, True, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 825, in __connect_addr
    tr, pr = await compat.wait_for(connector, timeout=timeout)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/compat.py", line 56, in wait_for
    return await asyncio.wait_for(fut, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 479, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/asyncpg/connect_utils.py", line 692, in _create_ssl_connection
    do_ssl_upgrade = await pr.on_data
                     ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 995, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionResetError: [Errno 104] Connection reset by peer
make[2]: *** [Makefile:15: migrate] Error 1
make[2]: Leaving directory '/workspaces/sec-insights/backend'
make[1]: *** [Makefile:29: confirmed_refresh_db] Error 2
make[1]: Leaving directory '/workspaces/sec-insights/backend'
make: *** [Makefile:21: refresh_db] Error 2

Then going back to try make run again

2023-09-30 08:58:36,454 [MainThread  ] [DEBUG]  Skipping migrations
INFO:     Will watch for changes in these directories: ['/workspaces/sec-insights/backend']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [36511] using StatReload
INFO:     Started server process [37826]
INFO:     Waiting for application startup.
Connected to the database on attempt 1.
2023-09-30 08:58:39,096 INFO sqlalchemy.engine.Engine select pg_catalog.version()
2023-09-30 08:58:39,097 INFO sqlalchemy.engine.Engine [raw sql] {}
2023-09-30 08:58:39,097 INFO sqlalchemy.engine.Engine select current_schema()
2023-09-30 08:58:39,097 INFO sqlalchemy.engine.Engine [raw sql] {}
2023-09-30 08:58:39,098 INFO sqlalchemy.engine.Engine show standard_conforming_strings
2023-09-30 08:58:39,098 INFO sqlalchemy.engine.Engine [raw sql] {}
2023-09-30 08:58:39,098 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2023-09-30 08:58:39,101 INFO sqlalchemy.engine.Engine SELECT pg_catalog.pg_class.relname 
FROM pg_catalog.pg_class JOIN pg_catalog.pg_namespace ON pg_catalog.pg_namespace.oid = pg_catalog.pg_class.relnamespace 
WHERE pg_catalog.pg_class.relname = %(table_name)s AND pg_catalog.pg_class.relkind = ANY (ARRAY[%(param_1)s, %(param_2)s, %(param_3)s, %(param_4)s, %(param_5)s]) AND pg_catalog.pg_table_is_visible(pg_catalog.pg_class.oid) AND pg_catalog.pg_namespace.nspname != %(nspname_1)s
2023-09-30 08:58:39,101 INFO sqlalchemy.engine.Engine [generated in 0.00016s] {'table_name': 'alembic_version', 'param_1': 'r', 'param_2': 'p', 'param_3': 'f', 'param_4': 'v', 'param_5': 'm', 'nspname_1': 'pg_catalog'}
2023-09-30 08:58:39,103 INFO sqlalchemy.engine.Engine COMMIT
ERROR:    Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/llama-app-backend--Qk0ygDj-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 677, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "/usr/local/lib/python3.11/contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/sec-insights/backend/app/main.py", line 77, in lifespan
    raise Exception(
Exception: Database is not up to date. Please run `poetry run alembic upgrade head`

ERROR:    Application startup failed. Exiting.

Not sure what the missing step is. I presume to keep the first make run terminal open per this line. Or do I rather ctrl+c to exit and run everything in that terminal? i think I've probably tried both. In any case I get stuck with above in both scenarios once I've got the Exception: Database is not up to date. Please run poetry run alembic upgrade head error

sourabhdesai commented 11 months ago

@mattarderne thanks for checking out the project and for the detailed list of issues. I'll try to address the errors Im seeing one by one:

I presume to keep the first make run terminal open per this line. Or do I rather ctrl+c to exit and run everything in that terminal? i think I've probably tried both. In any case I get stuck with above in both scenarios once I've got the Exception: Database is not up to date. Please run poetry run alembic upgrade head error

In regards to this, yes I would just do ctrl+c to stop the server process before running any of the seed_db_local, refresh_db, or migrate Makefile commands. You will need to have successfully applied the Alembic migrations before being able to start the backend server.

One thing that may help is using the devcontainer configuration. You can spin this up either on Github Codespaces or even in a locally running Docker conatiner through VS Code's devcontainer feature. This container sets up a development environment that is specifically configured for this project. NVM I see that you are in fact running this in a codespace haha

mattarderne commented 11 months ago

make seed_db_local: This prints out make[1]: awslocal: No such file or directory. Are you in the poetry shell (just type poetry shell) and have you run poetry install in that shell?

@sourabhdesai thanks this seemed to get it to work, I think the issue was having not re-entered the poetry shell at this step after exiting the first make run. I think this is just unfamiliarity with how Poetry works. The rest of the instructions worked with this info.

I've now not managed to get the frontend to load any documents in the document search drop down. I tested the backend and it is working at https://<codespaces url>.app.github.dev:8000/api/document/and I changed the NEXT_PUBLIC_BACKEND_URL=https://<codespaces url>-8000.app.github.dev/ but it doesn't seem to be connecting.

Thanks!

sourabhdesai commented 11 months ago

@mattarderne can you open dev tools in your browser and check the console logs? Most likely is due to CORS.

mattarderne commented 11 months ago
Access to fetch at 'https://< codespaces url >-8000.app.github.dev/api/document/' 
from origin 'https://< codespaces url >-3000.app.github.dev' has been blocked by 
CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch 
the resource with CORS disabled.

@sourabhdesai yes CORS, thanks

kikirizki commented 11 months ago

@mattarderne have you solved the issue? have you tried this add CODESPACE_NAME=your-code-space-name to your .env file in the backend then just set -a and source .env

then re-run the backend, hope this will help

jameszhucigna commented 11 months ago

@kikirizki Just want to follow up on this - I have added the relevant github info in the backend .env file:

DATABASE_URL=postgresql://user:password@127.0.0.1:5432/llama_app_db
BACKEND_CORS_ORIGINS='["http://localhost", "http://localhost:8000", "http://localhost:3000", "http://127.0.0.1:3000", "https://llama-app-backend.onrender.com", "https://llama-app-frontend.vercel.app", "http://secinsights.ai", "http://www.secinsights.ai", "https://secinsights.ai", "https://www.secinsights.ai"]'
OPENAI_API_KEY=xxx
LOG_LEVEL=debug
RENDER=False
S3_BUCKET_NAME=llama-app-backend-local
S3_ASSET_BUCKET_NAME=llama-app-web-assets-local
CDN_BASE_URL=http://llama-app-web-assets-local.s3-website.localhost.localstack.cloud:4566
AWS_KEY=xxx
AWS_SECRET=xxx
POLYGON_IO_API_KEY=xxx
CODESPACE_NAME=special-doodle-q7qp4969w9grc9jpg

And I also print out the origins variable in the code, which shows like below and seems to be working:

[AnyHttpUrl('http://localhost', ), AnyHttpUrl('http://localhost:8000', ), AnyHttpUrl('http://localhost:3000', ), AnyHttpUrl('http://127.0.0.1:3000', ), AnyHttpUrl('https://llama-app-backend.onrender.com', ), AnyHttpUrl('https://llama-app-frontend.vercel.app', ), AnyHttpUrl('http://secinsights.ai', ), AnyHttpUrl('http://www.secinsights.ai', ), AnyHttpUrl('https://secinsights.ai', ), AnyHttpUrl('https://www.secinsights.ai', ), 'https://special-doodle-q7qp4969w9grc9jpg-3000.app.github.dev']

However, I'm still getting this CORS error after I've restarted my backend:

Access to fetch at 'https://special-doodle-q7qp4969w9grc9jpg-8000.app.github.dev/api/document/' from origin 'https://special-doodle-q7qp4969w9grc9jpg-3000.app.github.dev' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Any insights here? From the backend code looks like we are trying to solve the CORS issue but it's not successful?

Also I've tried to manually add https://special-doodle-q7qp4969w9grc9jpg-3000.app.github.dev to BACKEND_CORS_ORIGINS='["http://localhost", "http://localhost:8000", "http://localhost:3000", "http://127.0.0.1:3000", "https://llama-app-backend.onrender.com", "https://llama-app-frontend.vercel.app", "http://secinsights.ai", "http://www.secinsights.ai", "https://secinsights.ai", "https://www.secinsights.ai"]' but still see the same issue.

jameszhucigna commented 11 months ago

OK, I think the above issue is solved. The tricky thing here is that the github codespace won't make your URL public accessible, so when codespace have a pop up asking you, you should click the "make it public" button to make sure your backend is publicly accessible. Otherwise Github requires you to login to access the backend, and the front end won't be able to do that (although they are launched in the same codespace)

kikirizki commented 11 months ago

@jameszhucigna thank you for the follow up, glad it's work for you