在網頁應用程式中,你可以透過串流回應的形式取得問題解答。本文說明如何使用 REST API 取得答案。如要使用 Gemini Enterprise 網頁應用程式,請參閱「與助理對話」。
StreamAssist 總覽
StreamAssist提供強大的互動式方式來處理使用者查詢。這項功能採用串流模式,可進行即時雙向互動。
這項 API 的主要功能包括:
保留對話脈絡:系統會使用工作階段保留對話脈絡,確保能理解先前互動範圍內的後續查詢。
納入提供的檔案:將附加檔案做為背景資訊,提供更實用且相關的回覆。
完美整合:可與各種代理程式和工具整合,滿足各種使用者要求。
事前準備
請確認您已具備以下條件:
已為 Google Cloud啟用 Discovery Engine API。 您可以在Google Cloud 控制台的 Discovery Engine API 頁面啟用這項功能。
已獲指派
discoveryengine.assistants.assist權限的 Discovery Engine 角色。現有的 Gemini Enterprise 應用程式。如要建立應用程式,請參閱「建立應用程式」。
取得搜尋結果
你可以使用 streamAssist 方法,向 Gemini Enterprise 查詢並取得回覆。以下 curl 指令會列出必填欄位。
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://ENDPOINT_LOCATION-discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/collections/default_collection/engines/APP_ID/assistants/default_assistant:streamAssist" \
-d '
{
"query": {
"text": "QUERY_TEXT_1"
}
}'
更改下列內容:
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值:
us適用於美國多區域eu代表歐盟多區域global適用於全域位置
- PROJECT_ID:專案的 ID。 Google Cloud
- APP_ID:Gemini Enterprise 應用程式的專屬 ID。
- LOCATION:資料儲存庫的多區域:
global、us或eu - QUERY_TEXT_1:搜尋查詢文字。
如要進一步瞭解欄位和傳回內容,請參閱「回應主體」。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Gemini Enterprise 快速入門導覽課程」中的 Python 設定說明操作。詳情請參閱 Gemini Enterprise Python API 參考文件。
如要向 Gemini Enterprise 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine
# TODO(developer): Update these variables before running the sample.
project_id = "YOUR_PROJECT_ID"
location = "YOUR_LOCATION"
engine_id = "YOUR_APP_ID"
query = "What is Gemini Enterprise?"
def stream_assist_sample(
project_id: str,
location: str,
engine_id: str,
query: str,
):
# For more information, refer to:
# https://docs.cloud.google.com/gemini/enterprise/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)
client = discoveryengine.AssistantServiceClient(client_options=client_options)
request = discoveryengine.StreamAssistRequest(
name=client.assistant_path(
project=project_id,
location=location,
collection="default_collection",
engine=engine_id,
assistant="default_assistant",
),
query=discoveryengine.Query(text=query),
)
# Make the request
stream = client.stream_assist(request=request)
# Handle the response
for response in stream:
print(response)
在同一工作階段中取得搜尋結果
如要在現有工作階段中繼續對話並取得 Gemini Enterprise 的回覆,請使用 streamAssist 方法。下列程式碼範例顯示必要欄位:
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://ENDPOINT_LOCATION-discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/collections/default_collection/engines/APP_ID/assistants/default_assistant:streamAssist" \
-d '
{
"session": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID",
"query": {
"text": "QUERY_TEXT_2"
}
}'
更改下列內容:
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值:
us適用於美國多區域eu代表歐盟多區域global適用於全域位置
- PROJECT_ID:專案的 ID。 Google Cloud
- APP_ID:Gemini Enterprise 應用程式的專屬 ID。
- LOCATION:資料儲存庫的多區域:
global、us或eu - QUERY_TEXT_2:搜尋查詢文字。
- SESSION_ID:先前對話或查詢的階段 ID。
如要進一步瞭解欄位和傳回內容,請參閱「回應主體」。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Gemini Enterprise 快速入門導覽課程」中的 Python 設定說明操作。詳情請參閱 Gemini Enterprise Python API 參考文件。
如要向 Gemini Enterprise 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine
# TODO(developer): Update these variables before running the sample.
project_id = "YOUR_PROJECT_ID"
location = "YOUR_LOCATION"
engine_id = "YOUR_APP_ID"
session = "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID" # From previous session
query = "What is Gemini Enterprise?"
def stream_assist_session_sample(
project_id: str,
location: str,
engine_id: str,
session: str,
query: str,
):
# For more information, refer to:
# https://docs.cloud.google.com/gemini/enterprise/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)
client = discoveryengine.AssistantServiceClient(client_options=client_options)
request = discoveryengine.StreamAssistRequest(
name=client.assistant_path(
project=project_id,
location=location,
collection="default_collection",
engine=engine_id,
assistant="default_assistant",
),
session=session,
query=discoveryengine.Query(text=query),
)
# Make the request
stream = client.stream_assist(request=request)
# Handle the response
for response in stream:
print(response.answer)
# Get Session ID
print(response.session_info.session)
上傳檔案並取得搜尋結果 (預覽版)
如要提供更多背景資訊給助理,請將檔案上傳至工作階段,然後查詢檔案內容。步驟如下:
使用
addContextFile方法將檔案上傳至工作階段。REST
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://ENDPOINT_LOCATION-discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/collections/default_collection/engines/APP_ID/sessions/SESSION_ID:addContextFile" \ -d ' { "fileName": "FILE_NAME", "mimeType": "MIME_TYPE", "fileContents": "BASE64_ENCODED_FILE" }'更改下列內容:
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值:
us適用於美國多區域eu代表歐盟多區域global適用於全域位置
- PROJECT_ID:專案的 ID。 Google Cloud
- LOCATION:資料儲存庫的多區域:
global、us或eu - APP_ID:Gemini Enterprise 應用程式的專屬 ID。
- SESSION_ID:先前對話或查詢的階段 ID。
- FILE_NAME:檔案名稱。
- MIME_TYPE:檔案的 MIME 類型,例如
text/plain或application/pdf。如要進一步瞭解不同的媒體類型,請參閱「媒體類型」。 - BASE64_ENCODED_FILE:檔案的 Base64 編碼表示法。
如要進一步瞭解欄位和傳回內容,請參閱「回應主體」。
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值:
使用相同工作階段,詢問您透過
streamAssist方法上傳的檔案。REST
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://ENDPOINT_LOCATION-discoveryengine.googleapis.com/v1alpha/projects/PROJECT_ID/locations/LOCATION/collections/default_collection/engines/APP_ID/assistants/default_assistant:streamAssist" \ -d ' { "session": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID", "fileIds": ["FILE_ID"], "query": { "text": "QUERY_TEXT_3" } }'更改下列內容:
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值:
us適用於美國多區域eu代表歐盟多區域global適用於全域位置
- PROJECT_ID:專案的 ID。 Google Cloud
- APP_ID:Gemini Enterprise 應用程式的專屬 ID。
- LOCATION:資料儲存庫的多區域:
global、us或eu - QUERY_TEXT_3:搜尋查詢文字。
- SESSION_ID:先前對話或查詢的階段 ID。
- FILE_ID:您上傳至助理的檔案 ID。
如要進一步瞭解欄位和傳回內容,請參閱「回應主體」。
- ENDPOINT_LOCATION:API 要求適用的多區域。請指定下列其中一個值: