rdkulkarni
commited on
Commit
•
eaf690e
1
Parent(s):
aa5e923
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
from langchain.embeddings import OpenAIEmbeddings
|
5 |
+
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain.prompts import ChatPromptTemplate
|
7 |
+
from langchain.schema import StrOutputParser
|
8 |
+
from langchain.document_loaders import (
|
9 |
+
PyMuPDFLoader,
|
10 |
+
)
|
11 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
+
from langchain.vectorstores.chroma import Chroma
|
13 |
+
from langchain.indexes import SQLRecordManager, index
|
14 |
+
from langchain.schema import Document
|
15 |
+
from langchain.schema.runnable import Runnable, RunnablePassthrough, RunnableConfig
|
16 |
+
|
17 |
+
import chainlit as cl
|
18 |
+
|
19 |
+
|
20 |
+
chunk_size = 1024
|
21 |
+
chunk_overlap = 50
|
22 |
+
|
23 |
+
embeddings_model = OpenAIEmbeddings()
|
24 |
+
|
25 |
+
PDF_STORAGE_PATH = "./pdfs"
|
26 |
+
|
27 |
+
|
28 |
+
def process_pdfs(pdf_storage_path: str):
|
29 |
+
pdf_directory = Path(pdf_storage_path)
|
30 |
+
docs = [] # type: List[Document]
|
31 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
32 |
+
|
33 |
+
for pdf_path in pdf_directory.glob("*.pdf"):
|
34 |
+
loader = PyMuPDFLoader(str(pdf_path))
|
35 |
+
documents = loader.load()
|
36 |
+
docs += text_splitter.split_documents(documents)
|
37 |
+
|
38 |
+
doc_search = Chroma.from_documents(docs, embeddings_model)
|
39 |
+
|
40 |
+
namespace = "chromadb/my_documents"
|
41 |
+
record_manager = SQLRecordManager(
|
42 |
+
namespace, db_url="sqlite:///record_manager_cache.sql"
|
43 |
+
)
|
44 |
+
record_manager.create_schema()
|
45 |
+
|
46 |
+
index_result = index(
|
47 |
+
docs,
|
48 |
+
record_manager,
|
49 |
+
doc_search,
|
50 |
+
cleanup="incremental",
|
51 |
+
source_id_key="source",
|
52 |
+
)
|
53 |
+
|
54 |
+
return doc_search
|
55 |
+
|
56 |
+
|
57 |
+
doc_search = process_pdfs(PDF_STORAGE_PATH)
|
58 |
+
model = ChatOpenAI(model_name="gpt-4", streaming=True)
|
59 |
+
|
60 |
+
|
61 |
+
@cl.on_chat_start
|
62 |
+
async def on_chat_start():
|
63 |
+
template = """Answer the question based only on the following context:
|
64 |
+
|
65 |
+
{context}
|
66 |
+
|
67 |
+
Question: {question}
|
68 |
+
"""
|
69 |
+
prompt = ChatPromptTemplate.from_template(template)
|
70 |
+
|
71 |
+
def format_docs(docs):
|
72 |
+
return "\n\n".join([d.page_content for d in docs])
|
73 |
+
|
74 |
+
retriever = doc_search.as_retriever()
|
75 |
+
|
76 |
+
runnable = (
|
77 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
78 |
+
| prompt
|
79 |
+
| model
|
80 |
+
| StrOutputParser()
|
81 |
+
)
|
82 |
+
|
83 |
+
cl.user_session.set("runnable", runnable)
|
84 |
+
|
85 |
+
|
86 |
+
@cl.on_message
|
87 |
+
async def on_message(message: cl.Message):
|
88 |
+
runnable = cl.user_session.get("runnable") # type: Runnable
|
89 |
+
|
90 |
+
msg = cl.Message(content="")
|
91 |
+
await msg.send()
|
92 |
+
|
93 |
+
async for chunk in runnable.astream(
|
94 |
+
message.content,
|
95 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
96 |
+
):
|
97 |
+
await msg.stream_token(chunk)
|
98 |
+
|
99 |
+
await msg.update()
|