You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中实现Google Gen AI多轮搜索API?无相关实现文档

实现Google Discovery Engine多轮上下文搜索(Python)

要实现保留历史查询上下文的多轮搜索,核心是在SearchRequest中传入会话上下文参数,关联同一对话的交互记录。以下是基于你现有代码的修改方案:

修改后的多轮搜索函数

from google.cloud import discoveryengine
from typing import List, Optional
import uuid

def genappbuilder_multi_turn_search(
        search_query: str,
        project_id: str = project_id,
        location: str = location,
        search_engine_id: str = search_engine_id,
        serving_config_id: str = serving_config_id,
        conversation_id: Optional[str] = None,
        previous_history: Optional[List[discoveryengine.ConversationContext.HistoryEntry]] = None,
) -> tuple[List[discoveryengine.SearchResponse.SearchResult], str, List[discoveryengine.ConversationContext.HistoryEntry]]:
    # 初始化客户端
    client = discoveryengine.SearchServiceClient()

    # 构建服务配置路径
    serving_config = client.serving_config_path(
        project=project_id,
        location=location,
        data_store=search_engine_id,
        serving_config=serving_config_id,
    )

    # 配置会话上下文
    conversation_context = discoveryengine.ConversationContext()
    # 复用或生成会话ID(同一对话必须使用相同ID)
    conversation_context.conversation_id = conversation_id or str(uuid.uuid4())
    # 传入历史交互记录
    if previous_history:
        conversation_context.history.extend(previous_history)
    
    # 将当前用户查询加入历史
    user_query_entry = discoveryengine.ConversationContext.HistoryEntry(
        user_query=search_query
    )
    conversation_context.history.append(user_query_entry)

    # 构建多轮搜索请求
    request = discoveryengine.SearchRequest(
        serving_config=serving_config,
        query=search_query,
        page_size=5,
        conversation_context=conversation_context
    )

    # 发送请求并获取响应
    response = client.search(request)

    # 将引擎响应加入历史,供下一轮使用
    response_entry = discoveryengine.ConversationContext.HistoryEntry(
        response=discoveryengine.ConversationContext.HistoryEntry.Response(
            search_results=response.results
        )
    )
    updated_history = conversation_context.history.copy()
    updated_history.append(response_entry)

    # 返回搜索结果、会话ID、更新后的历史记录
    return response.results, conversation_context.conversation_id, updated_history

关键说明

  • 会话ID(conversation_id):同一对话的所有轮次必须使用相同的ID,用于关联上下文。如果未传入,函数会自动生成UUID作为会话ID。
  • 历史记录(previous_history):存储之前的用户查询和引擎响应,每轮搜索后需要将新的交互记录传入下一轮调用,让引擎理解当前查询的语境。
  • conversation_context:这是实现多轮搜索的核心参数,包含会话标识和历史交互,让Discovery Engine能够基于上下文优化搜索结果。

使用示例

# 第一轮搜索:初始查询
first_results, conv_id, conversation_history = genappbuilder_multi_turn_search(
    "如何创建Google Cloud虚拟机实例"
)

# 第二轮搜索:基于上下文的追问
second_results, _, updated_history = genappbuilder_multi_turn_search(
    "如何给这个实例分配静态IP?",
    conversation_id=conv_id,
    previous_history=conversation_history
)

注意事项

  • 确保你的Discovery Engine服务配置已经在控制台中启用了多轮搜索功能。
  • 历史记录不宜过长,可根据实际场景截断早期交互,避免上下文冗余影响搜索精度。

内容的提问来源于stack exchange,提问作者Yash Tandon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:55:40