Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 TextLoader
|
| 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 os
|
| 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 |
+
from langchain.document_loaders import PyPDFLoader
|
| 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 = "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" # Replace with your actual API key
|
| 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="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")
|
| 62 |
+
|
| 63 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 64 |
+
llm=groq_llm,
|
| 65 |
+
retriever=retriever,
|
| 66 |
+
return_source_documents=True
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
query = "What is a chassis?"
|
| 70 |
+
result = qa_chain({"query": query})
|
| 71 |
+
print("Answer:", result["result"])
|