Spaces:
Sleeping
Sleeping
innitial commit
Browse files- .chainlit/.langchain.db +0 -0
- .chainlit/config.toml +42 -0
- .gitattributes +1 -0
- .gitignore +1 -0
- Dockerfile.txt +11 -0
- app.py +45 -0
- chainlit.md +2 -0
- index_creation.py +26 -0
- requirements.txt +7 -0
.chainlit/.langchain.db
ADDED
Binary file (12.3 kB). View file
|
|
.chainlit/config.toml
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[project]
|
2 |
+
# If true (default), the app will be available to anonymous users.
|
3 |
+
# If false, users will need to authenticate and be part of the project to use the app.
|
4 |
+
public = true
|
5 |
+
|
6 |
+
# The project ID (found on https://cloud.chainlit.io).
|
7 |
+
# The project ID is required when public is set to false or when using the cloud database.
|
8 |
+
#id = ""
|
9 |
+
|
10 |
+
# Uncomment if you want to persist the chats.
|
11 |
+
# local will create a database in your .chainlit directory (requires node.js installed).
|
12 |
+
# cloud will use the Chainlit cloud database.
|
13 |
+
# custom will load use your custom client.
|
14 |
+
#database = "local"
|
15 |
+
|
16 |
+
# Whether to enable telemetry (default: true). No personal data is collected.
|
17 |
+
enable_telemetry = false
|
18 |
+
|
19 |
+
# List of environment variables to be provided by each user to use the app.
|
20 |
+
user_env = []
|
21 |
+
|
22 |
+
# Duration (in seconds) during which the session is saved when the connection is lost
|
23 |
+
session_timeout = 3600
|
24 |
+
|
25 |
+
[UI]
|
26 |
+
# Name of the app and chatbot.
|
27 |
+
name = "Broomva Book Chat"
|
28 |
+
|
29 |
+
# Description of the app and chatbot. This is used for HTML tags.
|
30 |
+
# description = ""
|
31 |
+
|
32 |
+
# The default value for the expand messages settings.
|
33 |
+
default_expand_messages = false
|
34 |
+
|
35 |
+
# Hide the chain of thought details from the user in the UI.
|
36 |
+
hide_cot = false
|
37 |
+
|
38 |
+
# Link to your github repo. This will add a github button in the UI's header.
|
39 |
+
# github = ""
|
40 |
+
|
41 |
+
[meta]
|
42 |
+
generated_by = "0.6.2"
|
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.faiss filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
Dockerfile.txt
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,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import chainlit as cl
|
4 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
5 |
+
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
+
from langchain.prompts.chat import (AIMessagePromptTemplate,
|
8 |
+
ChatPromptTemplate,
|
9 |
+
HumanMessagePromptTemplate)
|
10 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
+
from langchain.vectorstores import FAISS
|
12 |
+
|
13 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
14 |
+
embeddings = OpenAIEmbeddings()
|
15 |
+
|
16 |
+
@cl.on_chat_start
|
17 |
+
async def init():
|
18 |
+
vector_store = FAISS.load_local("docs.faiss", embeddings)
|
19 |
+
|
20 |
+
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
21 |
+
ChatOpenAI(temperature=0, streaming=True, model="gpt-4-1106-preview"),
|
22 |
+
chain_type="stuff",
|
23 |
+
retriever=vector_store.as_retriever(search_kwargs={"k": 7}),
|
24 |
+
)
|
25 |
+
|
26 |
+
cl.user_session.set("chain", chain)
|
27 |
+
|
28 |
+
|
29 |
+
@cl.on_message
|
30 |
+
async def main(message):
|
31 |
+
chain = cl.user_session.get("chain") # type: RetrievalQAWithSourcesChain
|
32 |
+
cb = cl.AsyncLangchainCallbackHandler(
|
33 |
+
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
|
34 |
+
)
|
35 |
+
cb.answer_reached = True
|
36 |
+
|
37 |
+
res = await chain.acall(message.content, callbacks=[cb])
|
38 |
+
|
39 |
+
if cb.has_streamed_final_answer:
|
40 |
+
await cb.final_stream.update()
|
41 |
+
else:
|
42 |
+
answer = res["answer"]
|
43 |
+
await cl.Message(
|
44 |
+
content=answer,
|
45 |
+
).send()
|
chainlit.md
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# Welcome to the Broomva Book Chat
|
2 |
+
|
index_creation.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
from langchain.document_loaders import DirectoryLoader, TextLoader
|
3 |
+
from langchain.embeddings import OpenAIEmbeddings
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
|
7 |
+
# %%
|
8 |
+
loader = DirectoryLoader(
|
9 |
+
"../../../docs", glob="./*.md", loader_cls=TextLoader, recursive=True
|
10 |
+
)
|
11 |
+
|
12 |
+
documents = loader.load()
|
13 |
+
# %%
|
14 |
+
|
15 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
16 |
+
texts = text_splitter.split_documents(documents)
|
17 |
+
|
18 |
+
# %%
|
19 |
+
embedding = OpenAIEmbeddings()
|
20 |
+
|
21 |
+
vectordb = FAISS.from_documents(documents=texts, embedding=embedding)
|
22 |
+
|
23 |
+
# %%
|
24 |
+
vectordb.save_local("docs.faiss")
|
25 |
+
|
26 |
+
# %%
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
chainlit
|
3 |
+
openai
|
4 |
+
faiss-cpu
|
5 |
+
chromadb
|
6 |
+
tiktoken
|
7 |
+
pymupdf
|