答覆查詢
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Answer Query
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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)."]]