Menjawab Kueri
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menjawab Kueri
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to use the Vertex AI Agent Builder Python API to answer queries, including detailed options for query understanding and answer generation.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for Vertex AI Agent Builder in a local development environment is done via Application Default Credentials, as directed by the provided link.\u003c/p\u003e\n"],["\u003cp\u003eThe code uses the \u003ccode\u003eConversationalSearchServiceClient\u003c/code\u003e to interact with the service, specifying configurations such as serving configuration, query, and optional specifications for query understanding and answer generation.\u003c/p\u003e\n"],["\u003cp\u003eThe code allows customisation of the query through the \u003ccode\u003equery_understanding_spec\u003c/code\u003e, to rephrase the query or classify the query type, allowing further control over how the answer is generated.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eanswer_generation_spec\u003c/code\u003e can be modified, to specify detailed answer customisation, from ignoring adversarial or non-answer seeking query, to including citations, customising the model used, or the output language.\u003c/p\u003e\n"]]],[],null,["# Answer Query\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Get answers and follow-ups](/agentspace/docs/answer)\n- [Get answers and follow-ups](/generative-ai-app-builder/docs/answer)\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.api_core.client_options import ClientOptions\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 # location = \"YOUR_LOCATION\" # Values: \"global\", \"us\", \"eu\"\n # engine_id = \"YOUR_APP_ID\"\n\n\n def answer_query_sample(\n project_id: str,\n location: str,\n engine_id: str,\n ) -\u003e discoveryengine.AnswerQueryResponse:\n # For more information, refer to:\n # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store\n client_options = (\n ClientOptions(api_endpoint=f\"{location}-discoveryengine.googleapis.com\")\n if location != \"global\"\n else None\n )\n\n # Create a client\n client = discoveryengine.ConversationalSearchServiceClient(\n client_options=client_options\n )\n\n # The full resource name of the Search serving config\n serving_config = f\"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config\"\n\n # Optional: Options for query phase\n # The `query_understanding_spec` below includes all available query phase options.\n # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec\n query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(\n query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(\n disable=False, # Optional: Disable query rephraser\n max_rephrase_steps=1, # Optional: Number of rephrase steps\n ),\n # Optional: Classify query types\n query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(\n types=[\n discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,\n discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,\n ] # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both\n ),\n )\n\n # Optional: Options for answer phase\n # The `answer_generation_spec` below includes all available query phase options.\n # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec\n answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(\n ignore_adversarial_query=False, # Optional: Ignore adversarial query\n ignore_non_answer_seeking_query=False, # Optional: Ignore non-answer seeking query\n ignore_low_relevant_content=False, # Optional: Return fallback answer when content is not relevant\n model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(\n model_version=\"gemini-2.0-flash-001/answer_gen/v1\", # Optional: Model to use for answer generation\n ),\n prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(\n preamble=\"Give a detailed answer.\", # Optional: Natural language instructions for customizing the answer.\n ),\n include_citations=True, # Optional: Include citations in the response\n answer_language_code=\"en\", # Optional: Language code of the answer\n )\n\n # Initialize request argument(s)\n request = discoveryengine.AnswerQueryRequest(\n serving_config=serving_config,\n query=discoveryengine.Query(text=\"What is Vertex AI Search?\"),\n session=None, # Optional: include previous session ID to continue a conversation\n query_understanding_spec=query_understanding_spec,\n answer_generation_spec=answer_generation_spec,\n user_pseudo_id=\"user-pseudo-id\", # Optional: Add user pseudo-identifier for queries.\n )\n\n # Make the request\n response = client.answer_query(request)\n\n # Handle the response\n print(response)\n\n return 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)."]]