File size: 2,543 Bytes
b6f892b 8b7033c b6f892b 8b7033c b6f892b 84f5caf b6f892b 84f5caf b6f892b |
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 |
import urllib.request
from langchain.chains import RetrievalQA
from langchain_community.document_loaders import UnstructuredHTMLLoader
from langchain_openai import OpenAIEmbeddings
from langchain_openai import ChatOpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
import gradio as gr
# get the html data and save it to a file
url = "https://sea.ai/faq"
html = urllib.request.urlopen(url).read()
with open("FAQ_SEA.AI.html", "wb") as f:
f.write(html)
# load documents
loader = UnstructuredHTMLLoader("FAQ_SEA.AI.html")
documents = loader.load()
# split the documents into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
# select which embeddings we want to use
embeddings = OpenAIEmbeddings()
# create the vectorestore to use as the index
db = Chroma.from_documents(texts, embeddings)
# expose this index in a retriever interface
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
# create a chain to answer questions
qa = RetrievalQA.from_chain_type(
llm=ChatOpenAI(),
chain_type="stuff",
retriever=retriever,
return_source_documents=True,
verbose=True,
)
def answer_question(message, history, system):
# unwind the history of last 2 messages
history = " ".join(f"{user} {bot}" for user, bot in history[-2:])
# concatenate the history, message and system
query = " ".join([history, message, system])
retrieval_qa = qa.invoke(query)
result = retrieval_qa["result"]
result = result.replace('"', "").strip() # clean up the result
# query = retrieval_qa["query"]
# source_documents = retrieval_qa["source_documents"]
return result
title = "✨ SEA Dog"
description = """
<p align="center">
I have memorized the entire SEA.AI FAQ page. Ask me anything about it! 🧠
<br>
You can modify my response by using the <code>SYSTEM</code> input under
<code>Additional Inputs</code>.
</p>
"""
css = """
h1 {
text-align: center;
display: block;
}
"""
theme = gr.themes.Default(primary_hue=gr.themes.colors.indigo)
demo = gr.ChatInterface(
answer_question,
title=title,
description=description,
additional_inputs=[gr.Textbox("", label="SYSTEM")],
examples=[
["Can SEA.AI see at night?", "You are a helpful assistant."],
["Can SEA.AI see at night?", "Reply with sailor slang."],
],
css=css,
theme=theme,
)
if __name__ == "__main__":
demo.launch()
|