Spaces:
Runtime error
Runtime error
moved vectorstore initialization outside of RAG
Browse files- RAG.py +8 -8
- streamlit_app.py +16 -5
RAG.py
CHANGED
|
@@ -15,15 +15,15 @@ from typing import Dict, Any, Optional, List, Tuple
|
|
| 15 |
import json
|
| 16 |
import logging
|
| 17 |
|
| 18 |
-
def retrieve(
|
| 19 |
start = time.time()
|
| 20 |
load_dotenv()
|
| 21 |
-
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 22 |
-
pc = Pinecone(api_key=pinecone_api_key)
|
| 23 |
|
| 24 |
-
index = pc.Index(index_name)
|
| 25 |
-
vector_store = PineconeVectorStore(index=index, embedding=embeddings)
|
| 26 |
-
results =
|
| 27 |
query,
|
| 28 |
k=k,
|
| 29 |
)
|
|
@@ -118,7 +118,7 @@ def parse_xml_and_check(xml_string: str) -> str:
|
|
| 118 |
|
| 119 |
return parsed_response.get('RESPONSE', "No response found in the output")
|
| 120 |
|
| 121 |
-
def RAG(llm: Any, query: str, index_name: str, embeddings: Any, top: int = 10, k: int = 100) -> Tuple[str, List[Document]]:
|
| 122 |
"""Main RAG function with improved error handling and validation."""
|
| 123 |
start = time.time()
|
| 124 |
try:
|
|
@@ -154,7 +154,7 @@ def RAG(llm: Any, query: str, index_name: str, embeddings: Any, top: int = 10, k
|
|
| 154 |
new_query = parse_xml_and_query(query=query,xml_string=query_response.content)
|
| 155 |
print(f"New_Query: {new_query}")
|
| 156 |
|
| 157 |
-
retrieved, _ = retrieve(
|
| 158 |
if not retrieved:
|
| 159 |
return "No documents found for your query.", []
|
| 160 |
|
|
|
|
| 15 |
import json
|
| 16 |
import logging
|
| 17 |
|
| 18 |
+
def retrieve(query: str,vectorstore:PineconeVectorStore, k: int = 1000) -> Tuple[List[Document], List[float]]:
|
| 19 |
start = time.time()
|
| 20 |
load_dotenv()
|
| 21 |
+
# pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 22 |
+
# pc = Pinecone(api_key=pinecone_api_key)
|
| 23 |
|
| 24 |
+
# index = pc.Index(index_name)
|
| 25 |
+
# vector_store = PineconeVectorStore(index=index, embedding=embeddings)
|
| 26 |
+
results = vectorstore.similarity_search_with_score(
|
| 27 |
query,
|
| 28 |
k=k,
|
| 29 |
)
|
|
|
|
| 118 |
|
| 119 |
return parsed_response.get('RESPONSE', "No response found in the output")
|
| 120 |
|
| 121 |
+
def RAG(llm: Any, query: str, index_name: str, embeddings: Any,vectorstore:PineconeVectorStore, top: int = 10, k: int = 100) -> Tuple[str, List[Document]]:
|
| 122 |
"""Main RAG function with improved error handling and validation."""
|
| 123 |
start = time.time()
|
| 124 |
try:
|
|
|
|
| 154 |
new_query = parse_xml_and_query(query=query,xml_string=query_response.content)
|
| 155 |
print(f"New_Query: {new_query}")
|
| 156 |
|
| 157 |
+
retrieved, _ = retrieve(query=new_query, vectorstore=vectorstore, k=k)
|
| 158 |
if not retrieved:
|
| 159 |
return "No documents found for your query.", []
|
| 160 |
|
streamlit_app.py
CHANGED
|
@@ -50,7 +50,9 @@ def process_message(
|
|
| 50 |
query: str,
|
| 51 |
llm: ChatOpenAI,
|
| 52 |
index_name: str,
|
| 53 |
-
embeddings: HuggingFaceEmbeddings
|
|
|
|
|
|
|
| 54 |
) -> Tuple[str, List]:
|
| 55 |
"""Process the user message using the RAG system."""
|
| 56 |
try:
|
|
@@ -58,7 +60,8 @@ def process_message(
|
|
| 58 |
query=query,
|
| 59 |
llm=llm,
|
| 60 |
index_name=index_name,
|
| 61 |
-
embeddings=embeddings
|
|
|
|
| 62 |
)
|
| 63 |
return response, sources
|
| 64 |
except Exception as e:
|
|
@@ -89,6 +92,10 @@ def display_sources(sources: List) -> None:
|
|
| 89 |
def main():
|
| 90 |
st.title("RAG Chatbot")
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# Initialize session state
|
| 93 |
if "messages" not in st.session_state:
|
| 94 |
st.session_state.messages = []
|
|
@@ -99,8 +106,11 @@ def main():
|
|
| 99 |
st.error("Failed to initialize the application. Please check the logs.")
|
| 100 |
return
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Display chat history
|
| 106 |
for message in st.session_state.messages:
|
|
@@ -122,7 +132,8 @@ def main():
|
|
| 122 |
query=user_input,
|
| 123 |
llm=llm,
|
| 124 |
index_name=INDEX_NAME,
|
| 125 |
-
embeddings=embeddings
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
if isinstance(response, str):
|
|
|
|
| 50 |
query: str,
|
| 51 |
llm: ChatOpenAI,
|
| 52 |
index_name: str,
|
| 53 |
+
embeddings: HuggingFaceEmbeddings,
|
| 54 |
+
vectorstore: PineconeVectorStore,
|
| 55 |
+
|
| 56 |
) -> Tuple[str, List]:
|
| 57 |
"""Process the user message using the RAG system."""
|
| 58 |
try:
|
|
|
|
| 60 |
query=query,
|
| 61 |
llm=llm,
|
| 62 |
index_name=index_name,
|
| 63 |
+
embeddings=embeddings,
|
| 64 |
+
vectorstore=vectorstore,
|
| 65 |
)
|
| 66 |
return response, sources
|
| 67 |
except Exception as e:
|
|
|
|
| 92 |
def main():
|
| 93 |
st.title("RAG Chatbot")
|
| 94 |
|
| 95 |
+
INDEX_NAME = 'bpl-rag'
|
| 96 |
+
|
| 97 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 98 |
+
|
| 99 |
# Initialize session state
|
| 100 |
if "messages" not in st.session_state:
|
| 101 |
st.session_state.messages = []
|
|
|
|
| 106 |
st.error("Failed to initialize the application. Please check the logs.")
|
| 107 |
return
|
| 108 |
|
| 109 |
+
#initialize vectorstore
|
| 110 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
| 111 |
+
|
| 112 |
+
index = pc.Index(INDEX_NAME)
|
| 113 |
+
vector_store = PineconeVectorStore(index=index, embedding=embeddings)
|
| 114 |
|
| 115 |
# Display chat history
|
| 116 |
for message in st.session_state.messages:
|
|
|
|
| 132 |
query=user_input,
|
| 133 |
llm=llm,
|
| 134 |
index_name=INDEX_NAME,
|
| 135 |
+
embeddings=embeddings,
|
| 136 |
+
vectorstore=vector_store
|
| 137 |
)
|
| 138 |
|
| 139 |
if isinstance(response, str):
|