File size: 1,202 Bytes
02f0374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# %%
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()


# %%