Skip to content

Commit

Permalink
[Feat] Updating pinecone client to use serverless-index, adding best …
Browse files Browse the repository at this point in the history
…Dockerfile practices.

Adding example .env
  • Loading branch information
Davidnet committed Jan 29, 2024
1 parent 1811bb8 commit ae73734
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 1,163 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#----------------------------------------------------------------------------
# OpenAI
#----------------------------------------------------------------------------
OPENAI_TOKEN=your-api-key # Replace your-api-key with your personal API key

#----------------------------------------------------------------------------
# Pinecone
#----------------------------------------------------------------------------
PINECONE_TOKEN=your-api-key # Replace your-api-key with your personal API key
54 changes: 46 additions & 8 deletions docker-bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
FROM python:3.11-bullseye
# syntax=docker/dockerfile:1

WORKDIR /usr/src/app
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

COPY docker_bot /usr/src/app/docker_bot
COPY pyproject.toml /usr/src/app/pyproject.toml
COPY README.md /usr/src/app/README.md
ARG PYTHON_VERSION=3.11
FROM python:${PYTHON_VERSION}-slim as base

RUN pip install -e .
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to pyproject.toml to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=poetry.lock,target=poetry.lock \
--mount=type=bind,source=docker_bot,target=docker_bot \
--mount=type=bind,source=README.md,target=README.md \
python -m pip install .

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8504

HEALTHCHECK CMD curl --fail http://localhost:8504/_stcore/health

ENTRYPOINT ["streamlit", "run", "docker_bot/bot.py", "--server.port=8504", "--server.address=0.0.0.0"]
# Run the application.
CMD streamlit run docker_bot/bot.py --server.port=8504 --server.address=0.0.0.0
21 changes: 12 additions & 9 deletions docker-bot/docker_bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import pinecone
import streamlit as st
from dotenv import load_dotenv
from openai import OpenAI
from pinecone import Pinecone, ServerlessSpec
from streamlit.logger import get_logger

load_dotenv(".env")
Expand All @@ -16,14 +16,17 @@
@st.cache_resource
def load_pinecone(index_name="docker-genai"):
# initialize pinecone
pinecone.init(
api_key=os.getenv("PINECONE_TOKEN"),
environment=os.getenv("PINECONE_ENVIRONMENT"),
)
if index_name not in pinecone.list_indexes():
# we create a new index
pinecone.create_index(name=index_name, metric="cosine", dimension=1536)
index = pinecone.Index(index_name)
pc = Pinecone(api_key=os.getenv("PINECONE_TOKEN"))

if index_name not in pc.list_indexes().names():
logger.info(f"Creating index {index_name}, therefore there are no videos yet.")
pc.create_index(
name=index_name,
dimension=1536,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-west-2"),
)
index = pc.Index(index_name)
return index


Expand Down
Loading

0 comments on commit ae73734

Please sign in to comment.