Intro to Gemini Enterprise

Source notebook

Repo path: search/gemini-enterprise/intro_gemini_enterprise.ipynb · Open on GitHub · intro

Shows how to call Gemini Enterprise search and answer APIs with the Discovery Engine Python SDK.

Summary

This notebook introduces Gemini Enterprise as an AI-powered enterprise search and productivity tool. It installs the Discovery Engine client library, authenticates the notebook environment, configures project, location, and engine IDs, then demonstrates search() and answer_query() calls. The workflow retrieves search summaries, snippets, extractive answers, generated answers, citations, and includes a cleanup example for deleting a search engine.

Key code patterns

Location-aware client

client_options = (
    ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
    if location != "global"
    else None
)
client = discoveryengine.SearchServiceClient(client_options=client_options)

Uses a regional Discovery Engine endpoint only when LOCATION is not global.

Search request with summary config

content_search_spec = discoveryengine.SearchRequest.ContentSearchSpec(
    snippet_spec=discoveryengine.SearchRequest.ContentSearchSpec.SnippetSpec(return_snippet=True),
    summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
        summary_result_count=5,
        include_citations=True,
        model_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec.ModelSpec(version="preview"),
    ),
)
request = discoveryengine.SearchRequest(serving_config=serving_config, query=search_query, page_size=10, content_search_spec=content_search_spec)
response = client.search(request)

Retrieves search results with snippets, summaries, and citations.

Extractive answer parsing

document_dict = MessageToDict(
    response.results[0].document._pb,
    preserving_proto_field_name=True,
)
document_dict["derived_struct_data"]["extractive_answers"][0]["content"]

Converts the returned document proto to a dict to read derived extractive answer content.

Answer query generation

answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
    model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
        model_version="gemini-2.0-flash/answer_gen/v2"
    ),
    prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(preamble="Give a detailed answer."),
    include_citations=True,
    answer_language_code="en",
)
response = client.answer_query(request)

Configures generated answers with a specific model version, prompt preamble, citations, and language.

Models & APIs used

  • Models: gemini-2.0-flash/answer_gen/v2
  • APIs / services: Gemini Enterprise, Discovery Engine Search, Discovery Engine AnswerQuery
  • SDKs / libraries: google-cloud-discoveryengine

When to use this

Use this pattern when building enterprise search or conversational answer experiences over a pre-created Gemini Enterprise app.

Gotchas & caveats

  • The notebook requires installing google-cloud-discoveryengine and restarting the runtime.
  • Colab users must run google.colab auth.authenticate_user().
  • PROJECT_ID, ENGINE_ID, and data_store_id are placeholders that must be replaced or provided by GOOGLE_CLOUD_PROJECT.
  • A Gemini Enterprise datastore and engine must be created before running the search and answer calls.
  • Non-global locations require a location-specific Discovery Engine API endpoint.
  • The notebook notes search configuration options are only supported for unstructured data.

Best practices

  • Fallback to the GOOGLE_CLOUD_PROJECT environment variable when PROJECT_ID is not supplied.
  • Use ClientOptions to select a regional Discovery Engine endpoint for non-global locations.
  • Include citations in both search summaries and generated answers.
  • Use query expansion and spell correction in the search request.
  • Use query rephrasing and query classification in the answer request.
  • Call search and answer separately when you want to show search results quickly while answers are still generating.
  • Include a cleanup path to delete the search engine after testing.