File size: 2,760 Bytes
1f95db9
 
d018b4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f117d
 
517ed4a
d018b4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f95db9
d018b4e
 
 
 
 
 
1f95db9
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from langchain.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain.embeddings.openai import OpenAIEmbeddings
# convo chain lib
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)
OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
embedding = OpenAIEmbeddings()
vectorstore = Chroma(persist_directory='vectorstore', embedding_function=embedding)
retriever = vectorstore.as_retriever()
aisyah_template="""
Answer each question truthfully using the Malaysia's Form 1 History data provided. Your answers should be concise and straight to the point.
For questions that are open-ended, which require subjective judgment or opinion, you may not find a definitive answer in the textbook. 
However, you should still address the question's directive based on the data's context. Ideally, your answer should provide 3 points that support your response. 
You are encouraged to better provide positive suggestions for concepts that are less ethical.
Please keep in mind that the scope of the data provided is limited to the content covered in the Malaysia's Form 1 History textbook.
---------------
{context}"""
##If you don't know the answer, just say that you don't know, don't try to make up an answer.
system_template="""Use the following pieces of context to answer the users question. 
----------------
{context}"""
##If you don't know the answer, just say that you don't know, don't try to make up an answer.
messages = [
    SystemMessagePromptTemplate.from_template(aisyah_template),
    HumanMessagePromptTemplate.from_template("{question}")
]
prompt = ChatPromptTemplate.from_messages(messages)
qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), retriever, return_source_documents=True, qa_prompt=prompt)

def predict(input, chat_historyhistory=[]):
    response = qa({"question":input, "chat_history":history})
    return response, chat_history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    state = gr.State([])

    with gr.Row():
        txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)

    txt.submit(predict, [txt, state], [chatbot, state])
    # txt.submit(agent_executor.run, [txt, state], [chatbot, state])

demo.launch()