Commit
·
b6f892b
1
Parent(s):
d4c44e3
Basic chatbot functionality working
Browse files- .gitignore +1 -0
- README.md +1 -1
- app.py +85 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.html
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: FAQ Mate
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: FAQ Mate
|
3 |
+
emoji: 💬
|
4 |
colorFrom: blue
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import urllib.request
|
2 |
+
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
+
from langchain_community.document_loaders import UnstructuredHTMLLoader
|
5 |
+
from langchain_openai import OpenAIEmbeddings
|
6 |
+
from langchain_openai.llms import OpenAI
|
7 |
+
from langchain.text_splitter import CharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import Chroma
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
# get the html data and save it to a file
|
13 |
+
url = "https://sea.ai/faq"
|
14 |
+
html = urllib.request.urlopen(url).read()
|
15 |
+
with open("FAQ_SEA.AI.html", "wb") as f:
|
16 |
+
f.write(html)
|
17 |
+
|
18 |
+
# load documents
|
19 |
+
loader = UnstructuredHTMLLoader("FAQ_SEA.AI.html")
|
20 |
+
documents = loader.load()
|
21 |
+
# split the documents into chunks
|
22 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
23 |
+
texts = text_splitter.split_documents(documents)
|
24 |
+
# select which embeddings we want to use
|
25 |
+
embeddings = OpenAIEmbeddings()
|
26 |
+
|
27 |
+
# create the vectorestore to use as the index
|
28 |
+
db = Chroma.from_documents(texts, embeddings)
|
29 |
+
# expose this index in a retriever interface
|
30 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
31 |
+
# create a chain to answer questions
|
32 |
+
qa = RetrievalQA.from_chain_type(
|
33 |
+
llm=OpenAI(model="gpt-3.5-turbo-0125"),
|
34 |
+
chain_type="stuff",
|
35 |
+
retriever=retriever,
|
36 |
+
return_source_documents=True,
|
37 |
+
verbose=True,
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
def answer_question(message, history, system):
|
42 |
+
# unwind the history of last 2 messages
|
43 |
+
history = " ".join(f"{user} {bot}" for user, bot in history[-2:])
|
44 |
+
# concatenate the history, message and system
|
45 |
+
query = " ".join([history, message, system])
|
46 |
+
retrieval_qa = qa.invoke(query)
|
47 |
+
result = retrieval_qa["result"]
|
48 |
+
result = result.replace('"', "").strip() # clean up the result
|
49 |
+
# query = retrieval_qa["query"]
|
50 |
+
# source_documents = retrieval_qa["source_documents"]
|
51 |
+
return result
|
52 |
+
|
53 |
+
|
54 |
+
title = "✨ SEA Dog"
|
55 |
+
description = """
|
56 |
+
<p align="center">
|
57 |
+
I have memorized the entire SEA.AI FAQ page. Ask me anything about it! 🧠
|
58 |
+
<br>
|
59 |
+
You can modify my response by using the <code>SYSTEM</code> input under
|
60 |
+
<code>Additional Inputs</code>.
|
61 |
+
</p>
|
62 |
+
"""
|
63 |
+
|
64 |
+
css = """
|
65 |
+
h1 {
|
66 |
+
text-align: center;
|
67 |
+
display: block;
|
68 |
+
}
|
69 |
+
"""
|
70 |
+
|
71 |
+
demo = gr.ChatInterface(
|
72 |
+
answer_question,
|
73 |
+
title=title,
|
74 |
+
description=description,
|
75 |
+
additional_inputs=[gr.Textbox("", label="SYSTEM")],
|
76 |
+
examples=[
|
77 |
+
["Can SEA.AI see at night?", "You are a helpful assistant."],
|
78 |
+
["Can SEA.AI see at night?", "Reply with sailor slang."],
|
79 |
+
],
|
80 |
+
css=css,
|
81 |
+
analytics_enabled=True,
|
82 |
+
)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
unstructured
|
2 |
+
chroma
|
3 |
+
langchain
|
4 |
+
langchain_openai
|