diff --git a/demos/palm/python/docs-agent/README.md b/demos/palm/python/docs-agent/README.md index 3ec9cfd38..526708391 100644 --- a/demos/palm/python/docs-agent/README.md +++ b/demos/palm/python/docs-agent/README.md @@ -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 @@ -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 @@ -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 diff --git a/demos/palm/python/docs-agent/chatbot/chatui.py b/demos/palm/python/docs-agent/chatbot/chatui.py index 4b6c64bcf..d6982cb7f 100644 --- a/demos/palm/python/docs-agent/chatbot/chatui.py +++ b/demos/palm/python/docs-agent/chatbot/chatui.py @@ -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. @@ -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") @@ -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, diff --git a/demos/palm/python/docs-agent/chatbot/templates/chatui/result.html b/demos/palm/python/docs-agent/chatbot/templates/chatui/result.html index d02d95425..60d1a30c3 100644 --- a/demos/palm/python/docs-agent/chatbot/templates/chatui/result.html +++ b/demos/palm/python/docs-agent/chatbot/templates/chatui/result.html @@ -22,7 +22,7 @@

- {{ context | safe }} + {{ context_in_html | safe }}

Reference:

{{ clickable_urls | safe }} diff --git a/demos/palm/python/docs-agent/chroma.py b/demos/palm/python/docs-agent/chroma.py index 403ce462b..c6d3a46a4 100644 --- a/demos/palm/python/docs-agent/chroma.py +++ b/demos/palm/python/docs-agent/chroma.py @@ -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) @@ -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, diff --git a/demos/palm/python/docs-agent/config.yaml b/demos/palm/python/docs-agent/config.yaml index 21f2e78bc..44e5fb7e0 100644 --- a/demos/palm/python/docs-agent/config.yaml +++ b/demos/palm/python/docs-agent/config.yaml @@ -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." diff --git a/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-01.png b/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-01.png index 79df22fb6..32ac4dc5a 100644 Binary files a/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-01.png and b/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-01.png differ diff --git a/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-02.png b/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-02.png index 6be46d0af..7ff222efd 100644 Binary files a/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-02.png and b/demos/palm/python/docs-agent/docs/images/docs-agent-embeddings-02.png differ diff --git a/demos/palm/python/docs-agent/docs_agent.py b/demos/palm/python/docs-agent/docs_agent.py index 1fe383d38..7f5f7b155 100644 --- a/demos/palm/python/docs-agent/docs_agent.py +++ b/demos/palm/python/docs-agent/docs_agent.py @@ -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, @@ -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 @@ -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 diff --git a/demos/palm/python/docs-agent/pyproject.toml b/demos/palm/python/docs-agent/pyproject.toml index 8d3f74600..647e88018 100644 --- a/demos/palm/python/docs-agent/pyproject.toml +++ b/demos/palm/python/docs-agent/pyproject.toml @@ -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" diff --git a/demos/palm/python/docs-agent/scripts/read_config.py b/demos/palm/python/docs-agent/scripts/read_config.py index 852ab65e2..5e2c89956 100644 --- a/demos/palm/python/docs-agent/scripts/read_config.py +++ b/demos/palm/python/docs-agent/scripts/read_config.py @@ -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)