Skip to content

Commit

Permalink
[Release] Docs Agent version 0.1.4 (google#167)
Browse files Browse the repository at this point in the history
What's changed:

- Bug fix: Update `docs_agent.py` to remove a no-longer-in-use error message variable.
- Clean up the prompt format in Docs Agent
  - Context is added to prompts as Markdown
  - Remove extra new lines in context
  - Add extra new lines after the instruction and before the question in prompts.
- Remove the warning message in the Chroma module, which as displayed
  when launching the chatbot.
- Update the embeddings diagrams in the main `README` file.
- Minor updates in the main `README` file.
  • Loading branch information
kyolee415 authored Oct 30, 2023
1 parent 998c2f5 commit e0d216f
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 35 deletions.
32 changes: 17 additions & 15 deletions demos/palm/python/docs-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ The following list summarizes the tasks and features of the Docs Agent sample ap
the responses. (See the
[Enabling users to submit a rewrite of a generated response][submit-a-rewrite] and
[Enabling users to like generated responses][like-generate-responses] sections.)
- **Convert Google Docs, PDF, and Gmail into Markdown files**: This feature uses
Apps Script to convert Google Docs, PDF, and Gmail into Markdown files, which then
can be used as input datasets for Docs Agent. For more information, see the
[`README`][apps-script-readme] file in the `apps_script` directory.

## Flow of events

Expand Down Expand Up @@ -205,10 +209,10 @@ by the PaLM model:
- Additional condition (for fact-checking):

```
Can you compare the following text to the context provided in this prompt and write
a short message that warns the readers about which part of the text they should
consider fact-checking? (Please keep your response concise and focus on only
one important item.)
Can you compare the text below to the context provided
in this prompt above and write a short message that warns the readers about
which part of the text they should consider fact-checking? (Please keep your
response concise and focus on only one important item.)"
```

- Previously generated response
Expand Down Expand Up @@ -597,20 +601,18 @@ To launch the Docs Agent chat app, do the following:

```
$ poetry run ./chatbot/launch.sh
This script starts your flask app in a virtual environment
Installing all dependencies through pip...
Using the local vector database created at /home/alice/generative-ai-docs/demos/palm/python/docs-agent/vector_database
Using embedded DuckDB with persistence: data will be stored in: /home/alice/generative-ai-docs/demos/palm/python/docs-agent/vector_database
Reading the config file: /home/alice/docs-agent/config.yaml
Reading the config file: /home/alice/docs-agent/config.yaml
* Serving Flask app 'chatbot'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://example.com:5000
Press CTRL+C to quit
* Restarting with stat
Using the local vector database created at /home/alice/generative-ai-docs/demos/palm/python/docs-agent/vector_database
Using embedded DuckDB with persistence: data will be stored in: /home/alice/generative-ai-docs/demos/palm/python/docs-agent/vector_database
* Debugger is active!
* Debugger PIN: 129-640-957
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug: * Restarting with stat
Reading the config file: /home/alice/docs-agent/config.yaml
Reading the config file: /home/alice/docs-agent/config.yaml
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 825-594-989
```

Notice the line that shows the URL of this server (`http://example.com:5000` in
Expand Down
12 changes: 7 additions & 5 deletions demos/palm/python/docs-agent/chatbot/chatui.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ def ask_model(question):
# 3. Add the custom condition text to the context.
# 4. Send the prompt (condition + context + question) to the language model.
query_result = docs_agent.query_vector_store(question)
context = markdown.markdown(query_result.fetch_formatted(Format.CONTEXT))
context_with_prefix = docs_agent.add_instruction_to_context(context)
response = docs_agent.ask_text_model_with_context(context_with_prefix, question)
context = query_result.fetch_formatted(Format.CONTEXT)
context_with_instruction = docs_agent.add_instruction_to_context(context)
response = docs_agent.ask_text_model_with_context(context_with_instruction, question)

### PROMPT 2: FACT-CHECK THE PREVIOUS RESPONSE.
fact_checked_response = docs_agent.ask_text_model_to_fact_check(
context_with_prefix, response
context_with_instruction, response
)

### PROMPT 3: GET 5 RELATED QUESTIONS.
Expand All @@ -176,10 +176,12 @@ def ask_model(question):

### PREPARE OTHER ELEMENTS NEEDED BY UI.
# - Create a uuid for this request.
# - Convert the context returned from the database into HTML for rendering.
# - Convert the first response from the model into HTML for rendering.
# - Convert the fact-check response from the model into HTML for rendering.
# - A workaround to get the server's URL to work with the rewrite and like features.
new_uuid = uuid.uuid1()
context_in_html = markdown.markdown(context)
response_in_html = markdown.markdown(response)
fact_checked_response_in_html = markdown.markdown(fact_checked_response)
server_url = request.url_root.replace("http", "https")
Expand All @@ -190,7 +192,7 @@ def ask_model(question):
return render_template(
"chatui/index.html",
question=question,
context=context,
context_in_html=context_in_html,
response=response,
response_in_html=response_in_html,
clickable_urls=clickable_urls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2 class="handle">
<label for="handle1">Context</label>
</h2>
<div class="content">
{{ context | safe }}
{{ context_in_html | safe }}
<span class="reference-content">
<h4>Reference:</h4>
{{ clickable_urls | safe }}
Expand Down
8 changes: 4 additions & 4 deletions demos/palm/python/docs-agent/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def get_collection(self, name, embedding_function=None):
)
elif embedding_model is None or embedding_model == "palm/embedding-gecko-001":
if embedding_model is None:
logging.warning(
"Embedding model is not stored in the metadata of "
"the collection %s. Using PaLM as default.",
logging.info(
"Embedding model is not specified in the metadata of "
"the collection %s. Using the default PaLM embedding model.",
name,
)
palm = PaLM(embed_model="models/embedding-gecko-001", find_models=False)
Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(self, result: QueryResult, index: int) -> None:

def format(self, format_type: Format, ref_index: int = None):
d = {
"document": self.document,
"document": self.document.strip(),
"ref_index": ref_index,
"url": self.metadata.get("url", None),
"distance": self.distance,
Expand Down
8 changes: 4 additions & 4 deletions demos/palm/python/docs-agent/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ input:
condition_text: "You are a helpful chatbot answering questions from users. Read
the following context first and answer the question at the end:"

fact_check_question: "Can you compare the following text to the context provided
in this prompt and write a short message that warns the readers about which part
of the text they should consider fact-checking? (Please keep your response
concise and focus on only one important item.)"
fact_check_question: "Can you compare the text below to the context provided
in this prompt above and write a short message that warns the readers about
which part of the text they should consider fact-checking? (Please keep your
response concise and focus on only one important item.)"

model_error_message: "PaLM is not able to answer this question at the
moment. Rephrase the question and try asking again."
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions demos/palm/python/docs-agent/docs_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self):

# Use this method for talking to PaLM (Text)
def ask_text_model_with_context(self, context, question):
new_prompt = f"{context}\nQuestion: {question}"
new_prompt = f"{context}\n\nQuestion: {question}"
try:
response = palm.generate_text(
prompt=new_prompt,
Expand Down Expand Up @@ -101,7 +101,7 @@ def ask_chat_model_with_context(self, context, question):
return self.model_error_message

if response.last is None:
return self.palm_none_response
return self.model_error_message
return response.last

# Use this method for asking PaLM (Text) for fact-checking
Expand All @@ -117,5 +117,5 @@ def query_vector_store(self, question):
# Add specific instruction as a prefix to the context
def add_instruction_to_context(self, context):
new_context = ""
new_context += self.prompt_condition + "\n" + context
new_context += self.prompt_condition + "\n\n" + context
return new_context
2 changes: 1 addition & 1 deletion demos/palm/python/docs-agent/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "docs-agent"
version = "0.1.3"
version = "0.1.4"
description = ""
authors = ["Docs Agent contributors"]
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions demos/palm/python/docs-agent/scripts/read_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def __init__(self):
with open(INPUT_YAML, "r", encoding="utf-8") as inp_yaml:
self.config_values = yaml.safe_load(inp_yaml)
self.IS_CONFIG_FILE = True
print("Configuration defined in: " + INPUT_YAML)
print("Reading the config file: " + INPUT_YAML)
# Check that the required keys exist
self.validateKeys()
except FileNotFoundError:
print("The file " + INPUT_YAML + " does not exist.")
print("The config file " + INPUT_YAML + " does not exist.")
# Exits the scripts if there is no valid config file
return sys.exit(1)

Expand Down

0 comments on commit e0d216f

Please sign in to comment.