Know Your Customer Use Case - Gemini Grounding with Google Search

Source notebook

Repo path: gemini/use-cases/kyc/kyc-with-grounding.ipynb · Open on GitHub · intermediate

Builds grounded Gemini KYC negative-news reports and evaluates response quality with custom Vertex AI metrics.

Summary

The notebook teaches how to configure the Google Gen AI SDK for Vertex AI, prompt Gemini for KYC-style negative-news analysis, and ground responses with Google Search. It defines system instructions, a reusable report-generation function, source extraction from grounding metadata, and a batch workflow over example entities. It then evaluates generated reports with custom pointwise metrics for completeness, accuracy, structure, and professionalism.

Key code patterns

Initialize Gen AI client

from google import genai
 
client = genai.Client(
    vertexai=True,
    project=PROJECT_ID,
    location=LOCATION,
)

Uses Vertex AI-hosted Gemini models through the Google Gen AI SDK.

tools = [
    types.Tool(google_search=types.GoogleSearch()),
]
 
config = types.GenerateContentConfig(
    tools=tools,
    system_instruction=types.Content(parts=[types.Part(text=system_instructions)]),
)

Enables web-grounded responses and applies task-specific system instructions.

Generate KYC report

response = client.models.generate_content(
    model=MODEL_ID,
    contents=[prompt_template.format(input_entity=entity_name)],
    config=generate_content_config,
)

Encapsulates report generation for a person, company, or ship entity.

Extract grounding sources

metadata = response.candidates[0].grounding_metadata
for support in metadata.grounding_supports:
    for chunk_index in support.grounding_chunk_indices:
        chunk = metadata.grounding_chunks[chunk_index]
        sources[chunk_index + 1] = chunk.web.uri

Maps response citations back to grounded web source URLs.

Custom pointwise evaluation

metric = PointwiseMetric(
    metric="completeness_accuracy",
    metric_prompt_template=template,
)
 
eval_task = EvalTask(dataset=eval_dataset, metrics=[metric])
eval_result = eval_task.evaluate()

Uses Vertex AI evaluation with custom criteria and rubrics.

Models & APIs used

  • Models: gemini-2.5-flash
  • APIs / services: Vertex AI, Gemini API, Google Search, Vertex AI Evaluation Framework
  • SDKs / libraries: google-genai, vertexai, pandas, plotly, IPython

When to use this

Use this pattern for KYC or due-diligence workflows that need grounded negative-news summaries with cited sources and evaluation metrics.

Gotchas & caveats

  • Requires Google Cloud authentication with application default credentials or Colab auth.
  • Requires a Google Cloud project and the Vertex AI API enabled.
  • Defaults location to us-central1 unless GOOGLE_CLOUD_REGION is set.
  • Safety thresholds are set to BLOCK_NONE for sensitive negative-news retrieval and need responsible production review.
  • The reference data is generated from a second model run, not human-verified ground truth.
  • The notebook states no persistent Google Cloud resources are created, but API usage still occurs.

Best practices

  • Use explicit system instructions to define role, scope, output format, and no-result behavior.
  • Ground sensitive claims with Google Search and display source metadata for verification.
  • Check for missing candidates, grounding metadata, grounding supports, and out-of-bounds chunks before extracting sources.
  • Separate reusable single-entity generation from batch entity processing.
  • Evaluate outputs with task-specific custom metrics and rubrics.
  • Use human-verified references in real scenarios instead of a second model run.