Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add telemetry and fix broken api calls #163

Open
wants to merge 5 commits into
base: genai-for-wsm
Choose a base branch
from

Conversation

yudyhendry
Copy link

Pull-Request Template

Thank you for your contribution! Please provide a brief description of your changes and ensure you've completed the checklist below.

Description

What does this PR do? Why is it necessary?

Fixes # (if applicable)

Checklist

  • Contribution Guidelines: I have read the Contribution Guidelines.
  • CLA: I have signed the CLA.
  • Authorship: I am listed as the author (if applicable).
  • Conventional Commits: My PR title and commit messages follow the Conventional Commits spec.
  • Code Format: I have run nox -s format to format the code.
  • Spelling: I have fixed any spelling errors, and added false positives to .github/actions/spelling/allow.txt if necessary.
  • Template: I have followed the aaie_notebook_template.ipynb if submitting a new jupyter notbook.
  • Sync: My Fork is synced with the upstream.
  • Documentations: I have updated relevant documentations (if applicable) in the docs folder.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi team,

I'm currently reviewing this pull request and will provide feedback in a few minutes. In the meantime, I'm giving you a summary of the pull request's content so that you and other reviewers can quickly get up to speed on the changes and their intent.

This pull request, authored by yudyhendry, aims to add telemetry and fix broken API calls in the applied-ai-engineering-samples repository. The changes include:

  • Improved Error Handling: The code now includes more robust error handling in various API calls, preventing unexpected crashes and improving the user experience.
  • Added Telemetry: Telemetry has been added to several key functions, allowing for better monitoring and performance analysis of the application. This is implemented using the google.cloud.aiplatform.telemetry library and the USER_AGENT constant defined in utils/consts.py (lines 1-15).
  • Updated main.py: Several changes were made to genai-on-vertex-ai/genai-website-mod/backend/main.py:
    • Added Optional import to line 16.
    • Added urllib.parse import to line 38.
    • Removed extra whitespace on lines 88-91.
    • Added extract_form_data function (lines 132-141) to handle form data extraction.
    • Removed hx-trigger attribute from line 183 in the HTML response.
    • Modified the /web/magi endpoint (lines 288-307) to use the extract_form_data function.
    • Added a new endpoint /web/magi-follow (lines 311-328) for follow-up searches.
    • Modified the /web/magi-video endpoint (lines 331-340) to use the extract_form_data function.
  • Updated other files: Changes were also made to genai-on-vertex-ai/genai-website-mod/backend/routers/vertex_llm_video.py, genai-on-vertex-ai/genai-website-mod/backend/routers/vertex_search.py, genai-on-vertex-ai/genai-website-mod/backend/static/html/header.html, genai-on-vertex-ai/genai-website-mod/backend/templates/home.html, genai-on-vertex-ai/genai-website-mod/backend/templates/magi-video.html, genai-on-vertex-ai/genai-website-mod/backend/templates/magi.html, genai-on-vertex-ai/genai-website-mod/backend/utils/consts.py, genai-on-vertex-ai/genai-website-mod/backend/utils/content_utils.py, genai-on-vertex-ai/genai-website-mod/backend/utils/imagen.py, and genai-on-vertex-ai/genai-website-mod/backend/utils/vertex_llm_utils.py and genai-on-vertex-ai/genai-website-mod/backend/utils/vertex_search_utils.py to add telemetry and improve functionality. Specific changes within these files are detailed in the diff.
  • UI Improvements: The UI has been slightly simplified by removing redundant elements, resulting in a cleaner and more user-friendly interface.

I'll provide a more detailed review shortly.

Meanwhile, here's a little poem for your enjoyment:

Code flows like a river,
Telemetry, a guiding star,
Bugs flee in the night.

Thanks!

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request adds telemetry and fixes some broken API calls. The changes are generally well-structured and address the described issues. The addition of telemetry using a context manager is a good practice. A few minor improvements can enhance the code's readability and maintainability.

Comment on lines +132 to +141
def extract_form_data(form:str) -> dict:
kvs = {}
for kv in form.split("&"):
key = kv.split("=")[0]
k = parse.unquote(key, encoding="utf-8", errors="replace")
value = kv.split("=")[1]
v = parse.unquote(value, encoding="utf-8", errors="replace")
if value != "":
kvs[k] = v
return kvs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The extract_form_data function can be simplified using urllib.parse.parse_qs. This built-in function directly handles the parsing of URL-encoded forms, making the code more concise and efficient.

Suggested change
def extract_form_data(form:str) -> dict:
kvs = {}
for kv in form.split("&"):
key = kv.split("=")[0]
k = parse.unquote(key, encoding="utf-8", errors="replace")
value = kv.split("=")[1]
v = parse.unquote(value, encoding="utf-8", errors="replace")
if value != "":
kvs[k] = v
return kvs
def extract_form_data(form: str) -> dict:
return parse.parse_qs(form)

form=query.decode(encoding="utf-8")
)

print("Entering /web/magi: %s", form_data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

While print statements are helpful for debugging, it's generally better to use a logging framework for production code. This allows for more structured logging and easier management of log levels.

logging.debug(f"Entering /web/magi: {form_data}")

Comment on lines +13 to +16
<input style="border: 1px solid rgb(228, 204, 204); height: 40px; width: 510px;" type="text"
id="search-bar-query-video" name="video_query" title="Search" type="search" placeholder="Ask Video ..."
hx-post="/web/magi-video" hx-indicator="#loading-initial-video" hx-include="#search-bar-query-video"
hx-target="#magi-container-target2" aria-label="Search" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To maintain consistency with the search functionality in the header, consider adding hx-trigger="submit" to the video search input. This ensures that the search is triggered upon form submission, providing a unified user experience.

Suggested change
<input style="border: 1px solid rgb(228, 204, 204); height: 40px; width: 510px;" type="text"
id="search-bar-query-video" name="video_query" title="Search" type="search" placeholder="Ask Video ..."
hx-post="/web/magi-video" hx-indicator="#loading-initial-video" hx-include="#search-bar-query-video"
hx-target="#magi-container-target2" aria-label="Search" />
<input style="border: 1px solid rgb(228, 204, 204); height: 40px; width: 510px;" type="text"
id="search-bar-query-video" name="video_query" title="Search" type="search" placeholder="Ask Video ..."
hx-post="/web/magi-video" hx-indicator="#loading-initial-video" hx-include="#search-bar-query-video"
hx-target="#magi-container-target2" aria-label="Search" hx-trigger="submit" />

Comment on lines +29 to 32
<input type="text" id="prompt-follow-up" name="video_query" title="Search" type="search" value=""
placeholder="Ask ..." aria-label="Search" hx-post="/web/magi-video"
hx-indicator="#loading-initial-video" hx-include="#prompt-follow-up"
hx-target="#magi-container-target2" style="width: 100%" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other search inputs and to improve user experience, add hx-trigger="submit" to this input field. This will trigger the search when the user submits the form, aligning the behavior with the main search bar.

Suggested change
<input type="text" id="prompt-follow-up" name="video_query" title="Search" type="search" value=""
placeholder="Ask ..." aria-label="Search" hx-post="/web/magi-video"
hx-indicator="#loading-initial-video" hx-include="#prompt-follow-up"
hx-target="#magi-container-target2" style="width: 100%" />
<input type="text" id="prompt-follow-up" name="video_query" title="Search" type="search" value=""
placeholder="Ask ..." aria-label="Search" hx-post="/web/magi-video"
hx-indicator="#loading-initial-video" hx-include="#prompt-follow-up"
hx-target="#magi-container-target2" style="width: 100%" hx-trigger="submit" />

Comment on lines +37 to +38
from google.cloud.aiplatform import telemetry
from utils.consts import USER_AGENT
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

There are duplicate imports of telemetry and USER_AGENT. Remove lines 37 and 38 to clean up the imports.

Comment on lines +18 to +19
from google.cloud.aiplatform import telemetry
from google.cloud.aiplatform import telemetry
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Duplicate import for telemetry, remove one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants