vraman54 commited on
Commit
1933b6e
1 Parent(s): d47f5f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -41
app.py CHANGED
@@ -1,49 +1,82 @@
1
  import os
2
  from embedchain import App
3
  import streamlit as st
4
- import ollama
5
- from langchain.text_splitter import RecursiveCharacterTextSplitter
6
- from langchain_community.document_loaders import WebBaseLoader
7
- from langchain_community.vectorstores import Chroma
8
- from langchain_community.embeddings import OllamaEmbeddings
9
-
10
-
11
- #with st.sidebar:
12
- #huggingface_access_token = st.text_input("Hugging face Token", key="chatbot_api_key", type="password")
13
- #"[Get Hugging Face Access Token](https://huggingface.co/settings/tokens)"
14
- #"[View the source code](https://github.com/embedchain/examples/mistral-streamlit)"
15
-
16
- st.title("Chat with webpage")
17
- st.caption("This app allows you to chat with a webpage using local llama3 and RAG")
18
- #webpage_url = st.text_input("Enter web page URL", type = "default")
19
- webpage_url = "https://en.wikipedia.org/wiki/Indian_Premier_League"
20
- if webpage_url:
21
- loader = WebBaseLoader(webpage_url)
22
- docs = loader.load()
23
- text_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 10)
24
- splits = text_splitter.split_documents(docs)
25
-
26
- embeddings = OllamaEmbeddings(base_url='http://localhost:11434',model="llama2")
27
- vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
28
-
29
- def ollama_llm(question, context):
30
- formatted_prompt = f"Question: {question}\n\nContext: {context}"
31
- response = ollama.chat(model = "llama3", messages=[{'role':'user','content': formatted_prompt}])
32
- return response['message']['content']
33
 
34
- retriever = vectorstore.as_retriever()
35
- def combine_docs(docs):
36
- return "\n\n".join(doc.page_content for doc in docs)
 
37
 
38
- def rag_chain(question):
39
- retrieved_docs = retriever.invoke(question)
40
- formatted_context = combine_docs(retrieved_docs)
41
- return ollama_llm(question, formatted_context)
42
 
43
- st.success(f"Loaded {webpage_url} successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- prompt = st.text_input("Ask any question about the webpage")
 
 
46
 
47
- if prompt:
48
- result = rag_chain(prompt)
49
- st.write(result)
 
1
  import os
2
  from embedchain import App
3
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ with st.sidebar:
6
+ huggingface_access_token = st.text_input("Hugging face Token", key="chatbot_api_key", type="password")
7
+ "[Get Hugging Face Access Token](https://huggingface.co/settings/tokens)"
8
+ "[View the source code](https://github.com/embedchain/examples/mistral-streamlit)"
9
 
 
 
 
 
10
 
11
+ config = {
12
+ 'llm': {
13
+ 'provider': 'huggingface',
14
+ 'config': {
15
+ 'model': 'mistralai/Mistral-7B-Instruct-v0.2',
16
+ 'top_p': 0.5
17
+ }
18
+ },
19
+ 'embedder': {
20
+ 'provider': 'huggingface',
21
+ 'config': {
22
+ 'model': 'sentence-transformers/all-mpnet-base-v2'
23
+ }
24
+ }
25
+ }
26
+
27
+
28
+ st.title("💬 Chatbot")
29
+ st.caption("🚀 An Embedchain app powered by Mistral!")
30
+ if "messages" not in st.session_state:
31
+ st.session_state.messages = [
32
+ {
33
+ "role": "assistant",
34
+ "content": """
35
+ Hi! I'm a chatbot. I can answer questions and learn new things!\n
36
+ Ask me anything and if you want me to learn something do `/add <source>`.\n
37
+ I can learn mostly everything. :)
38
+ """,
39
+ }
40
+ ]
41
+
42
+ for message in st.session_state.messages:
43
+ with st.chat_message(message["role"]):
44
+ st.markdown(message["content"])
45
+
46
+ if prompt := st.chat_input("Ask me anything!"):
47
+ if not st.session_state.chatbot_api_key:
48
+ st.error("Please enter your Hugging Face Access Token")
49
+ st.stop()
50
+
51
+ os.environ["HUGGINGFACE_ACCESS_TOKEN"] = st.session_state.chatbot_api_key
52
+ app = App.from_config(config = config)
53
+ app.add("https://en.wikipedia.org/wiki/Indian_Premier_League")
54
+
55
+ if prompt.startswith("/add"):
56
+ with st.chat_message("user"):
57
+ st.markdown(prompt)
58
+ st.session_state.messages.append({"role": "user", "content": prompt})
59
+ prompt = prompt.replace("/add", "").strip()
60
+ with st.chat_message("assistant"):
61
+ message_placeholder = st.empty()
62
+ message_placeholder.markdown("Adding to knowledge base...")
63
+ app.add(prompt)
64
+ message_placeholder.markdown(f"Added {prompt} to knowledge base!")
65
+ st.session_state.messages.append({"role": "assistant", "content": f"Added {prompt} to knowledge base!"})
66
+ st.stop()
67
+
68
+ with st.chat_message("user"):
69
+ st.markdown(prompt)
70
+ st.session_state.messages.append({"role": "user", "content": prompt})
71
+
72
+ with st.chat_message("assistant"):
73
+ msg_placeholder = st.empty()
74
+ msg_placeholder.markdown("Thinking...")
75
+ full_response = ""
76
 
77
+ for response in app.chat(prompt):
78
+ msg_placeholder.empty()
79
+ full_response += response
80
 
81
+ msg_placeholder.markdown(full_response)
82
+ st.session_state.messages.append({"role": "assistant", "content": full_response})