-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
204 lines (162 loc) · 7.43 KB
/
chatbot.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from langchain.prompts import PromptTemplate
from langchain.text_splitter import CharacterTextSplitter
from langchain.memory import ConversationBufferMemory
from langchain.chains import RetrievalQA
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import Chroma
# =============================================================================
def get_retriever(pdf_file: str
):
"""
Description
----------
Processes a PDF file, generates embeddings for each page,
and stores these embeddings in an in-memory search structure.
Key Parameters
----------
pdf_file : str
The path to the PDF file to be processed.
Returns
----------
retriever : VectorStoreRetriever
A retriever object for querying the embeddings of the PDF pages.
"""
loader = PyPDFLoader(pdf_file)
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vectorstore = Chroma.from_documents(texts, embeddings)
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k":2})
return retriever
# =============================================================================
def get_prompt_template(age: int,
name: str,
use_case: str=None
) -> str:
"""
Description
----------
Generates a prompt template for the chatbot based on the age and name of the user.
Key Parameters
----------
age : int
The age of the user.
name : str
The name of the user.
Returns
----------
prompt_template : str
A string that represents the prompt template for the chatbot.
"""
if use_case == "test":
prompt_template ="""
**You are a teacher for Arabic-speaking children.**
**Instructions:**
As the PDF Reader Expert, your goal is to assist children by only generating 5 multiple choice questions that are appropriate for a %d-year-old child as a JSON regarding the context:
- The kid is %d-year-old, so keep the language simple and easy to understand and their name is "%s".
- Only respond in Arabic and nothing else.
- Return the questions as JSON only which contains the (question, options, correct_answer) and don't use any formatting and the options max number is 4.
**Context:**
{context}
{question}
**Answer:**
""".strip() % (age, age, name)
elif use_case == "explain":
prompt_template ="""
**You are a teacher for Arabic-speaking children.**
**Instructions:**
As the PDF Reader Expert, your goal is to assist children by providing explanations in Arabic from the context of the provided PDF, and nothing outside of it:
- The kid is %d-year-old, so keep the language simple and easy to understand and their name is "%s".
- Be chatty, kind, friendly and patient with the child.
- Only respond in Arabic and nothing else.
- explain the lecture (context), and present the information in an engaging and fun manner suitable for a %d-year-old child.
- The Answer should not exceed 150 words.
- Don't make it a story.
**Context:**
{context}
{question}
**Answer:**
""".strip() % (age, name, age)
else:
prompt_template ="""
**You are a teacher for Arabic-speaking children.**
**Instructions:**
As the PDF Reader Expert, your goal is to assist children by providing explanations in Arabic from the context of the provided PDF, and nothing outside of it:
- The Answer should not exceed 100 words.
- The kid is %d-year-old, so keep the language simpleshort, and easy to understand and their name is "%s".
- Be chatty, kind, friendly and patient with the child.
- Only respond in Arabic and nothing else.
- Don't generate any questions.
- If asked outside the context of the PDF, respond with "الرجاء عدم الخروج عن سياق الدرس"
- If there's no context, forget everything and act as a normal chatbot.
**Context:**
{context}
**Chat History:**:
{history}
**Question:**
{question}
**Answer:**
""".strip() % (age, name)
return prompt_template
# =============================================================================
def generate_response(age: int,
name: str,
user_question: str,
tmp_path: str,
use_case: str=None
):
"""
Description
----------
Generates a response from the chatbot based on the user's question and the content of a PDF file.
Key Parameters
----------
age : int
The age of the user.
name : str
The name of the user.
user_question : str
The question asked by the user.
tmp_path : str
The path to the PDF file to be processed.
Returns
----------
response : str
The response generated by the chatbot.
"""
retriever = get_retriever(tmp_path)
memory = ConversationBufferMemory(memory_key="history", input_key="question")
print(memory.load_memory_variables({}))
if use_case == "test":
prompt_template = get_prompt_template(age, name, use_case=use_case)
prompt = PromptTemplate(template=prompt_template,
input_variables=["context", "question"]
)
user_question = "test"
elif use_case == "explain":
prompt_template = get_prompt_template(age, name, use_case=use_case)
prompt = PromptTemplate(template=prompt_template,
input_variables=["context", "question"]
)
user_question = "explain"
else:
prompt_template = get_prompt_template(age, name, use_case=use_case)
prompt = PromptTemplate(template=prompt_template,
input_variables=["history", "context", "question"]
)
llm = ChatGoogleGenerativeAI(model="gemini-pro",
temperature=0,
maxOutputTokens=1024,
safety_settings=None
)
retrieval_chain = RetrievalQA.from_chain_type(llm,
chain_type='stuff',
retriever=retriever,
chain_type_kwargs={"prompt": prompt,
"memory": memory}
)
response = retrieval_chain.run(user_question)
return response
# =============================================================================