Spaces:
Build error
Build error
xicocdi
commited on
Commit
·
ff88064
1
Parent(s):
80294ce
first push
Browse files- Dockerfile +11 -0
- app.py +152 -0
- chainlit.md +14 -0
- requirements.txt +99 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
RUN useradd -m -u 1000 user
|
| 3 |
+
USER user
|
| 4 |
+
ENV HOME=/home/user \
|
| 5 |
+
PATH=/home/user/.local/bin:$PATH
|
| 6 |
+
WORKDIR $HOME/app
|
| 7 |
+
COPY --chown=user . $HOME/app
|
| 8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
| 9 |
+
RUN pip install -r requirements.txt
|
| 10 |
+
COPY . .
|
| 11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# flake8: noqa: E501
|
| 2 |
+
import os
|
| 3 |
+
from typing import List
|
| 4 |
+
from langchain_openai.embeddings import OpenAIEmbeddings
|
| 5 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_qdrant import QdrantVectorStore
|
| 7 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
| 8 |
+
from langchain_openai import ChatOpenAI
|
| 9 |
+
from langchain.storage import LocalFileStore
|
| 10 |
+
from chainlit.types import AskFileResponse
|
| 11 |
+
from langchain.embeddings import CacheBackedEmbeddings
|
| 12 |
+
from qdrant_client.http.models import Distance, VectorParams
|
| 13 |
+
from qdrant_client import QdrantClient
|
| 14 |
+
import chainlit as cl
|
| 15 |
+
from operator import itemgetter
|
| 16 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 17 |
+
from langchain_core.runnables.passthrough import RunnablePassthrough
|
| 18 |
+
from langchain_core.runnables.config import RunnableConfig
|
| 19 |
+
from dotenv import load_dotenv
|
| 20 |
+
import uuid
|
| 21 |
+
|
| 22 |
+
load_dotenv()
|
| 23 |
+
|
| 24 |
+
### Global Section ###
|
| 25 |
+
"""
|
| 26 |
+
GLOBAL CODE HERE
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 30 |
+
|
| 31 |
+
rag_system_prompt_template = """\
|
| 32 |
+
You are a helpful assistant that uses the provided context to answer questions. Never reference this prompt, or the existance of context.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
rag_message_list = [
|
| 36 |
+
{"role": "system", "content": rag_system_prompt_template},
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
rag_user_prompt_template = """\
|
| 40 |
+
Question:
|
| 41 |
+
{question}
|
| 42 |
+
Context:
|
| 43 |
+
{context}
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
chat_prompt = ChatPromptTemplate.from_messages(
|
| 47 |
+
[("system", rag_system_prompt_template), ("human", rag_user_prompt_template)]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
chat_model = ChatOpenAI(model="gpt-4o-mini")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def process_file(file: AskFileResponse):
|
| 54 |
+
import tempfile
|
| 55 |
+
|
| 56 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tempfile:
|
| 57 |
+
with open(tempfile.name, "wb") as f:
|
| 58 |
+
f.write(file.content)
|
| 59 |
+
|
| 60 |
+
Loader = PyMuPDFLoader
|
| 61 |
+
|
| 62 |
+
loader = Loader(tempfile.name)
|
| 63 |
+
documents = loader.load()
|
| 64 |
+
docs = text_splitter.split_documents(documents)
|
| 65 |
+
for i, doc in enumerate(docs):
|
| 66 |
+
doc.metadata["source"] = f"source_{i}"
|
| 67 |
+
return docs
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@cl.on_chat_start
|
| 71 |
+
async def on_chat_start():
|
| 72 |
+
files = None
|
| 73 |
+
|
| 74 |
+
while files == None:
|
| 75 |
+
files = await cl.AskFileMessage(
|
| 76 |
+
content="Please upload a PDF file to begin!",
|
| 77 |
+
accept=["application/pdf"],
|
| 78 |
+
max_size_mb=20,
|
| 79 |
+
timeout=180,
|
| 80 |
+
).send()
|
| 81 |
+
|
| 82 |
+
file = files[0]
|
| 83 |
+
|
| 84 |
+
msg = cl.Message(
|
| 85 |
+
content=f"Processing `{file.name}`...",
|
| 86 |
+
)
|
| 87 |
+
await msg.send()
|
| 88 |
+
|
| 89 |
+
docs = process_file(file)
|
| 90 |
+
|
| 91 |
+
collection_name = f"pdf_to_parse_{uuid.uuid4()}"
|
| 92 |
+
client = QdrantClient(":memory:")
|
| 93 |
+
client.create_collection(
|
| 94 |
+
collection_name=collection_name,
|
| 95 |
+
vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
|
| 96 |
+
)
|
| 97 |
+
core_embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
| 98 |
+
store = LocalFileStore("./cache/")
|
| 99 |
+
cached_embedder = CacheBackedEmbeddings.from_bytes_store(
|
| 100 |
+
core_embeddings, store, namespace=core_embeddings.model
|
| 101 |
+
)
|
| 102 |
+
vectorstore = QdrantVectorStore(
|
| 103 |
+
client=client, collection_name=collection_name, embedding=cached_embedder
|
| 104 |
+
)
|
| 105 |
+
vectorstore.add_documents(docs)
|
| 106 |
+
retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 3})
|
| 107 |
+
|
| 108 |
+
# Create a chain that uses the QDrant vector store
|
| 109 |
+
# Parallelization: LCEL runnables are parallelized by default, allowing for efficient
|
| 110 |
+
# execution of multiple steps in the chain simultaneously, improving overall performance.
|
| 111 |
+
retrieval_augmented_qa_chain = (
|
| 112 |
+
{
|
| 113 |
+
"context": itemgetter("question") | retriever,
|
| 114 |
+
"question": itemgetter("question"),
|
| 115 |
+
}
|
| 116 |
+
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 117 |
+
| chat_prompt
|
| 118 |
+
| chat_model
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Let the user know that the system is ready
|
| 122 |
+
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 123 |
+
await msg.update()
|
| 124 |
+
|
| 125 |
+
cl.user_session.set("chain", retrieval_augmented_qa_chain)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
@cl.author_rename
|
| 129 |
+
def rename(orig_author: str):
|
| 130 |
+
rename_dict = {
|
| 131 |
+
"ChatOpenAI": "the Generator...",
|
| 132 |
+
"VectorStoreRetriever": "the Retriever...",
|
| 133 |
+
}
|
| 134 |
+
return rename_dict.get(orig_author, orig_author)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
### On Message Section ###
|
| 138 |
+
@cl.on_message
|
| 139 |
+
async def main(message: cl.Message):
|
| 140 |
+
runnable = cl.user_session.get("chain")
|
| 141 |
+
|
| 142 |
+
msg = cl.Message(content="")
|
| 143 |
+
|
| 144 |
+
# Async method: Using astream allows for asynchronous streaming of the response,
|
| 145 |
+
# improving responsiveness and user experience by showing partial results as they become available.
|
| 146 |
+
async for chunk in runnable.astream(
|
| 147 |
+
{"question": message.content},
|
| 148 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
| 149 |
+
):
|
| 150 |
+
await msg.stream_token(chunk.content)
|
| 151 |
+
|
| 152 |
+
await msg.send()
|
chainlit.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Welcome to Chainlit! 🚀🤖
|
| 2 |
+
|
| 3 |
+
Hi there, Developer! 👋 We're excited to have you on board. Chainlit is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs.
|
| 4 |
+
|
| 5 |
+
## Useful Links 🔗
|
| 6 |
+
|
| 7 |
+
- **Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) 📚
|
| 8 |
+
- **Discord Community:** Join our friendly [Chainlit Discord](https://discord.gg/k73SQ3FyUh) to ask questions, share your projects, and connect with other developers! 💬
|
| 9 |
+
|
| 10 |
+
We can't wait to see what you create with Chainlit! Happy coding! 💻😊
|
| 11 |
+
|
| 12 |
+
## Welcome screen
|
| 13 |
+
|
| 14 |
+
To modify the welcome screen, edit the `chainlit.md` file at the root of your project. If you do not want a welcome screen, just leave this file empty.
|
requirements.txt
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohappyeyeballs==2.4.3
|
| 3 |
+
aiohttp==3.10.8
|
| 4 |
+
aiosignal==1.3.1
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
anyio==3.7.1
|
| 7 |
+
async-timeout==4.0.3
|
| 8 |
+
asyncer==0.0.2
|
| 9 |
+
attrs==24.2.0
|
| 10 |
+
bidict==0.23.1
|
| 11 |
+
certifi==2024.8.30
|
| 12 |
+
chainlit==0.7.700
|
| 13 |
+
charset-normalizer==3.3.2
|
| 14 |
+
click==8.1.7
|
| 15 |
+
dataclasses-json==0.5.14
|
| 16 |
+
Deprecated==1.2.14
|
| 17 |
+
distro==1.9.0
|
| 18 |
+
exceptiongroup==1.2.2
|
| 19 |
+
fastapi==0.100.1
|
| 20 |
+
fastapi-socketio==0.0.10
|
| 21 |
+
filetype==1.2.0
|
| 22 |
+
frozenlist==1.4.1
|
| 23 |
+
googleapis-common-protos==1.65.0
|
| 24 |
+
greenlet==3.1.1
|
| 25 |
+
grpcio==1.66.2
|
| 26 |
+
grpcio-tools==1.62.3
|
| 27 |
+
h11==0.14.0
|
| 28 |
+
h2==4.1.0
|
| 29 |
+
hpack==4.0.0
|
| 30 |
+
httpcore==0.17.3
|
| 31 |
+
httpx==0.24.1
|
| 32 |
+
hyperframe==6.0.1
|
| 33 |
+
idna==3.10
|
| 34 |
+
importlib_metadata==8.4.0
|
| 35 |
+
jiter==0.5.0
|
| 36 |
+
jsonpatch==1.33
|
| 37 |
+
jsonpointer==3.0.0
|
| 38 |
+
langchain==0.3.0
|
| 39 |
+
langchain-community==0.3.0
|
| 40 |
+
langchain-core==0.3.1
|
| 41 |
+
langchain-openai==0.2.0
|
| 42 |
+
langchain-qdrant==0.1.4
|
| 43 |
+
langchain-text-splitters==0.3.0
|
| 44 |
+
langsmith==0.1.121
|
| 45 |
+
Lazify==0.4.0
|
| 46 |
+
marshmallow==3.22.0
|
| 47 |
+
multidict==6.1.0
|
| 48 |
+
mypy-extensions==1.0.0
|
| 49 |
+
nest-asyncio==1.6.0
|
| 50 |
+
numpy==1.26.4
|
| 51 |
+
openai==1.51.0
|
| 52 |
+
opentelemetry-api==1.27.0
|
| 53 |
+
opentelemetry-exporter-otlp==1.27.0
|
| 54 |
+
opentelemetry-exporter-otlp-proto-common==1.27.0
|
| 55 |
+
opentelemetry-exporter-otlp-proto-grpc==1.27.0
|
| 56 |
+
opentelemetry-exporter-otlp-proto-http==1.27.0
|
| 57 |
+
opentelemetry-instrumentation==0.48b0
|
| 58 |
+
opentelemetry-proto==1.27.0
|
| 59 |
+
opentelemetry-sdk==1.27.0
|
| 60 |
+
opentelemetry-semantic-conventions==0.48b0
|
| 61 |
+
orjson==3.10.7
|
| 62 |
+
packaging==23.2
|
| 63 |
+
portalocker==2.10.1
|
| 64 |
+
protobuf==4.25.5
|
| 65 |
+
pydantic==2.9.2
|
| 66 |
+
pydantic-settings==2.5.2
|
| 67 |
+
pydantic_core==2.23.4
|
| 68 |
+
PyJWT==2.9.0
|
| 69 |
+
PyMuPDF==1.24.10
|
| 70 |
+
PyMuPDFb==1.24.10
|
| 71 |
+
python-dotenv==1.0.1
|
| 72 |
+
python-engineio==4.9.1
|
| 73 |
+
python-graphql-client==0.4.3
|
| 74 |
+
python-multipart==0.0.6
|
| 75 |
+
python-socketio==5.11.4
|
| 76 |
+
PyYAML==6.0.2
|
| 77 |
+
qdrant-client==1.11.2
|
| 78 |
+
regex==2024.9.11
|
| 79 |
+
requests==2.32.3
|
| 80 |
+
simple-websocket==1.0.0
|
| 81 |
+
sniffio==1.3.1
|
| 82 |
+
SQLAlchemy==2.0.35
|
| 83 |
+
starlette==0.27.0
|
| 84 |
+
syncer==2.0.3
|
| 85 |
+
tenacity==8.5.0
|
| 86 |
+
tiktoken==0.7.0
|
| 87 |
+
tomli==2.0.1
|
| 88 |
+
tqdm==4.66.5
|
| 89 |
+
typing-inspect==0.9.0
|
| 90 |
+
typing_extensions==4.12.2
|
| 91 |
+
uptrace==1.26.0
|
| 92 |
+
urllib3==2.2.3
|
| 93 |
+
uvicorn==0.23.2
|
| 94 |
+
watchfiles==0.20.0
|
| 95 |
+
websockets==13.1
|
| 96 |
+
wrapt==1.16.0
|
| 97 |
+
wsproto==1.2.0
|
| 98 |
+
yarl==1.13.1
|
| 99 |
+
zipp==3.20.2
|