搜索数据存储区并跟进
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对数据存储区执行多轮/对话式搜索。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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 perform a multi-turn or conversational search on a specified data store using the Vertex AI Agent Builder.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves setting up Application Default Credentials for authentication and utilizing the Vertex AI Agent Builder Python API.\u003c/p\u003e\n"],["\u003cp\u003eThe code initializes a multi-turn session, adds new messages (search queries), and retrieves responses that include summaries and search results.\u003c/p\u003e\n"],["\u003cp\u003eThe code uses \u003ccode\u003ediscoveryengine.ConversationalSearchServiceClient\u003c/code\u003e to create conversations, add queries, and print the response, including summaries, links, and snippets.\u003c/p\u003e\n"],["\u003cp\u003eThis search functionality can be tested further by using the google cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Search a data store with follow-ups\n\nPerform a multi-turn/conversational search on a data store.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Search with follow-ups](/generative-ai-app-builder/docs/multi-turn-search)\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 typing import List\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 # data_store_id = \"YOUR_DATA_STORE_ID\"\n # search_queries = [\"YOUR_FIRST_SEARCH_QUERY\", \"YOUR_SECOND_SEARCH_QUERY\"]\n\n\n def multi_turn_search_sample(\n project_id: str,\n location: str,\n data_store_id: str,\n search_queries: List[str],\n ) -\u003e List[discoveryengine.ConverseConversationResponse]:\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 # Initialize Multi-Turn Session\n conversation = client.create_conversation(\n # The full resource name of the data store\n # e.g. projects/{project_id}/locations/{location}/dataStores/{data_store_id}\n parent=client.data_store_path(\n project=project_id, location=location, data_store=data_store_id\n ),\n conversation=discoveryengine.Conversation(),\n )\n\n\n for search_query in search_queries:\n # Add new message to session\n request = discoveryengine.ConverseConversationRequest(\n name=conversation.name,\n query=discoveryengine.TextInput(input=search_query),\n serving_config=client.serving_config_path(\n project=project_id,\n location=location,\n data_store=data_store_id,\n serving_config=\"default_config\",\n ),\n # Options for the returned summary\n summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(\n # Number of results to include in summary\n summary_result_count=3,\n include_citations=True,\n ),\n )\n response = client.converse_conversation(request)\n\n print(f\"Reply: {response.reply.summary.summary_text}\\n\")\n\n for i, result in enumerate(response.search_results, 1):\n result_data = result.document.derived_struct_data\n print(f\"[{i}]\")\n print(f\"Link: {result_data['link']}\")\n print(f\"First Snippet: {result_data['snippets'][0]['snippet']}\")\n print(\n \"First Extractive Answer: \\n\"\n f\"\\tPage: {result_data['extractive_answers'][0]['pageNumber']}\\n\"\n f\"\\tContent: {result_data['extractive_answers'][0]['content']}\\n\\n\"\n )\n print(\"\\n\\n\")\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)."]]