Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,49 @@
|
|
|
|
|
| 1 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 2 |
from langchain_community.vectorstores import Chroma
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain.document_loaders import
|
| 5 |
from langchain.chains import RetrievalQA
|
| 6 |
from langchain.llms.base import LLM
|
| 7 |
-
|
| 8 |
-
from typing import List, Optional
|
| 9 |
from groq import Groq
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
sample_text = '''# Sample Project
|
| 13 |
-
|
| 14 |
-
This project demonstrates an example of a LangChain-powered RAG pipeline. It uses FAISS for vector search and a GROQ-hosted LLaMA3 model for response generation.
|
| 15 |
-
|
| 16 |
-
## Features
|
| 17 |
-
|
| 18 |
-
- Document embedding
|
| 19 |
-
- Vector similarity search
|
| 20 |
-
- LLM-based QA over documents
|
| 21 |
-
'''
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
loader = PyPDFLoader("/content/ivas103.pdf")
|
| 26 |
documents = loader.load()
|
| 27 |
|
| 28 |
-
|
| 29 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 30 |
docs = text_splitter.split_documents(documents)
|
| 31 |
|
| 32 |
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 33 |
vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
|
| 34 |
|
|
|
|
| 35 |
class GroqLLM(LLM):
|
| 36 |
model: str = "llama3-8b-8192"
|
| 37 |
-
api_key: str = "
|
| 38 |
temperature: float = 0.7
|
| 39 |
|
| 40 |
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 41 |
client = Groq(api_key=self.api_key)
|
| 42 |
-
|
| 43 |
messages = [
|
| 44 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 45 |
{"role": "user", "content": prompt}
|
| 46 |
]
|
| 47 |
-
|
| 48 |
response = client.chat.completions.create(
|
| 49 |
model=self.model,
|
| 50 |
messages=messages,
|
| 51 |
temperature=self.temperature,
|
| 52 |
)
|
| 53 |
-
|
| 54 |
return response.choices[0].message.content
|
| 55 |
|
| 56 |
@property
|
| 57 |
def _llm_type(self) -> str:
|
| 58 |
return "groq-llm"
|
| 59 |
|
|
|
|
| 60 |
retriever = vectorstore.as_retriever()
|
| 61 |
-
groq_llm = GroqLLM(api_key="
|
| 62 |
|
| 63 |
qa_chain = RetrievalQA.from_chain_type(
|
| 64 |
llm=groq_llm,
|
|
@@ -66,6 +51,20 @@ qa_chain = RetrievalQA.from_chain_type(
|
|
| 66 |
return_source_documents=True
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 3 |
from langchain_community.vectorstores import Chroma
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.document_loaders import PyPDFLoader
|
| 6 |
from langchain.chains import RetrievalQA
|
| 7 |
from langchain.llms.base import LLM
|
|
|
|
|
|
|
| 8 |
from groq import Groq
|
| 9 |
+
from typing import List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Step 1: Load PDF and prepare vector store
|
| 12 |
loader = PyPDFLoader("/content/ivas103.pdf")
|
| 13 |
documents = loader.load()
|
| 14 |
|
|
|
|
| 15 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 16 |
docs = text_splitter.split_documents(documents)
|
| 17 |
|
| 18 |
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 19 |
vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
|
| 20 |
|
| 21 |
+
# Step 2: Define custom LLM class using Groq
|
| 22 |
class GroqLLM(LLM):
|
| 23 |
model: str = "llama3-8b-8192"
|
| 24 |
+
api_key: str = "your_groq_api_key_here" # Replace with your key
|
| 25 |
temperature: float = 0.7
|
| 26 |
|
| 27 |
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 28 |
client = Groq(api_key=self.api_key)
|
|
|
|
| 29 |
messages = [
|
| 30 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 31 |
{"role": "user", "content": prompt}
|
| 32 |
]
|
|
|
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
model=self.model,
|
| 35 |
messages=messages,
|
| 36 |
temperature=self.temperature,
|
| 37 |
)
|
|
|
|
| 38 |
return response.choices[0].message.content
|
| 39 |
|
| 40 |
@property
|
| 41 |
def _llm_type(self) -> str:
|
| 42 |
return "groq-llm"
|
| 43 |
|
| 44 |
+
# Step 3: Create RetrievalQA chain
|
| 45 |
retriever = vectorstore.as_retriever()
|
| 46 |
+
groq_llm = GroqLLM(api_key="your_groq_api_key_here") # Replace with your key
|
| 47 |
|
| 48 |
qa_chain = RetrievalQA.from_chain_type(
|
| 49 |
llm=groq_llm,
|
|
|
|
| 51 |
return_source_documents=True
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Step 4: Gradio interface function
|
| 55 |
+
def ask_question(query):
|
| 56 |
+
result = qa_chain({"query": query})
|
| 57 |
+
answer = result["result"]
|
| 58 |
+
sources = "\n\n".join([doc.metadata.get("source", "Unknown") for doc in result["source_documents"]])
|
| 59 |
+
return f"### Answer:\n{answer}\n\n### Sources:\n{sources}"
|
| 60 |
+
|
| 61 |
+
# Step 5: Launch Gradio UI
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=ask_question,
|
| 64 |
+
inputs=gr.Textbox(label="Ask a question", placeholder="e.g., What is a chassis?"),
|
| 65 |
+
outputs=gr.Markdown(),
|
| 66 |
+
title="📄 PDF RAG Chatbot (Groq + LangChain)",
|
| 67 |
+
description="Ask questions based on the content of the PDF file."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
iface.launch()
|