Rank documents
Stay organized with collections
Save and categorize content based on your preferences.
Rank documents
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 page provides information and a code sample on how to rank documents using the Vertex AI Agent Builder.\u003c/p\u003e\n"],["\u003cp\u003eThe code sample demonstrates ranking documents using the \u003ccode\u003eRankServiceClient\u003c/code\u003e from the \u003ccode\u003egoogle.cloud.discoveryengine_v1\u003c/code\u003e library.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Vertex AI Agent Builder requires setting up Application Default Credentials, detailed instructions are available in the provided link.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python code allows for ranking documents based on a query using a specified model such as \u003ccode\u003esemantic-ranker-512@latest\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe user can view further information on the topic of ranking and reranking documents with RAG by following a link to detailed documentation.\u003c/p\u003e\n"]]],[],null,["# Rank documents\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Improve search and RAG quality with ranking API](/generative-ai-app-builder/docs/ranking)\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.RankServiceClient()\n\n # The full resource name of the ranking config.\n # Format: projects/{project_id}/locations/{location}/rankingConfigs/default_ranking_config\n ranking_config = client.ranking_config_path(\n project=project_id,\n location=\"global\",\n ranking_config=\"default_ranking_config\",\n )\n request = discoveryengine.RankRequest(\n ranking_config=ranking_config,\n model=\"semantic-ranker-default@latest\",\n top_n=10,\n query=\"What is Google Gemini?\",\n records=[\n discoveryengine.RankingRecord(\n id=\"1\",\n title=\"Gemini\",\n content=\"The Gemini zodiac symbol often depicts two figures standing side-by-side.\",\n ),\n discoveryengine.RankingRecord(\n id=\"2\",\n title=\"Gemini\",\n content=\"Gemini is a cutting edge large language model created by Google.\",\n ),\n discoveryengine.RankingRecord(\n id=\"3\",\n title=\"Gemini Constellation\",\n content=\"Gemini is a constellation that can be seen in the night sky.\",\n ),\n ],\n )\n\n response = client.rank(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)."]]