Basic version with llama2-7b
Browse files- .gitattributes +1 -0
- AstroBot/.chainlit/.langchain.db +0 -0
- AstroBot/.chainlit/config.toml +63 -0
- AstroBot/README.md +2 -0
- AstroBot/__pycache__/model.cpython-311.pyc +0 -0
- AstroBot/__pycache__/model.cpython-39.pyc +0 -0
- AstroBot/chainlit.md +4 -0
- AstroBot/load.py +27 -0
- AstroBot/model.py +94 -0
- AstroBot/requirements.txt +11 -0
- AstroBot/vectorstore/db_faiss/index.faiss +3 -0
- AstroBot/vectorstore/db_faiss/index.pkl +3 -0
.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 |
+
AstroBot/vectorstore/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
|
AstroBot/.chainlit/.langchain.db
ADDED
Binary file (81.9 kB). View file
|
|
AstroBot/.chainlit/config.toml
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = true
|
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 = "Chatbot"
|
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 |
+
# Override default MUI light theme. (Check theme.ts)
|
42 |
+
[UI.theme.light]
|
43 |
+
#background = "#FAFAFA"
|
44 |
+
#paper = "#FFFFFF"
|
45 |
+
|
46 |
+
[UI.theme.light.primary]
|
47 |
+
#main = "#F80061"
|
48 |
+
#dark = "#980039"
|
49 |
+
#light = "#FFE7EB"
|
50 |
+
|
51 |
+
# Override default MUI dark theme. (Check theme.ts)
|
52 |
+
[UI.theme.dark]
|
53 |
+
#background = "#FAFAFA"
|
54 |
+
#paper = "#FFFFFF"
|
55 |
+
|
56 |
+
[UI.theme.dark.primary]
|
57 |
+
#main = "#F80061"
|
58 |
+
#dark = "#980039"
|
59 |
+
#light = "#FFE7EB"
|
60 |
+
|
61 |
+
|
62 |
+
[meta]
|
63 |
+
generated_by = "0.6.2"
|
AstroBot/README.md
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# Astrobot
|
2 |
+
An AI powered chatbot based on llama2 for answering your questions related to astronomy and astrophysics.
|
AstroBot/__pycache__/model.cpython-311.pyc
ADDED
Binary file (4.65 kB). View file
|
|
AstroBot/__pycache__/model.cpython-39.pyc
ADDED
Binary file (3.01 kB). View file
|
|
AstroBot/chainlit.md
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Welcome to AstroBot! 🚀🤖
|
2 |
+
|
3 |
+
Hi there! 👋 I am AstroBot - a locally-hosted astronomy chatbot designed to answer your questions in the field of astronomy!
|
4 |
+
|
AstroBot/load.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
2 |
+
from langchain.vectorstores import FAISS
|
3 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
|
6 |
+
DATA_PATH = 'Files/'
|
7 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
8 |
+
|
9 |
+
# Create vector database
|
10 |
+
def create_vector_db():
|
11 |
+
loader = DirectoryLoader(DATA_PATH,
|
12 |
+
glob='*.pdf',
|
13 |
+
loader_cls=PyPDFLoader)
|
14 |
+
|
15 |
+
documents = loader.load()
|
16 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,
|
17 |
+
chunk_overlap=50)
|
18 |
+
texts = text_splitter.split_documents(documents)
|
19 |
+
|
20 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
|
21 |
+
model_kwargs={'device': 'cpu'})
|
22 |
+
|
23 |
+
db = FAISS.from_documents(texts, embeddings)
|
24 |
+
db.save_local(DB_FAISS_PATH)
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
create_vector_db()
|
AstroBot/model.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
2 |
+
from langchain import PromptTemplate
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.llms import CTransformers
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
import chainlit as cl
|
8 |
+
|
9 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
10 |
+
|
11 |
+
custom_prompt_template = """Use the following pieces of information to answer the user's questions.
|
12 |
+
If you don't know the answer, just say that you don't know, but don't try to make up an answer.
|
13 |
+
|
14 |
+
Context: {context}
|
15 |
+
Question: {question}
|
16 |
+
|
17 |
+
Only return the helpful answer below and nothing else.
|
18 |
+
Helpful answer:
|
19 |
+
"""
|
20 |
+
|
21 |
+
def set_custom_prompt():
|
22 |
+
"""
|
23 |
+
Prompt template for QA retrieval for each vectorstore
|
24 |
+
"""
|
25 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
26 |
+
input_variables=['context', 'question'])
|
27 |
+
return prompt
|
28 |
+
|
29 |
+
#Retrieval QA Chain
|
30 |
+
def retrieval_qa_chain(llm, prompt, db):
|
31 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm,
|
32 |
+
chain_type='stuff',
|
33 |
+
retriever=db.as_retriever(search_kwargs={'k': 2}),
|
34 |
+
return_source_documents=True,
|
35 |
+
chain_type_kwargs={'prompt': prompt}
|
36 |
+
)
|
37 |
+
return qa_chain
|
38 |
+
|
39 |
+
#Loading the model
|
40 |
+
def load_llm():
|
41 |
+
# Load the locally downloaded model here
|
42 |
+
llm = CTransformers(
|
43 |
+
model = "llama-2-7b-chat.ggmlv3.q8_0.bin",
|
44 |
+
model_type="llama",
|
45 |
+
max_new_tokens = 512,
|
46 |
+
temperature = 0.5
|
47 |
+
)
|
48 |
+
return llm
|
49 |
+
|
50 |
+
#QA Model Function
|
51 |
+
def qa_bot():
|
52 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
53 |
+
model_kwargs={'device': 'cpu'})
|
54 |
+
db = FAISS.load_local(DB_FAISS_PATH, embeddings)
|
55 |
+
llm = load_llm()
|
56 |
+
qa_prompt = set_custom_prompt()
|
57 |
+
qa = retrieval_qa_chain(llm, qa_prompt, db)
|
58 |
+
|
59 |
+
return qa
|
60 |
+
|
61 |
+
#output function
|
62 |
+
def final_result(query):
|
63 |
+
qa_result = qa_bot()
|
64 |
+
response = qa_result({'query': query})
|
65 |
+
return response
|
66 |
+
|
67 |
+
#chainlit code
|
68 |
+
@cl.on_chat_start
|
69 |
+
async def start():
|
70 |
+
chain = qa_bot()
|
71 |
+
msg = cl.Message(content="Starting the bot...")
|
72 |
+
await msg.send()
|
73 |
+
msg.content = "Hi, Welcome to AstroBot. What is your query?"
|
74 |
+
await msg.update()
|
75 |
+
|
76 |
+
cl.user_session.set("chain", chain)
|
77 |
+
|
78 |
+
@cl.on_message
|
79 |
+
async def main(message):
|
80 |
+
chain = cl.user_session.get("chain")
|
81 |
+
cb = cl.AsyncLangchainCallbackHandler(
|
82 |
+
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
|
83 |
+
)
|
84 |
+
cb.answer_reached = True
|
85 |
+
res = await chain.acall(message, callbacks=[cb])
|
86 |
+
answer = res["result"]
|
87 |
+
sources = res["source_documents"]
|
88 |
+
|
89 |
+
if sources:
|
90 |
+
answer += f"\nSources:" + str(sources)
|
91 |
+
else:
|
92 |
+
answer += "\nNo sources found"
|
93 |
+
|
94 |
+
await cl.Message(content=answer).send()
|
AstroBot/requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pypdf
|
2 |
+
langchain
|
3 |
+
torch
|
4 |
+
accelerate
|
5 |
+
bitsandbytes
|
6 |
+
transformers
|
7 |
+
sentence_transformers
|
8 |
+
faiss_cpu
|
9 |
+
chainlit
|
10 |
+
ctransformers
|
11 |
+
protobuf==3.20.*
|
AstroBot/vectorstore/db_faiss/index.faiss
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6caa034bb007e2add887fe4566d2ad634be7a2823e58a2725f1b5ec6a85be251
|
3 |
+
size 7620141
|
AstroBot/vectorstore/db_faiss/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:19c32c83481ebbdb462d24bcb194dab3e4d1b9865aa495def7f2cdadb8f326ef
|
3 |
+
size 2725130
|