File size: 1,812 Bytes
9fd14cb
ac2329b
 
 
 
9fd14cb
ac2329b
ed6578e
 
ac2329b
 
9fd14cb
ac2329b
 
 
9fd14cb
ac2329b
 
 
9fd14cb
ac2329b
 
 
 
 
9fd14cb
ac2329b
 
 
 
 
9fd14cb
ac2329b
 
9fd14cb
ac2329b
9fd14cb
ac2329b
 
9fd14cb
ac2329b
9fd14cb
ac2329b
 
9fd14cb
 
ac2329b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
from pinecone import Pinecone
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
from langchain.chains.llm import LLMChain

# ========== CONFIGURATION ========== #
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
INDEX_NAME = "rag-granite-index"
NAMESPACE = "rag-ns"

# ========== SETUP ========== #
st.set_page_config(page_title="RAG Assistant", page_icon="🤖")
st.title("💬 RAG-Powered Q&A Assistant")

# Init Pinecone
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(INDEX_NAME)

# Init Groq LLM
llm = ChatGroq(
    model_name="llama3-70b-8192",
    api_key=GROQ_API_KEY
)

# Prompt Template
prompt = PromptTemplate(
    input_variables=["context", "question"],
    template="""
You are a smart assistant. Based on the provided context, answer the question in 1 to 2 lines only.

Context:
{context}

Question: {question}

Answer:"""
)

llm_chain = LLMChain(llm=llm, prompt=prompt)

# ========== STREAMLIT UI ========== #
user_query = st.text_input("Ask a question from the document 👇")

if user_query:
    with st.spinner("Fetching answer..."):
        from sentence_transformers import SentenceTransformer
        embedder = SentenceTransformer("all-MiniLM-L6-v2")  # Match your embedding model
        query_embedding = embedder.encode(user_query).tolist()

        results = index.query(
            namespace=NAMESPACE,
            vector=query_embedding,
            top_k=3,
            include_metadata=True
        )

        context = "\n\n".join(match['metadata']['text'] for match in results['matches'])

        response = llm_chain.invoke({
            "context": context,
            "question": user_query
        })

        st.success("Answer:")
        st.write(response["text"])