chatbot-yang / chatbot.py
kafai-lam's picture
feat: first draft chatbot yang
02f0374 unverified
Raw History Blame Contribute Delete
1.2 kB
# %%
import gradio
import openai
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import AzureChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
# %%
embeddings = OpenAIEmbeddings(
deployment="CapSuiteAda", client=openai.Embedding, chunk_size=16
)
vectorstore = FAISS.load_local(
folder_path="stores/faiss_vectorstore", embeddings=embeddings
)
# %%
llm = AzureChatOpenAI(deployment_name="CapSuiteGPT", temperature=0.0)
# %%
qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vectorstore.as_retriever(),
)
# %%
def chatbot(question, history=[]):
chat_history = list(map(tuple, history))
result = qa_chain({"question": question, "chat_history": chat_history})
return result["answer"]
# %%
chat_interface = gradio.ChatInterface(
fn=chatbot,
examples=[
"循道衛理楊震社會服務處主要從事什麼工作",
"詳細介紹循道衛理楊震社會服務處",
"循道衛理楊震社會服務處的服務內容",
"牽晴計劃的服務內容",
],
title="Custom-trained AI Chatbot",
)
chat_interface.launch()
# %%