Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,10 @@ from langchain_pinecone import PineconeVectorStore
|
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
import pinecone
|
| 11 |
|
| 12 |
# π ENV VARS (Set in HF Spaces Secrets)
|
|
@@ -20,26 +23,41 @@ def build_rag_chain():
|
|
| 20 |
pc = pinecone.Pinecone(api_key=PINECONE_API)
|
| 21 |
embedder = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
spec=pinecone.ServerlessSpec(cloud="aws", region="us-east-1")
|
| 28 |
-
)
|
| 29 |
with st.spinner("π₯ Loading docs & building vector index... (runs once)"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
loader = DirectoryLoader(DOCS_PATH, glob="**/*.md")
|
| 31 |
docs = loader.load()
|
|
|
|
|
|
|
| 32 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 33 |
chunks = splitter.split_documents(docs)
|
|
|
|
|
|
|
| 34 |
PineconeVectorStore.from_documents(chunks, embedder, index_name=INDEX_NAME)
|
| 35 |
st.success(f"β
Indexed {len(chunks)} chunks to Pinecone.")
|
| 36 |
else:
|
| 37 |
st.info("π Vector index already built. Loading...")
|
| 38 |
|
|
|
|
| 39 |
index = pc.Index(INDEX_NAME)
|
| 40 |
retriever = PineconeVectorStore(index=index, embedding=embedder).as_retriever(search_kwargs={"k": 3})
|
| 41 |
|
|
|
|
| 42 |
llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0.1, api_key=GROQ_API)
|
|
|
|
|
|
|
| 43 |
prompt = ChatPromptTemplate.from_template(
|
| 44 |
"""Answer strictly using the context. If unknown, say "Not in docs."
|
| 45 |
Always cite source files.
|
|
@@ -49,6 +67,8 @@ def build_rag_chain():
|
|
| 49 |
|
| 50 |
Question: {input}"""
|
| 51 |
)
|
|
|
|
|
|
|
| 52 |
stuff_chain = create_stuff_documents_chain(llm, prompt)
|
| 53 |
return create_retrieval_chain(retriever, stuff_chain)
|
| 54 |
|
|
@@ -63,7 +83,7 @@ if not PINECONE_API or not GROQ_API:
|
|
| 63 |
|
| 64 |
rag_chain = build_rag_chain()
|
| 65 |
|
| 66 |
-
query = st.text_input("Ask about the docs:", placeholder="e.g., How to configure
|
| 67 |
if query:
|
| 68 |
with st.spinner("π Retrieving & generating..."):
|
| 69 |
result = rag_chain.invoke({"input": query})
|
|
@@ -75,4 +95,4 @@ if query:
|
|
| 75 |
for doc in result["context"]:
|
| 76 |
st.markdown(f"**File:** `{doc.metadata.get('source', 'unknown')}`")
|
| 77 |
st.text(doc.page_content[:250] + "...")
|
| 78 |
-
st.divider()
|
|
|
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
+
|
| 10 |
+
# π§ FIX: LangChain 0.2.x moved these imports!
|
| 11 |
+
from langchain.chains import create_retrieval_chain
|
| 12 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 13 |
import pinecone
|
| 14 |
|
| 15 |
# π ENV VARS (Set in HF Spaces Secrets)
|
|
|
|
| 23 |
pc = pinecone.Pinecone(api_key=PINECONE_API)
|
| 24 |
embedder = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 25 |
|
| 26 |
+
# Check if index exists (Safe for Pinecone 5.x)
|
| 27 |
+
existing_indexes = pc.list_indexes().names()
|
| 28 |
+
|
| 29 |
+
if INDEX_NAME not in existing_indexes:
|
|
|
|
|
|
|
| 30 |
with st.spinner("π₯ Loading docs & building vector index... (runs once)"):
|
| 31 |
+
# Create Index
|
| 32 |
+
pc.create_index(
|
| 33 |
+
name=INDEX_NAME,
|
| 34 |
+
dimension=384,
|
| 35 |
+
metric="cosine",
|
| 36 |
+
spec=pinecone.ServerlessSpec(cloud="aws", region="us-east-1")
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Load Docs
|
| 40 |
loader = DirectoryLoader(DOCS_PATH, glob="**/*.md")
|
| 41 |
docs = loader.load()
|
| 42 |
+
|
| 43 |
+
# Chunk Docs
|
| 44 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 45 |
chunks = splitter.split_documents(docs)
|
| 46 |
+
|
| 47 |
+
# Embed & Store
|
| 48 |
PineconeVectorStore.from_documents(chunks, embedder, index_name=INDEX_NAME)
|
| 49 |
st.success(f"β
Indexed {len(chunks)} chunks to Pinecone.")
|
| 50 |
else:
|
| 51 |
st.info("π Vector index already built. Loading...")
|
| 52 |
|
| 53 |
+
# Connect to Index
|
| 54 |
index = pc.Index(INDEX_NAME)
|
| 55 |
retriever = PineconeVectorStore(index=index, embedding=embedder).as_retriever(search_kwargs={"k": 3})
|
| 56 |
|
| 57 |
+
# LLM Setup
|
| 58 |
llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0.1, api_key=GROQ_API)
|
| 59 |
+
|
| 60 |
+
# Prompt Setup
|
| 61 |
prompt = ChatPromptTemplate.from_template(
|
| 62 |
"""Answer strictly using the context. If unknown, say "Not in docs."
|
| 63 |
Always cite source files.
|
|
|
|
| 67 |
|
| 68 |
Question: {input}"""
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
# Chain Setup
|
| 72 |
stuff_chain = create_stuff_documents_chain(llm, prompt)
|
| 73 |
return create_retrieval_chain(retriever, stuff_chain)
|
| 74 |
|
|
|
|
| 83 |
|
| 84 |
rag_chain = build_rag_chain()
|
| 85 |
|
| 86 |
+
query = st.text_input("Ask about the docs:", placeholder="e.g., How to configure settings?")
|
| 87 |
if query:
|
| 88 |
with st.spinner("π Retrieving & generating..."):
|
| 89 |
result = rag_chain.invoke({"input": query})
|
|
|
|
| 95 |
for doc in result["context"]:
|
| 96 |
st.markdown(f"**File:** `{doc.metadata.get('source', 'unknown')}`")
|
| 97 |
st.text(doc.page_content[:250] + "...")
|
| 98 |
+
st.divider()
|