|
import subprocess |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.document_loaders.text import TextLoader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.schema import Document |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain import PromptTemplate |
|
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler |
|
from langchain.callbacks.manager import CallbackManager |
|
|
|
|
|
from langchain.vectorstores import FAISS |
|
from langchain.chains import RetrievalQA |
|
|
|
from langchain.memory import ConversationBufferMemory |
|
from langchain.chains import ConversationalRetrievalChain |
|
|
|
from huggingface_hub import hf_hub_download |
|
from langchain.llms import LlamaCpp |
|
|
|
import time |
|
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
|
|
|
|
|
loader = TextLoader("./blog_data_1.txt") |
|
pages = loader.load() |
|
|
|
def split_text(documents: list[Document]): |
|
text_splitter = RecursiveCharacterTextSplitter( |
|
chunk_size=1000, |
|
chunk_overlap=150, |
|
length_function=len, |
|
add_start_index=True, |
|
) |
|
chunks = text_splitter.split_documents(documents) |
|
print(f"Split {len(documents)} documents into {len(chunks)} chunks.") |
|
|
|
document = chunks[10] |
|
print(document.page_content) |
|
print(document.metadata) |
|
|
|
return chunks |
|
|
|
chunks_text = split_text(pages) |
|
|
|
print("chunks") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') |
|
|
|
docs_text = [doc.page_content for doc in chunks_text] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VectorStore = FAISS.from_texts(docs_text, embedding=embedding) |
|
|
|
MODEL_ID = "TheBloke/Mistral-7B-OpenOrca-GGUF" |
|
MODEL_BASENAME = "mistral-7b-openorca.Q4_K_M.gguf" |
|
|
|
model_path = hf_hub_download( |
|
repo_id=MODEL_ID, |
|
filename=MODEL_BASENAME, |
|
resume_download=True, |
|
) |
|
|
|
print("model_path : ", model_path) |
|
|
|
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) |
|
|
|
|
|
CONTEXT_WINDOW_SIZE = 1500 |
|
MAX_NEW_TOKENS = 2000 |
|
N_BATCH = 512 |
|
n_gpu_layers = 40 |
|
kwargs = { |
|
"model_path": model_path, |
|
"n_ctx": CONTEXT_WINDOW_SIZE, |
|
"max_tokens": MAX_NEW_TOKENS, |
|
"n_batch": N_BATCH, |
|
"n_gpu_layers": n_gpu_layers, |
|
"callback_manager": callback_manager, |
|
"verbose":True, |
|
} |
|
|
|
from langchain.callbacks.manager import CallbackManager |
|
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler |
|
from langchain.chains import LLMChain |
|
from langchain.llms import LlamaCpp |
|
|
|
|
|
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) |
|
|
|
n_gpu_layers = 40 |
|
n_batch = 512 |
|
max_tokens = 2000 |
|
|
|
llm = LlamaCpp( |
|
model_path=model_path, |
|
n_gpu_layers=n_gpu_layers, |
|
|
|
n_batch=n_batch, |
|
max_tokens= max_tokens, |
|
callback_manager=callback_manager, |
|
verbose=True, |
|
) |
|
|
|
llm = LlamaCpp(**kwargs) |
|
|
|
memory = ConversationBufferMemory( |
|
memory_key="chat_history", |
|
return_messages=True, |
|
input_key='question', |
|
output_key='answer' |
|
) |
|
|
|
|
|
|
|
qa = ConversationalRetrievalChain.from_llm( |
|
llm, |
|
chain_type="stuff", |
|
retriever=VectorStore.as_retriever(search_kwargs={"k": 5}), |
|
memory=memory, |
|
return_source_documents=True, |
|
verbose=False, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import streamlit as st |
|
import time |
|
|
|
|
|
st.set_page_config(page_title="π€πΌ π²π¦ Financial advisor is Here") |
|
|
|
|
|
with st.sidebar: |
|
st.title(' Mokawil.AI is Here π€πΌ π²π¦') |
|
st.markdown('π an AI-powered advisor designed to assist founders (or anyone aspiring to start their own company) with various aspects of business in Morocco, including legal considerations, budget planning, available investors, and strategies for success.') |
|
|
|
|
|
if "messages" not in st.session_state.keys(): |
|
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}] |
|
|
|
|
|
for message in st.session_state.messages: |
|
if message["role"] == "user" : |
|
with st.chat_message(message["role"], avatar="π¨βπ»"): |
|
st.write(message["content"]) |
|
else : |
|
with st.chat_message(message["role"], avatar="π€"): |
|
st.write(message["content"]) |
|
|
|
def clear_chat_history(): |
|
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}] |
|
|
|
st.sidebar.button('Clear Chat History', on_click=clear_chat_history) |
|
|
|
|
|
def generate_llama2_response(prompt_input): |
|
res = qa(f'''{prompt_input}''') |
|
return res['answer'] |
|
|
|
|
|
if prompt := st.chat_input("What is up?"): |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
with st.chat_message("user", avatar="π¨βπ»"): |
|
st.write(prompt) |
|
|
|
|
|
if st.session_state.messages[-1]["role"] != "assistant": |
|
with st.chat_message("assistant", avatar="π€"): |
|
with st.spinner("Thinking..."): |
|
response = generate_llama2_response(st.session_state.messages[-1]["content"]) |
|
placeholder = st.empty() |
|
full_response = '' |
|
for item in response: |
|
full_response += item |
|
placeholder.markdown(full_response) |
|
time.sleep(0.05) |
|
placeholder.markdown(full_response) |
|
message = {"role": "assistant", "content": full_response} |
|
st.session_state.messages.append(message) |
|
|
|
|
|
with st.sidebar : |
|
st.title('Input examples') |
|
def promptExample1(): |
|
prompt = "how can I start my company example 1" |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
def promptExample2(): |
|
prompt = "how can I start my company example 2" |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
def promptExample3(): |
|
prompt = "how can I start my company example 3" |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
st.sidebar.button('how can I start my company in morocco?', on_click=promptExample1) |
|
st.sidebar.button('What are some recommended cities for starting a business in finance', on_click=promptExample2) |
|
st.sidebar.button('what is the estimate money I need for starting my company', on_click=promptExample3) |
|
|