Spaces:
Runtime error
Runtime error
xicocdi
commited on
Commit
·
b902207
1
Parent(s):
eef1379
first push
Browse files- Dockerfile +11 -0
- app.py +122 -0
- chainlit.md +3 -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,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# flake8: noqa ignore
|
2 |
+
|
3 |
+
from langchain_community.vectorstores import FAISS
|
4 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
5 |
+
from langchain_core.prompts import PromptTemplate
|
6 |
+
from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
|
7 |
+
import numpy as np
|
8 |
+
from numpy.linalg import norm
|
9 |
+
from langchain_community.document_loaders import TextLoader
|
10 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
11 |
+
from langchain_community.vectorstores import FAISS
|
12 |
+
from operator import itemgetter
|
13 |
+
from langchain.schema.output_parser import StrOutputParser
|
14 |
+
from langchain.schema.runnable import RunnablePassthrough
|
15 |
+
from langchain_core.runnables.passthrough import RunnablePassthrough
|
16 |
+
from langchain_core.runnables.config import RunnableConfig
|
17 |
+
from dotenv import load_dotenv
|
18 |
+
import chainlit as cl
|
19 |
+
import os
|
20 |
+
import uuid
|
21 |
+
|
22 |
+
load_dotenv()
|
23 |
+
|
24 |
+
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
25 |
+
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
|
26 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
27 |
+
|
28 |
+
document_loader = TextLoader("data/paul-graham-to-kindle/paul_graham_essays.txt")
|
29 |
+
documents = document_loader.load()
|
30 |
+
|
31 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
32 |
+
split_documents = text_splitter.split_documents(documents)
|
33 |
+
|
34 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
35 |
+
model=HF_EMBED_ENDPOINT,
|
36 |
+
task="feature-extraction",
|
37 |
+
huggingfacehub_api_token=HF_TOKEN,
|
38 |
+
)
|
39 |
+
|
40 |
+
if os.path.exists("./data/vectorstore/index.faiss"):
|
41 |
+
vectorstore = FAISS.load_local(
|
42 |
+
"./data/vectorstore",
|
43 |
+
hf_embeddings,
|
44 |
+
)
|
45 |
+
hf_retriever = vectorstore.as_retriever()
|
46 |
+
print("Loaded Vectorstore")
|
47 |
+
else:
|
48 |
+
print("Indexing Files")
|
49 |
+
for i in range(0, len(split_documents), 32):
|
50 |
+
if i == 0:
|
51 |
+
vectorstore = FAISS.from_documents(
|
52 |
+
split_documents[i : i + 32], hf_embeddings
|
53 |
+
)
|
54 |
+
continue
|
55 |
+
vectorstore.add_documents(split_documents[i : i + 32])
|
56 |
+
|
57 |
+
hf_retriever = vectorstore.as_retriever()
|
58 |
+
|
59 |
+
RAG_PROMPT_TEMPLATE = """\
|
60 |
+
<|start_header_id|>system<|end_header_id|>
|
61 |
+
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
|
62 |
+
|
63 |
+
<|start_header_id|>user<|end_header_id|>
|
64 |
+
User Query:
|
65 |
+
{query}
|
66 |
+
|
67 |
+
Context:
|
68 |
+
{context}<|eot_id|>
|
69 |
+
|
70 |
+
<|start_header_id|>assistant<|end_header_id|>
|
71 |
+
"""
|
72 |
+
|
73 |
+
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
74 |
+
|
75 |
+
hf_llm = HuggingFaceEndpoint(
|
76 |
+
endpoint_url=f"{HF_LLM_ENDPOINT}",
|
77 |
+
max_new_tokens=512,
|
78 |
+
top_k=10,
|
79 |
+
top_p=0.95,
|
80 |
+
typical_p=0.95,
|
81 |
+
temperature=0.01,
|
82 |
+
repetition_penalty=1.03,
|
83 |
+
huggingfacehub_api_token=HF_TOKEN,
|
84 |
+
)
|
85 |
+
|
86 |
+
|
87 |
+
@cl.on_chat_start
|
88 |
+
async def on_chat_start():
|
89 |
+
lcel_rag_chain = (
|
90 |
+
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
91 |
+
| rag_prompt
|
92 |
+
| hf_llm
|
93 |
+
)
|
94 |
+
|
95 |
+
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
96 |
+
await cl.Message(
|
97 |
+
content="Hi! What questions do you have about Paul Graham's essays?"
|
98 |
+
).send()
|
99 |
+
|
100 |
+
|
101 |
+
@cl.author_rename
|
102 |
+
def rename(orig_author: str):
|
103 |
+
rename_dict = {
|
104 |
+
"ChatOpenAI": "the Generator...",
|
105 |
+
"VectorStoreRetriever": "the Retriever...",
|
106 |
+
}
|
107 |
+
return rename_dict.get(orig_author, orig_author)
|
108 |
+
|
109 |
+
|
110 |
+
@cl.on_message
|
111 |
+
async def main(message: cl.Message):
|
112 |
+
runnable = cl.user_session.get("lcel_rag_chain")
|
113 |
+
|
114 |
+
msg = cl.Message(content="")
|
115 |
+
|
116 |
+
async for chunk in runnable.astream(
|
117 |
+
{"query": message.content},
|
118 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
119 |
+
):
|
120 |
+
await msg.stream_token(chunk)
|
121 |
+
|
122 |
+
await msg.send()
|
chainlit.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# AIM Endpoint Assignment
|
2 |
+
|
3 |
+
Check out this cool app I made to chat with Paul Graham's essays!
|