jefalod commited on
Commit
0ce2499
·
verified ·
1 Parent(s): a9917b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -58
app.py CHANGED
@@ -1,80 +1,50 @@
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:
39
- client = Groq(api_key=self.api_key)
40
- messages = [
41
- {"role": "system", "content": "You are a helpful assistant."},
42
- {"role": "user", "content": prompt}
43
- ]
44
- response = client.chat.completions.create(
45
- model=self.model,
46
- messages=messages,
47
- temperature=self.temperature,
48
- )
49
- return response.choices[0].message.content
50
-
51
- @property
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(
59
  llm=groq_llm,
60
  retriever=retriever,
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()
 
1
  import os
2
+ import gradio as gr
 
3
  from langchain_community.document_loaders import TextLoader
 
 
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain_huggingface import HuggingFaceEmbeddings
6
+ from langchain_community.vectorstores import Chroma
7
  from langchain.chains import RetrievalQA
8
+ from langchain_groq import ChatGroq
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Load documents
11
  loader = TextLoader("sample_readme.txt")
12
  documents = loader.load()
13
+
14
+ # Split into chunks
15
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
16
  docs = text_splitter.split_documents(documents)
17
 
18
+ # Create embeddings
19
  embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
20
+
21
+ # Vector DB
22
  vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
23
+ retriever = vectorstore.as_retriever()
24
 
25
  # Groq LLM
26
+ groq_llm = ChatGroq(api_key=os.getenv("GROQ_API_KEY"), model_name="llama3-70b-8192")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # RAG chain
 
 
29
  qa_chain = RetrievalQA.from_chain_type(
30
  llm=groq_llm,
31
  retriever=retriever,
32
+ return_source_documents=False
33
  )
34
 
35
+ # Chat function
36
+ def chatbot_interface(user_query):
37
+ result = qa_chain({"query": user_query})
38
+ return result["result"]
39
+
40
  # Gradio UI
41
+ iface = gr.Interface(
42
+ fn=chatbot_interface,
43
+ inputs=gr.Textbox(label="Ask a question about the document"),
44
+ outputs=gr.Textbox(label="Answer"),
45
+ title="RAG Chatbot with Groq + LangChain",
46
+ description="Ask questions about sample_readme.txt using Groq LLM"
47
+ )
48
 
49
+ if __name__ == "__main__":
50
+ iface.launch()