jefalod commited on
Commit
a9917b9
·
verified ·
1 Parent(s): 365e731

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -1,28 +1,38 @@
 
 
 
 
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
- from typing import List, Optional
8
  from groq import Groq
9
  import gradio as gr
10
- import os
11
 
12
- # Load and split the document
 
 
 
 
 
 
 
 
 
13
  loader = TextLoader("sample_readme.txt")
14
  documents = loader.load()
15
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
16
  docs = text_splitter.split_documents(documents)
17
 
18
- # Generate embeddings and persist them
19
  embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
20
  vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
21
 
22
- # Define custom Groq LLM wrapper
23
  class GroqLLM(LLM):
24
  model: str = "llama3-8b-8192"
25
- api_key: str = os.environ.get("GROQ_API_KEY") # Store securely in Hugging Face secrets
26
  temperature: float = 0.0
27
 
28
  def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
@@ -42,7 +52,7 @@ class GroqLLM(LLM):
42
  def _llm_type(self) -> str:
43
  return "groq-llm"
44
 
45
- # Initialize chain
46
  retriever = vectorstore.as_retriever()
47
  groq_llm = GroqLLM()
48
  qa_chain = RetrievalQA.from_chain_type(
@@ -51,17 +61,20 @@ qa_chain = RetrievalQA.from_chain_type(
51
  return_source_documents=True
52
  )
53
 
54
- # Gradio Interface
55
  def ask_question(query):
56
  response = qa_chain(query)
57
  answer = response["result"]
58
- sources = "\n".join([doc.metadata.get("source", "unknown") for doc in response["source_documents"]])
59
- return f"**Answer:** {answer}\n\n**Sources:**\n{sources}"
 
 
60
 
61
  gr.Interface(
62
  fn=ask_question,
63
- inputs=gr.Textbox(label="Ask a question"),
64
  outputs=gr.Markdown(),
65
- title="RAG Chatbot with Groq LLaMA3",
66
- description="Ask questions based on the content of a sample README document using LangChain + Chroma + Groq-hosted LLaMA3."
 
67
  ).launch()
 
1
+ import os
2
+ from typing import List, Optional
3
+
4
+ from langchain_community.document_loaders import TextLoader
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import Chroma
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
8
  from langchain.chains import RetrievalQA
9
  from langchain.llms.base import LLM
 
10
  from groq import Groq
11
  import gradio as gr
 
12
 
13
+ # Create sample file if missing
14
+ if not os.path.exists("sample_readme.txt"):
15
+ with open("sample_readme.txt", "w") as f:
16
+ f.write(
17
+ "# Sample Project\n\nThis project demonstrates an example of a LangChain-powered RAG pipeline. "
18
+ "It uses FAISS for vector search and a GROQ-hosted LLaMA3 model for response generation.\n\n"
19
+ "## Features\n- Document embedding\n- Vector similarity search\n- LLM-based QA over documents"
20
+ )
21
+
22
+ # Load & split
23
  loader = TextLoader("sample_readme.txt")
24
  documents = loader.load()
25
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
26
  docs = text_splitter.split_documents(documents)
27
 
28
+ # Embed & store vectors
29
  embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
30
  vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
31
 
32
+ # Groq LLM
33
  class GroqLLM(LLM):
34
  model: str = "llama3-8b-8192"
35
+ api_key: str = os.environ.get("GROQ_API_KEY")
36
  temperature: float = 0.0
37
 
38
  def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
 
52
  def _llm_type(self) -> str:
53
  return "groq-llm"
54
 
55
+ # QA setup
56
  retriever = vectorstore.as_retriever()
57
  groq_llm = GroqLLM()
58
  qa_chain = RetrievalQA.from_chain_type(
 
61
  return_source_documents=True
62
  )
63
 
64
+ # Gradio UI
65
  def ask_question(query):
66
  response = qa_chain(query)
67
  answer = response["result"]
68
+ sources = "\n".join(
69
+ [doc.metadata.get("source", "sample_readme.txt") for doc in response["source_documents"]]
70
+ )
71
+ return f"### Answer:\n{answer}\n\n### Sources:\n{sources}"
72
 
73
  gr.Interface(
74
  fn=ask_question,
75
+ inputs=gr.Textbox(label="Ask something about the README"),
76
  outputs=gr.Markdown(),
77
+ title="📄 RAG Chatbot with Groq LLaMA3",
78
+ description="Ask questions about a README file using a LangChain + Groq LLaMA3-powered chatbot.",
79
+ theme="soft"
80
  ).launch()