Skip to content

Commit 7ea6bb6

Browse files
committed
[Release] version 0.1.2
What's changed: - Refactored the `chatbot/chatui.py` to consolidate methods into `ask_model()`. - Updated the element names in the `result.html` template. - Cleaned up the code related to reading and replacing custom error messages. - Added more comments describing each prompt in the `ask_model()` method. - Moved the role of `condition.txt` to `config.yaml`. - Updated the Docs Agent module to read custom prompt text from `config.yaml`. - Removed the `condition.txt` file. - Re-generated the `poetry.lock` file to be up to date. October 6, 2023
1 parent bec6573 commit 7ea6bb6

File tree

8 files changed

+926
-824
lines changed

8 files changed

+926
-824
lines changed

demos/palm/python/docs-agent/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ content from the source documents given user questions.
5757
Once the most relevant content is returned, the Docs Agent server uses the prompt structure
5858
shown in Figure 3 to augment the user question with a preset **condition** and a list of
5959
**context**. (When the Docs Agent server starts, the condition value is read from the
60-
[`condition.txt`][condition-txt] file.) Then the Docs Agent server sends this prompt to a
60+
[`config.yaml`][condition-txt] file.) Then the Docs Agent server sends this prompt to a
6161
PaLM 2 model using the PaLM API and receives a response generated by the model.
6262

6363
![Docs Agent prompt strcture](docs/images/docs-agent-prompt-structure-01.png)
@@ -202,16 +202,16 @@ by the PaLM model:
202202
- Additional condition (for fact-checking):
203203

204204
```
205-
Compare the following body of text to the context provided in this prompt and write
206-
a short message that warns the readers about which part of the text below they
207-
should consider fact-checking for themselves? (please keep your response concise and
208-
mention only one important point):
205+
Can you compare the following text to the context provided in this prompt and write
206+
a short message that warns the readers about which part of the text they should
207+
consider fact-checking? (Please keep your response concise and focus on only
208+
one important item.)
209209
```
210210

211211
- Previously generated response
212212

213213
```
214-
<RESPONSE_RETURNED_FROM_THE_PREVIOUS_PROMPT>
214+
Text: <RESPONSE_RETURNED_FROM_THE_PREVIOUS_PROMPT>
215215
```
216216

217217
This "fact-checking" prompt returns a response similar to the following example:
@@ -542,15 +542,7 @@ allowing you to easily bring up and destory the Flask app instance.
542542

543543
To customize settings in the Docs Agent chat app, do the following:
544544

545-
1. (**Optional**) Update the `condition.txt` file to provide a more specific prompt condition
546-
for your custom dataset, for example:
547-
548-
```
549-
You are a helpful chatbot answering questions from developers working on Flutter apps.
550-
Read the following context first and answer the question at the end:
551-
```
552-
553-
2. Edit the `config.yaml` file to update the following field:
545+
1. Edit the `config.yaml` file to update the following field:
554546

555547
```
556548
product_name: "My product"
@@ -563,6 +555,14 @@ To customize settings in the Docs Agent chat app, do the following:
563555
product_name: "Flutter"
564556
```
565557

558+
2. (**Optional**) Edit the `config.yaml` file to provide a more specific prompt
559+
condition for your custom dataset, for example:
560+
561+
```
562+
condition_text: "You are a helpful chatbot answering questions from developers working on
563+
Flutter apps. Read the following context first and answer the question at the end:"
564+
```
565+
566566
### 2. Launch the Docs Agent chat app
567567

568568
To launch the Docs Agent chat app, do the following:
@@ -636,7 +636,7 @@ Meggin Kearney (`@Meggin`), and Kyo Lee (`@kyolee415`).
636636
[set-up-docs-agent]: #set-up-docs-agent
637637
[markdown-to-plain-text]: ./scripts/markdown_to_plain_text.py
638638
[populate-vector-database]: ./scripts/populate_vector_database.py
639-
[condition-txt]: ./condition.txt
639+
[condition-txt]: ./config.yaml
640640
[context-source-01]: http://eventhorizontelescope.org
641641
[fact-check-section]: #using-a-palm-2-model-to-fact-check-its-own-response
642642
[related-questions-section]: #using-a-palm-2-model-to-suggest-related-questions

demos/palm/python/docs-agent/chatbot/chatui.py

Lines changed: 95 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -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"])
116118
def 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"])
189129
def 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.
262225
def log_question(uid, user_question, response):
263226
date_format = "%m/%d/%Y %H:%M:%S %Z"

demos/palm/python/docs-agent/chatbot/templates/chatui/result.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ <h2>PaLM's answer</h2>
88
{{ response_in_html | safe }}
99
</span>
1010
<h4>Important:</h4>
11-
{{ fact_checked_answer | safe }}
11+
{{ fact_checked_response_in_html | safe }}
1212
<p id="fact-check-url">To verify this information, please check out:</p>
13-
{{ fact_link | safe }}
13+
{{ fact_check_url | safe }}
1414
</div>
1515
<div class="related-questions">
1616
<h3>Related questions</h3>
@@ -25,7 +25,7 @@ <h2 class="handle">
2525
{{ context | safe }}
2626
<span class="reference-content">
2727
<h4>Reference:</h4>
28-
{{ metadatas | safe }}
28+
{{ clickable_urls | safe }}
2929
</span>
3030
</div>
3131
</section>
@@ -43,7 +43,7 @@ <h4 id="rewrite-response-header">PaLM's response:</h4>
4343
{{ response_in_html | safe }}
4444
</span>
4545
<h4>Rewrite:</h4>
46-
<textarea id="edit-text-area">{{ response_in_markdown | safe }}</textarea>
46+
<textarea id="edit-text-area">{{ response | safe }}</textarea>
4747
<label for="user-id">User ID:</label>
4848
<input type="text" id="user-id" name="user-id" placeholder="Optional">
4949
<br>

demos/palm/python/docs-agent/condition.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)