Spaces:
Runtime error
Runtime error
Kushwanth Chowday Kandala
commited on
update app.py with pinecone connection
Browse files
app.py
CHANGED
|
@@ -31,12 +31,50 @@ model = SentenceTransformer("all-MiniLM-L6-v2", device=device)
|
|
| 31 |
st.divider()
|
| 32 |
|
| 33 |
# Creating a Index(Pinecone Vector Database)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def chat_actions():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
st.session_state["chat_history"].append(
|
| 36 |
{"role": "user", "content": st.session_state["chat_input"]},
|
| 37 |
)
|
| 38 |
|
| 39 |
-
response = model.
|
| 40 |
st.session_state["chat_history"].append(
|
| 41 |
{
|
| 42 |
"role": "assistant",
|
|
@@ -54,3 +92,18 @@ st.chat_input("Enter your message", on_submit=chat_actions, key="chat_input")
|
|
| 54 |
for i in st.session_state["chat_history"]:
|
| 55 |
with st.chat_message(name=i["role"]):
|
| 56 |
st.write(i["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
st.divider()
|
| 32 |
|
| 33 |
# Creating a Index(Pinecone Vector Database)
|
| 34 |
+
import os
|
| 35 |
+
import pinecone
|
| 36 |
+
|
| 37 |
+
from pinecone import Index, GRPCIndex
|
| 38 |
+
|
| 39 |
+
PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
|
| 40 |
+
PINECONE_ENV=os.getenv("PINECONE_ENV")
|
| 41 |
+
PINECONE_ENVIRONMENT=os.getenv("PINECONE_ENVIRONMENT")
|
| 42 |
+
|
| 43 |
+
def connect_pinecone():
|
| 44 |
+
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENV)
|
| 45 |
+
st.code(pinecone)
|
| 46 |
+
st.divider()
|
| 47 |
+
st.text(pinecone.list_indexes())
|
| 48 |
+
st.divider()
|
| 49 |
+
st.text(f"Succesfully connected to the pinecone")
|
| 50 |
+
return pinecone
|
| 51 |
+
|
| 52 |
+
def get_pinecone_semantic_index(pinecone):
|
| 53 |
+
index_name = "sematic-search"
|
| 54 |
+
|
| 55 |
+
# only create if it deosnot exists
|
| 56 |
+
if index_name not in pinecone.list_indexes():
|
| 57 |
+
pinecone.create_index(
|
| 58 |
+
name=index_name,
|
| 59 |
+
description="Semantic search",
|
| 60 |
+
dimension=model.get_sentence_embedding_dimension(),
|
| 61 |
+
metric="cosine",
|
| 62 |
+
)
|
| 63 |
+
# now connect to index
|
| 64 |
+
index = pinecone.GRPCIndex(index_name)
|
| 65 |
+
st.text(f"Succesfully connected to the pinecone")
|
| 66 |
+
return index
|
| 67 |
+
|
| 68 |
def chat_actions():
|
| 69 |
+
|
| 70 |
+
pinecone = connect_pinecone()
|
| 71 |
+
index = get_pinecone_semantic_index(pinecone semantic index)
|
| 72 |
+
|
| 73 |
st.session_state["chat_history"].append(
|
| 74 |
{"role": "user", "content": st.session_state["chat_input"]},
|
| 75 |
)
|
| 76 |
|
| 77 |
+
response = model.encode(st.session_state["chat_input"])
|
| 78 |
st.session_state["chat_history"].append(
|
| 79 |
{
|
| 80 |
"role": "assistant",
|
|
|
|
| 92 |
for i in st.session_state["chat_history"]:
|
| 93 |
with st.chat_message(name=i["role"]):
|
| 94 |
st.write(i["content"])
|
| 95 |
+
|
| 96 |
+
### Creating a Index(Pinecone Vector Database)
|
| 97 |
+
# %%writefile .env
|
| 98 |
+
PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
|
| 99 |
+
PINECONE_ENV=os.getenv("PINECONE_ENV")
|
| 100 |
+
PINECONE_ENVIRONMENT=os.getenv("PINECONE_ENVIRONMENT")
|
| 101 |
+
|
| 102 |
+
import os
|
| 103 |
+
import pinecone
|
| 104 |
+
|
| 105 |
+
from pinecone import Index, GRPCIndex
|
| 106 |
+
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENV)
|
| 107 |
+
st.text(pinecone)
|
| 108 |
+
|
| 109 |
+
|