@@ -112,152 +112,115 @@ def rewrite():
112112 return redirect (url_for ("chatui.index" ))
113113
114114
115+ # Render a response page when the user asks a question
116+ # using input text box.
115117@bp .route ("/result" , methods = ["GET" , "POST" ])
116118def result ():
117119 if request .method == "POST" :
118- uuid_value = uuid .uuid1 ()
119- question_captured = request .form ["question" ]
120- query_result = docs_agent .query_vector_store (question_captured )
121- context = markdown .markdown (query_result .fetch_formatted (Format .CONTEXT ))
122- context_with_prefix = docs_agent .add_instruction_to_context (context )
123- response_in_markdown = docs_agent .ask_text_model_with_context (
124- context_with_prefix , question_captured
125- )
126- if response_in_markdown is None :
127- response_in_markdown = (
128- "The PaLM API is not able to answer this question at the moment. "
129- "Try to rephrase the question and ask again."
130- )
131- response_in_html = markdown .markdown (response_in_markdown )
132- metadatas = markdown .markdown (
133- query_result .fetch_formatted (Format .CLICKABLE_URL )
134- )
135- fact_checked_answer_in_markdown = docs_agent .ask_text_model_to_fact_check (
136- context_with_prefix , response_in_markdown
137- )
138- if fact_checked_answer_in_markdown is None :
139- fact_checked_answer_in_markdown = (
140- "The PaLM API is not able to answer this question at the moment. "
141- "Try to rephrase the question and ask again."
142- )
143- fact_checked_answer_in_html = markdown .markdown (fact_checked_answer_in_markdown )
144- new_question = (
145- "What are 5 questions developers might ask after reading the context?"
146- )
147- related_questions = markdown .markdown (
148- docs_agent .ask_text_model_with_context (response_in_markdown , new_question )
149- )
150- soup = BeautifulSoup (related_questions , "html.parser" )
151- for item in soup .find_all ("li" ):
152- if item .string is not None :
153- link = soup .new_tag (
154- "a" ,
155- href = url_for (
156- "chatui.question" , ask = urllib .parse .quote_plus (item .string )
157- ),
158- )
159- link .string = item .string
160- item .string = ""
161- item .append (link )
162- related_questions = soup
163- fact_link = markdown .markdown (
164- query_result .fetch_nearest_formatted (Format .CLICKABLE_URL )
165- )
166- server_url = request .url_root .replace ("http" , "https" )
167- # Log the question and response to the log file.
168- log_question (uuid_value , question_captured , response_in_markdown )
169- return render_template (
170- "chatui/index.html" ,
171- question = question_captured ,
172- context = context ,
173- context_with_prefix = context_with_prefix ,
174- response_in_markdown = response_in_markdown ,
175- response_in_html = response_in_html ,
176- product = product ,
177- metadatas = metadatas ,
178- fact_checked_answer = fact_checked_answer_in_html ,
179- fact_link = fact_link ,
180- related_questions = related_questions ,
181- server_url = server_url ,
182- uuid = uuid_value ,
183- )
120+ question = request .form ["question" ]
121+ return ask_model (question )
184122 else :
185123 return redirect (url_for ("chatui.index" ))
186124
187125
126+ # Render a response page when the user clicks a question
127+ # from the related questions list.
188128@bp .route ("/question/<ask>" , methods = ["GET" , "POST" ])
189129def question (ask ):
190130 if request .method == "GET" :
191- uuid_value = uuid .uuid1 ()
192- question_captured = urllib .parse .unquote_plus (ask )
193- query_result = docs_agent .query_vector_store (question_captured )
194- context = markdown .markdown (query_result .fetch_formatted (Format .CONTEXT ))
195- context_with_prefix = docs_agent .add_instruction_to_context (context )
196- response_in_markdown = docs_agent .ask_text_model_with_context (
197- context_with_prefix , question_captured
198- )
199- if response_in_markdown is None :
200- response_in_markdown = (
201- "The PaLM API is not able to answer this question at the moment. "
202- "Try to rephrase the question and ask again."
203- )
204- response_in_html = markdown .markdown (response_in_markdown )
205- metadatas = markdown .markdown (
206- query_result .fetch_formatted (Format .CLICKABLE_URL )
207- )
208- fact_checked_answer_in_markdown = docs_agent .ask_text_model_to_fact_check (
209- context_with_prefix , response_in_markdown
210- )
211- if fact_checked_answer_in_markdown is None :
212- fact_checked_answer_in_markdown = (
213- "The PaLM API is not able to answer this question at the moment. "
214- "Try to rephrase the question and ask again."
215- )
216- fact_checked_answer_in_html = markdown .markdown (fact_checked_answer_in_markdown )
217- new_question = (
218- "What are 5 questions developers might ask after reading the context?"
219- )
220- related_questions = markdown .markdown (
221- docs_agent .ask_text_model_with_context (response_in_markdown , new_question )
222- )
223- soup = BeautifulSoup (related_questions , "html.parser" )
224- for item in soup .find_all ("li" ):
225- if item .string is not None :
226- link = soup .new_tag (
227- "a" ,
228- href = url_for (
229- "chatui.question" , ask = urllib .parse .quote_plus (item .string )
230- ),
231- )
232- link .string = item .string
233- item .string = ""
234- item .append (link )
235- related_questions = soup
236- fact_link = markdown .markdown (
237- query_result .fetch_nearest_formatted (Format .CLICKABLE_URL )
238- )
239- server_url = request .url_root .replace ("http" , "https" )
240- # Log the question and response to the log file.
241- log_question (uuid_value , question_captured , response_in_markdown )
242- return render_template (
243- "chatui/index.html" ,
244- question = question_captured ,
245- context = context ,
246- context_with_prefix = context_with_prefix ,
247- response_in_markdown = response_in_markdown ,
248- response_in_html = response_in_html ,
249- product = product ,
250- metadatas = metadatas ,
251- fact_checked_answer = fact_checked_answer_in_html ,
252- fact_link = fact_link ,
253- related_questions = related_questions ,
254- server_url = server_url ,
255- uuid = uuid_value ,
256- )
131+ question = urllib .parse .unquote_plus (ask )
132+ return ask_model (question )
257133 else :
258134 return redirect (url_for ("chatui.index" ))
259135
260136
137+ # Construct a set of prompts using the user question, send the prompts to
138+ # the lanaguage model, receive responses, and present them into a page.
139+ def ask_model (question ):
140+ ### PROMPT 1: AUGMENT THE USER QUESTION WITH CONTEXT.
141+ # 1. Use the question to retrieve a list of related contents from the database.
142+ # 2. Convert the list of related contents into plain Markdown text (context).
143+ # 3. Add the custom condition text to the context.
144+ # 4. Send the prompt (condition + context + question) to the language model.
145+ query_result = docs_agent .query_vector_store (question )
146+ context = markdown .markdown (query_result .fetch_formatted (Format .CONTEXT ))
147+ context_with_prefix = docs_agent .add_instruction_to_context (context )
148+ response = docs_agent .ask_text_model_with_context (context_with_prefix , question )
149+
150+ ### PROMPT 2: FACT-CHECK THE PREVIOUS RESPONSE.
151+ fact_checked_response = docs_agent .ask_text_model_to_fact_check (
152+ context_with_prefix , response
153+ )
154+
155+ ### PROMPT 3: GET 5 RELATED QUESTIONS.
156+ # 1. Prepare a new question asking the model to come up with 5 related questions.
157+ # 2. Ask the language model with the new question.
158+ # 3. Parse the model's response into a list in HTML format.
159+ new_question = (
160+ "What are 5 questions developers might ask after reading the context?"
161+ )
162+ new_response = markdown .markdown (
163+ docs_agent .ask_text_model_with_context (response , new_question )
164+ )
165+ related_questions = parse_related_questions_response_to_html_list (new_response )
166+
167+ ### RETRIEVE SOURCE URLS.
168+ # - Construct clickable URLs using the returned related contents above.
169+ # - Extract the URL of the top related content for the fact-check message.
170+ clickable_urls = markdown .markdown (
171+ query_result .fetch_formatted (Format .CLICKABLE_URL )
172+ )
173+ fact_check_url = markdown .markdown (
174+ query_result .fetch_nearest_formatted (Format .CLICKABLE_URL )
175+ )
176+
177+ ### PREPARE OTHER ELEMENTS NEEDED BY UI.
178+ # - Create a uuid for this request.
179+ # - Convert the first response from the model into HTML for rendering.
180+ # - Convert the fact-check response from the model into HTML for rendering.
181+ # - A workaround to get the server's URL to work with the rewrite and like features.
182+ new_uuid = uuid .uuid1 ()
183+ response_in_html = markdown .markdown (response )
184+ fact_checked_response_in_html = markdown .markdown (fact_checked_response )
185+ server_url = request .url_root .replace ("http" , "https" )
186+
187+ ### LOG THIS REQUEST.
188+ log_question (new_uuid , question , response )
189+
190+ return render_template (
191+ "chatui/index.html" ,
192+ question = question ,
193+ context = context ,
194+ response = response ,
195+ response_in_html = response_in_html ,
196+ clickable_urls = clickable_urls ,
197+ fact_checked_response_in_html = fact_checked_response_in_html ,
198+ fact_check_url = fact_check_url ,
199+ related_questions = related_questions ,
200+ product = product ,
201+ server_url = server_url ,
202+ uuid = new_uuid ,
203+ )
204+
205+
206+ # Parse a response containing a list of related questions from the language model
207+ # and convert it into an HTML-based list.
208+ def parse_related_questions_response_to_html_list (response ):
209+ soup = BeautifulSoup (response , "html.parser" )
210+ for item in soup .find_all ("li" ):
211+ if item .string is not None :
212+ link = soup .new_tag (
213+ "a" ,
214+ href = url_for (
215+ "chatui.question" , ask = urllib .parse .quote_plus (item .string )
216+ ),
217+ )
218+ link .string = item .string
219+ item .string = ""
220+ item .append (link )
221+ return soup
222+
223+
261224# Log the question and response to the server's log file.
262225def log_question (uid , user_question , response ):
263226 date_format = "%m/%d/%Y %H:%M:%S %Z"
0 commit comments