Skip to content

Commit

Permalink
migrating pinecone (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
beingkk authored Jun 24, 2024
1 parent 8d4ed03 commit b5ab4b0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 86 deletions.
84 changes: 20 additions & 64 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Flask = "^2.3.2"
twilio = "^8.5.0"
aiofiles = "23.1.0"
gunicorn = "^21.2.0"
pinecone-client = "^2.2.2"
pinecone-client = "^4.1"
streamlit-feedback = "0.0.9"
boto3= "1.28.17"
s3fs = "^2023.9.0"
Expand Down
2 changes: 1 addition & 1 deletion src/genai/streamlit_pages/dm_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from genai.utils import read_json


def eyfs_dm_kb(index_name: str = "eyfs-index", sidebar: bool = True) -> None:
def eyfs_dm_kb(index_name: str = "eyfs-index-1718673592-index", sidebar: bool = True) -> None:
"""Run the Development Matters app."""

# Define your custom CSS
Expand Down
2 changes: 1 addition & 1 deletion src/genai/streamlit_pages/eyfs_compare_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def eyfs_compare() -> None:
"""Compare the EYFS prototypes."""
st.title("Compare the EYFS prototypes")
index = get_index(index_name="eyfs-index")
index = get_index(index_name="eyfs-index-1718673592-index")
areas_of_learning_desc = read_json("src/genai/eyfs/areas_of_learning.json")
aol = list(areas_of_learning_desc.keys())

Expand Down
2 changes: 1 addition & 1 deletion src/genai/streamlit_pages/parenting_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parenting_chatbot(aws_key: str, aws_secret: str, s3_path: str, sidebar: bool

selected_model = "gpt-3.5-turbo"
temperature = 0.6
pinecone_index = get_index(index_name="eyfs-index")
pinecone_index = get_index(index_name="eyfs-index-1718673592-index")

if sidebar:
with st.sidebar:
Expand Down
4 changes: 4 additions & 0 deletions src/genai/streamlit_pages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
from typing import Optional
from typing import Union

import dotenv
import pinecone
import streamlit as st

from genai.vector_index import PineconeIndex


dotenv.load_dotenv()


def reset_state(key: Optional[str] = None) -> None:
"""Delete the message placeholder state."""
keys = [
Expand Down
29 changes: 11 additions & 18 deletions src/genai/vector_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from typing import Optional

import pinecone
# import pinecone
from pinecone import Pinecone

from genai.utils import batch

Expand All @@ -23,24 +24,16 @@ def __init__(
"""Initialize the index."""

# Connect to pinecone
if api_key and environment:
pinecone.init(
api_key=api_key,
environment=environment,
)
if api_key:
self.pc = Pinecone(api_key=api_key)
else:
pinecone.init(
api_key=os.environ["PINECONE_API_KEY"],
environment=os.environ["PINECONE_REGION"],
)
self.pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])

def connect(self, index_name: str) -> pinecone.index.Index:
def connect(self, index_name: str) -> Pinecone.Index:
"""Connect to the index."""
if index_name not in pinecone.list_indexes():
raise ValueError(f"Index {index_name} does not exist.")

return pinecone.Index(index_name)
return self.pc.Index(index_name)

# To do: update for the new Pinecone API
def build_and_upsert(
self,
index_name: str,
Expand Down Expand Up @@ -81,10 +74,10 @@ def build_and_upsert(
if delete_if_exists:
self.delete(index_name)

if index_name in pinecone.list_indexes():
if index_name in pinecone.list_indexes(): # noqa: F821
index = self.connect(index_name)
else:
pinecone.create_index(
pinecone.create_index( # noqa: F821
index_name,
dimension=dimension,
metadata_config=metadata_config,
Expand All @@ -103,6 +96,6 @@ def build_and_upsert(
def delete(index_name: str) -> None:
"""Delete the index."""
try:
pinecone.delete_index(index_name)
pinecone.delete_index(index_name) # noqa: F821
except Exception as e:
print(e) # noqa: T001

0 comments on commit b5ab4b0

Please sign in to comment.