From e3d6b822d397c6e38024b28cbbba5ec05105df51 Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Mon, 15 Apr 2024 20:52:33 +0000 Subject: [PATCH] fix redis config --- .devcontainer/devcontainer.json | 1 + .devcontainer/docker-compose.yml | 2 +- server/cli.py | 17 +++++++++++++++++ server/config.py | 3 +-- server/nlp/embeddings.py | 6 ++---- server_tests/conftest.py | 11 +++++------ 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index f3e633a27..43c8ba461 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -15,6 +15,7 @@ 5173, 2010, 6379, + 6380, 5432 ], "postCreateCommand": "bash scripts/devcontainer_setup.sh", diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 809b883c0..cda0a937f 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -25,7 +25,7 @@ services: image: redis/redis-stack-server:7.2.0-v6 restart: unless-stopped ports: - - "6380:6379" + - "6379:6379" volumes: - data:/home/pigeon networks: diff --git a/server/cli.py b/server/cli.py index 4469640a4..fcd907232 100644 --- a/server/cli.py +++ b/server/cli.py @@ -52,6 +52,20 @@ def _generate_test_documents(): return test_documents +def _embed_existing_documents(documents: list[Document]): + """Embed existing documents.""" + to_embed_documents = [ + { + "question": doc.question, + "source": doc.source, + "content": doc.content, + "sql_id": doc.id, + } + for doc in documents + ] + embed_corpus(to_embed_documents) + + @seed.cli.command() def corpus(): """Add test documents to the corpus.""" @@ -70,6 +84,9 @@ def email(): print("No documents in the database. Generating test documents...") test_documents = _generate_test_documents() embed_corpus(test_documents) + else: + print("Embedding existing documents...") + _embed_existing_documents(docs) subject = "Test Email Subject" body = "Hello! What is blueprint?" diff --git a/server/config.py b/server/config.py index 1bf1736f4..41ce6ae27 100644 --- a/server/config.py +++ b/server/config.py @@ -36,8 +36,7 @@ def _get_config_option(name: str, default_value: str | None = None) -> str: SQLALCHEMY_DATABASE_URI = _get_config_option( "DATABASE_URL", "postgresql://postgres:password@database/pigeondb" ) -REDIS_URL = _get_config_option("REDIS_URL", "redis") -REDIS_DB_INDEX = _get_config_option("REDIS_DB_INDEX", "0") +REDIS_HOST = _get_config_option("REDIS_HOST", "redis") FLASK_RUN_PORT = 2010 DEBUG = True diff --git a/server/nlp/embeddings.py b/server/nlp/embeddings.py index 532c0c4b4..3fe751328 100644 --- a/server/nlp/embeddings.py +++ b/server/nlp/embeddings.py @@ -17,16 +17,14 @@ from redis.commands.search.indexDefinition import IndexDefinition, IndexType from redis.commands.search.query import Query -from server.config import REDIS_DB_INDEX, REDIS_URL, RedisDocument +from server.config import REDIS_HOST, RedisDocument cwd = os.path.dirname(__file__) VECTOR_DIMENSION = 1536 # load redis client -client = redis.Redis( - host=REDIS_URL, port=6379, decode_responses=True, db=int(REDIS_DB_INDEX) -) +client = redis.Redis(host=REDIS_HOST, port=6379, decode_responses=True) # load corpus # with open('corpus.json', 'r') as f: diff --git a/server_tests/conftest.py b/server_tests/conftest.py index 8ed159539..66731e726 100644 --- a/server_tests/conftest.py +++ b/server_tests/conftest.py @@ -43,22 +43,21 @@ def db_url(db_name="pigeondb_test"): @pytest.fixture(scope="session") def redis_db_index(): - """Yields test db index for Redis. + """Yields test redis db host. Flushes test db if it already exists. """ - test_db_index = 1 - client = redis.Redis(host="redis", port=6379, db=test_db_index) + client = redis.Redis(host="redis-test", port=6379) client.flushdb() client.close() - yield test_db_index + yield "redis-test" @pytest.fixture(scope="session") -def app(db_url: str, redis_db_index: int): +def app(db_url: str, redis_db_index: str): os.environ["DATABASE_URL"] = db_url - os.environ["REDIS_DB_INDEX"] = str(redis_db_index) + os.environ["REDIS_HOST"] = redis_db_index app = create_app() app.config.update(