Turbiling commited on
Commit
b60534d
·
verified ·
1 Parent(s): 9fedfff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import chainlit as cl
3
+ from langchain.document_loaders import PyPDFLoader
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.chains import RetrievalQA
8
+
9
+ # Optional: Switch to Groq API
10
+ USE_GROQ = False # Set to True if using Groq
11
+
12
+ if USE_GROQ:
13
+ import httpx
14
+
15
+ def load_pdf(file_path):
16
+ loader = PyPDFLoader(file_path)
17
+ pages = loader.load_and_split()
18
+ return pages
19
+
20
+ @cl.on_chat_start
21
+ async def start():
22
+ files = await cl.AskFileMessage(
23
+ content="📄 Please upload a PDF file to begin.",
24
+ accept=["application/pdf"],
25
+ max_size_mb=10
26
+ ).send()
27
+
28
+ file = files[0]
29
+ file_path = file.path
30
+
31
+ pages = load_pdf(file_path)
32
+
33
+ # Create vector store
34
+ embeddings = OpenAIEmbeddings()
35
+ vectordb = FAISS.from_documents(pages, embeddings)
36
+
37
+ retriever = vectordb.as_retriever()
38
+
39
+ if USE_GROQ:
40
+ # Custom RAG using Groq API
41
+ cl.user_session.set("retriever", retriever)
42
+ else:
43
+ # Standard LangChain RetrievalQA with OpenAI
44
+ qa_chain = RetrievalQA.from_chain_type(
45
+ llm=ChatOpenAI(temperature=0.2),
46
+ retriever=retriever
47
+ )
48
+ cl.user_session.set("chain", qa_chain)
49
+
50
+ @cl.on_message
51
+ async def chat(msg: cl.Message):
52
+ if USE_GROQ:
53
+ retriever = cl.user_session.get("retriever")
54
+ docs = retriever.get_relevant_documents(msg.content)
55
+ context = "\n".join([doc.page_content for doc in docs][:3])
56
+
57
+ headers = {
58
+ "Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
59
+ "Content-Type": "application/json"
60
+ }
61
+
62
+ body = {
63
+ "messages": [
64
+ {"role": "system", "content": f"You are a helpful assistant answering questions from a PDF. Use this context:\n{context}"},
65
+ {"role": "user", "content": msg.content}
66
+ ],
67
+ "model": "llama3-8b-8192",
68
+ "temperature": 0.5
69
+ }
70
+
71
+ async with httpx.AsyncClient() as client:
72
+ res = await client.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=body)
73
+
74
+ reply = res.json()["choices"][0]["message"]["content"]
75
+ await cl.Message(content=reply).send()
76
+
77
+ else:
78
+ chain = cl.user_session.get("chain")
79
+ response = chain.run(msg.content)
80
+ await cl.Message(content=response).send()