File size: 2,476 Bytes
736448d 1a7b2d4 2d6ed01 736448d 2d6ed01 09aa142 1a7b2d4 09aa142 1a7b2d4 2d6ed01 1a7b2d4 09aa142 1a7b2d4 09aa142 736448d 09aa142 736448d 09aa142 736448d 09aa142 2d6ed01 736448d 09aa142 736448d 09aa142 736448d 2d6ed01 09aa142 736448d 09aa142 736448d 09aa142 736448d 09aa142 736448d 2d6ed01 09aa142 2d6ed01 736448d 09aa142 2d6ed01 09aa142 |
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 69 70 71 72 73 74 75 76 77 |
import os
import streamlit as st
from groq import Groq
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader, PyPDFLoader
from langchain.chains import RetrievalQA
from langchain.llms.base import LLM
from tempfile import NamedTemporaryFile
# Load Groq API key from environment variable
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
st.error("❌ GROQ_API_KEY is not set in environment variables.")
st.stop()
# Initialize Groq client
groq_client = Groq(api_key=GROQ_API_KEY)
# Define a basic LLM wrapper for Groq
class GroqLLM(LLM):
def __init__(self, model_name="llama3-8b-8192"):
self.model_name = model_name
def _call(self, prompt, stop=None):
response = groq_client.chat.completions.create(
model=self.model_name,
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content.strip()
@property
def _llm_type(self):
return "groq_llm"
# Streamlit UI
st.set_page_config(page_title="Groq RAG App", layout="centered")
st.title("📚 RAG App with Groq + LangChain + FAISS")
st.write("Upload a PDF or TXT file, ask a question, and get smart answers.")
uploaded_file = st.file_uploader("Upload your document", type=["pdf", "txt"])
if uploaded_file:
with NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(uploaded_file.read())
tmp_path = tmp_file.name
# Load document based on file type
if uploaded_file.type == "application/pdf":
loader = PyPDFLoader(tmp_path)
else:
loader = TextLoader(tmp_path)
docs = loader.load()
# Split into chunks
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = splitter.split_documents(docs)
# Create embeddings and FAISS index
embeddings = HuggingFaceEmbeddings()
db = FAISS.from_documents(texts, embeddings)
# Set up RAG chain
retriever = db.as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm=GroqLLM(), retriever=retriever)
# Input box for user question
query = st.text_input("🔍 Ask a question about the document:")
if query:
with st.spinner("Thinking..."):
result = qa_chain.run(query)
st.markdown("### 🧠 Answer:")
st.success(result)
|