-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
190 lines (163 loc) · 6.49 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
"""Chatbot.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1SCb3lbRwKNVHqVO21mFEErfw3rX7QWoY
# You need to use the T4 gpu (free tier) or any GPU option to use the chatbot
"""
# Commented out IPython magic to ensure Python compatibility.
!pip install colab-xterm # https://pypi.org/project/colab-xterm/
# %load_ext colabxterm
!pip install colab-xterm -qqq
!pip install langchain -qqq
!pip install langchain_community -qqq
!pip install langchain faiss-cpu sentence-transformers
!pip -q install llama-index llama-index-embeddings-huggingface llama-index-llms-llama-cpp pypdf
!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip -q install llama-cpp-python
!pip install chromadb
!pip install streamlit ngrok -qqq
!pip install pyngrok
# Commented out IPython magic to ensure Python compatibility.
# %load_ext colabxterm
# %xterm
# When content appear write "curl -fsSL https://ollama.com/install.sh | sh"
# The next command is "ollama serve & ollama pull llama3.1"
# You may have to run ollama pull llama3.1 a second time by typing CTRL-c to stop the command and then use right click insert to use the previous command again
# Import Ollama module from Langchain
from langchain_community.llms import Ollama
# Initialize an instance of the Ollama model
llm = Ollama(model="llama3.1")
# Invoke the model to generate responses
response = llm.invoke("What is the capital of Denmark?")
print(response)
"""# You need to upload a document in the files/filer tab to test the LLM model"""
from langchain_community.llms import Ollama
from langchain.chains import RetrievalQA
from langchain.vectorstores import Chroma
from sentence_transformers import SentenceTransformer
from langchain.embeddings import SentenceTransformerEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
from llama_index.core import Prompt, StorageContext, load_index_from_storage, Settings, VectorStoreIndex, SimpleDirectoryReader, set_global_tokenizer
import os
import time
# Initialize the Llama 3 model
llm = Ollama(model="llama3.1")
# Create an embedding model
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
# Prepare documents
pdf_path = '/content/wiki.pdf'
filename_fn = lambda filename: {'file_name': os.path.basename(pdf_path)}
loader = SimpleDirectoryReader(input_files=[pdf_path], file_metadata=filename_fn)
documents = loader.load_data()
# Convert documents to the required format
texts = [doc.text for doc in documents] # Assuming the content is in `text` attribute
metadatas = [doc.metadata for doc in documents]
# Create Chroma vector store
vector_store = Chroma.from_texts(texts=texts, metadatas=metadatas, embedding=embeddings)
# Load the QA chain
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vector_store.as_retriever()
)
queries = [
"What is the Danish Refugee Council?",
"Who is the Secretary General of the Danish Refugee Council?",
"What are the key activities of the Danish Refugee Council?",
"What was the Danish Refugee Council's role in Syria?"
]
for query in queries:
response = qa_chain.run(query)
print(f"Query: {query}\nResponse: {response}\n")
# Commented out IPython magic to ensure Python compatibility.
# %%writefile app.py
# import streamlit as st
# import time
# from langchain_community.llms import Ollama
# from langchain.chains import RetrievalQA
# from langchain.vectorstores import Chroma
# from sentence_transformers import SentenceTransformer
# from langchain.embeddings import SentenceTransformerEmbeddings
# from llama_index.core import SimpleDirectoryReader
# # Set up the Streamlit interface
# st.title("DRC24/7")
#
# # Initialize session state for chat history
# if 'messages' not in st.session_state:
# st.session_state.messages = []
#
# # Upload PDF
# uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
#
# if uploaded_file is not None:
# # Initialize the Llama 3 model
# llm = Ollama(model="llama3.1")
#
# # Create an embedding model
# embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
#
# # Load and process the document
# with open("temp.pdf", "wb") as f:
# f.write(uploaded_file.getbuffer())
# loader = SimpleDirectoryReader(input_files=["temp.pdf"])
# documents = loader.load_data()
#
# # Convert documents to the required format
# texts = [doc.text for doc in documents]
# metadatas = [doc.metadata for doc in documents]
#
# # Create Chroma vector store
# vector_store = Chroma.from_texts(texts=texts, metadatas=metadatas, embedding=embeddings)
#
# # Load the QA chain
# qa_chain = RetrievalQA.from_chain_type(
# llm=llm,
# chain_type="stuff",
# retriever=vector_store.as_retriever()
# )
#
# # Display chat history
# for message in st.session_state.messages:
# with st.chat_message(message["role"]):
# st.markdown(message["content"])
#
# # User input for query
# if prompt := st.chat_input("What would you like to know about the Danish Refugee Council?"):
# # Add user message to chat history
# st.session_state.messages.append({"role": "user", "content": prompt})
#
# # Display user message
# with st.chat_message("user"):
# st.markdown(prompt)
#
# # Get the response
# with st.chat_message("assistant"):
# message_placeholder = st.empty()
# full_response = ""
#
# # Simulate stream of response with milliseconds delay
# for chunk in qa_chain.run(prompt).split():
# full_response += chunk + " "
# time.sleep(0.05)
# # Add a blinking cursor to simulate typing
# message_placeholder.markdown(full_response + "▌")
#
# message_placeholder.markdown(full_response)
#
# # Add assistant response to chat history
# st.session_state.messages.append({"role": "assistant", "content": full_response})
#
# else:
# st.write("Please upload a PDF file to proceed.")
from pyngrok import ngrok
# Replace 'YOUR_NGROK_AUTHTOKEN' with your actual Ngrok authtoken
ngrok.set_auth_token("Your API KEY")
# Run the Streamlit app in the background
!streamlit run app.py &>/dev/null&
# Start ngrok tunnel to the streamlit app
public_url = ngrok.connect(8501,'http')
print(f"Streamlit app is live at: {public_url}")
"""# For restarting the server"""
ngrok.disconnect(public_url)
ngrok.kill()