-
Notifications
You must be signed in to change notification settings - Fork 188
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
base: genai-for-wsm
Are you sure you want to change the base?
add telemetry and fix broken api calls #163
Conversation
There was a problem hiding this 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 theUSER_AGENT
constant defined inutils/consts.py
(lines 1-15). - Updated
main.py
: Several changes were made togenai-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 theextract_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 theextract_form_data
function.
- Added
- 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
, andgenai-on-vertex-ai/genai-website-mod/backend/utils/vertex_llm_utils.py
andgenai-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!
There was a problem hiding this 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.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<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" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
<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" /> |
<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%" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
<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" /> |
from google.cloud.aiplatform import telemetry | ||
from utils.consts import USER_AGENT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from google.cloud.aiplatform import telemetry | ||
from google.cloud.aiplatform import telemetry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
nox -s format
to format the code.aaie_notebook_template.ipynb
if submitting a new jupyter notbook.