Check grounding
Stay organized with collections
Save and categorize content based on your preferences.
Check grounding
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to check grounding using the Vertex AI Agent Builder Python API, verifying the accuracy of generated answers against provided facts.\u003c/p\u003e\n"],["\u003cp\u003eThe grounding check process involves setting up a \u003ccode\u003eGroundedGenerationServiceClient\u003c/code\u003e and specifying a grounding configuration, an answer candidate, and relevant facts.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Vertex AI Agent Builder requires setting up Application Default Credentials, as detailed in the linked documentation for local development environments.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example sends a \u003ccode\u003eCheckGroundingRequest\u003c/code\u003e with sample data and then prints the server's \u003ccode\u003eresponse\u003c/code\u003e when complete.\u003c/p\u003e\n"]]],[],null,["# Check grounding\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Check grounding with RAG](/generative-ai-app-builder/docs/check-grounding)\n\nCode sample\n-----------\n\n### Python\n\n\nFor more information, see the\n[AI Applications Python API\nreference documentation](/python/docs/reference/discoveryengine/latest).\n\n\nTo authenticate to AI Applications, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import discoveryengine_v1 as discoveryengine\n\n # TODO(developer): Uncomment these variables before running the sample.\n # project_id = \"YOUR_PROJECT_ID\"\n\n client = discoveryengine.GroundedGenerationServiceClient()\n\n # The full resource name of the grounding config.\n # Format: projects/{project_id}/locations/{location}/groundingConfigs/default_grounding_config\n grounding_config = client.grounding_config_path(\n project=project_id,\n location=\"global\",\n grounding_config=\"default_grounding_config\",\n )\n\n request = discoveryengine.CheckGroundingRequest(\n grounding_config=grounding_config,\n answer_candidate=\"Titanic was directed by James Cameron. It was released in 1997.\",\n facts=[\n discoveryengine.GroundingFact(\n fact_text=(\n \"Titanic is a 1997 American epic romantic disaster movie. It was directed, written,\"\n \" and co-produced by James Cameron. The movie is about the 1912 sinking of the\"\n \" RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released\"\n \" on December 19, 1997. It received positive critical reviews. The movie won 11 Academy\"\n \" Awards, and was nominated for fourteen total Academy Awards.\"\n ),\n attributes={\"author\": \"Simple Wikipedia\"},\n ),\n discoveryengine.GroundingFact(\n fact_text=(\n 'James Cameron\\'s \"Titanic\" is an epic, action-packed romance'\n \"set against the ill-fated maiden voyage of the R.M.S. Titanic;\"\n \"the pride and joy of the White Star Line and, at the time,\"\n \"the largest moving object ever built. \"\n 'She was the most luxurious liner of her era -- the \"ship of dreams\" -- '\n \"which ultimately carried over 1,500 people to their death in the \"\n \"ice cold waters of the North Atlantic in the early hours of April 15, 1912.\"\n ),\n attributes={\"author\": \"Simple Wikipedia\"},\n ),\n ],\n grounding_spec=discoveryengine.CheckGroundingSpec(citation_threshold=0.6),\n )\n\n response = client.check_grounding(request=request)\n\n # Handle the response\n print(response)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=genappbuilder)."]]