Add code samples for Agent Search - #14591
Conversation
Adding code samples for Agent Search
|
Here is the summary of changes. You are about to add 9 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces several Python code samples and corresponding unit tests for Google Cloud Discovery Engine (Agent Search), covering features like multi-turn conversational search, structured document ingestion, Gemini layout parsing, custom chunk imports, robust LRO polling, and user event recording. The review feedback highlights several improvement opportunities: avoiding the hasattr anti-pattern on Protobuf messages in converse_conversation_sample.py, adding defensive None checks to prevent potential AttributeErrors in create_document_metadata_sample.py and search_extractive_segments_sample.py, and using the idiomatic GetCurrentTime() method for Protobuf timestamps in write_user_event_sample.py.
| if ( | ||
| hasattr(response.reply, "summary") | ||
| and response.reply.summary | ||
| and response.reply.summary.summary_text | ||
| ): | ||
| reply_text = response.reply.summary.summary_text | ||
| elif hasattr(response.reply, "reply") and response.reply.reply: | ||
| reply_text = response.reply.reply |
There was a problem hiding this comment.
Using hasattr to check for the presence of fields on Protobuf or proto-plus messages is an anti-pattern because these attributes are always defined on the class descriptor, meaning hasattr will always return True regardless of whether the field is set. Instead, directly check the truthiness of the fields, which is more idiomatic and robust.
if response.reply.summary and response.reply.summary.summary_text:
reply_text = response.reply.summary.summary_text
elif response.reply.reply:
reply_text = response.reply.reply| current_time = time.time() | ||
| event_time = timestamp_pb2.Timestamp( | ||
| seconds=int(current_time), | ||
| nanos=int((current_time - int(current_time)) * 1e9), | ||
| ) |
There was a problem hiding this comment.
Instead of manually calculating seconds and nanoseconds from time.time(), you can use the built-in GetCurrentTime() method on the Protobuf Timestamp object. This simplifies the code and makes it more idiomatic.
# Set accurate UTC event timestamp
event_time = timestamp_pb2.Timestamp()
event_time.GetCurrentTime()|
@sailobo Be sure to register your GitHub account with Google internally. Also address the lint isses https://github.com/GoogleCloudPlatform/python-docs-samples/actions/runs/34269115170/job/102206281775?pr=14591 and the Gemini Code Assist comments |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@holtskinner I have applied the batched suggestions from the Gemini Code Assist bot to address the review feedback and lint issues. Also, I have double-checked, and my GitHub account is successfully registered internally (I will need to follow up in aligning ldap as per a previous registration error ) Could you please approve the workflow runs so the checks can proceed against these new commits? |
Description
Adds 9 supportability-driven Python code samples for Vertex AI Search (Agent Search / Discovery Engine) addressing high-frequency customer implementation patterns:
converse_conversation_sample.py(genappbuilder_converse_conversation): Multi-turn conversational search usingConversationalSearchServiceClient(session initialization and continuation).create_document_metadata_sample.py(genappbuilder_create_document_metadata): Structured document ingestion passing dictionary metadata directly intoDocument(struct_data=...).enable_gemini_layout_parser_sample.py(genappbuilder_enable_gemini_layout_parser): Data store creation with Gemini LLM layout parsing, table/image annotations, and chunking config.update_data_store_gemini_parser_sample.py(genappbuilder_update_data_store_gemini_parser): Dedicated sample to update an existing data store to enable Gemini layout parser withupdate_mask.import_custom_chunks_sample.py(genappbuilder_import_custom_chunks): Bring-Your-Own-Chunks (BYOC) document ingestion with inlined BYOC JSON schema explanation.poll_lro_robust_sample.py(genappbuilder_poll_lro_robust): Exponential backoff polling for long-running operations (Operation.metadataand error unpacking).search_chunks_sample.py(genappbuilder_search_chunks): Chunk-mode search query returning chunk content, parent document metadata, and adjacent previous/next snippets.search_extractive_segments_sample.py(genappbuilder_search_extractive_segments): Multi-data store search with extractive segments and relevance confidence scores.write_user_event_sample.py(genappbuilder_write_user_event): User event ingestion with search attribution tokens for search quality feedback loops.Includes comprehensive unit tests in
discoveryengine/supportability_samples_test.py.Reviewers: @holtskinner, @janeday
Internal Reference: cl/972770246
Checklist
Testing
Compliance & Style
Post-Approval Actions