In your web app, you can get answers to your questions in the form of streaming responses. This document shows you how to use the REST API to get answers. If you want to use the Gemini Enterprise web app, see Chat with the assistant.
StreamAssist overview
StreamAssist provides a powerful, interactive way to handle user
queries. It operates in a streaming fashion, enabling real-time, back-and-forth
interactions.
The key capabilities of the API include the following:
Maintaining conversational context: it uses sessions to maintain conversational context, ensuring it understands follow-up queries within the scope of previous interactions.
Incorporating provided files: it includes the attached files as context for more informed and relevant responses.
Seamless integration: it integrates with various agents and tools to fulfill a wide range of user requests.
Before you begin
Ensure that you have the following:
The Discovery Engine API enabled for your Google Cloud. You can enable it on the Discovery Engine API page in the Google Cloud console.
A Discovery Engine role that has the
discoveryengine.assistants.assistpermission assigned to it.An existing Gemini Enterprise app. To create an app, see Create an app.
Get search results
You can use the streamAssist
method to query and get answers from
Gemini Enterprise. The following curl command lists the mandatory fields.
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"
}
}'
Replace the following:
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values:
usfor the US multi-regioneufor the EU multi-regionglobalfor the Global location
- PROJECT_ID: the ID of your Google Cloud project.
- APP_ID: the unique identifier for the Gemini Enterprise app.
- LOCATION: the multi-region of your data store:
global,us, oreu - QUERY_TEXT_1: the search query text.
For more information on the fields and what is returned, see Response body.
Python
Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.
To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
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)
Get search results using the same session
To continue a conversation and get answers from Gemini Enterprise within an
existing session, use the
streamAssist method. The following
code sample shows the mandatory fields:
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"
}
}'
Replace the following:
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values:
usfor the US multi-regioneufor the EU multi-regionglobalfor the Global location
- PROJECT_ID: the ID of your Google Cloud project.
- APP_ID: the unique identifier for the Gemini Enterprise app.
- LOCATION: the multi-region of your data store:
global,us, oreu - QUERY_TEXT_2: the search query text.
- SESSION_ID: the ID of a session from a previous conversation or query.
For more information on the fields and what is returned, see Response body.
Python
Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.
To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
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)
Upload a file and get search results (Preview)
To give the assistant more context, upload a file to a session and then query its content. Follow these steps:
Upload a file to a session using the
addContextFilemethod.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" }'Replace the following:
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values:
usfor the US multi-regioneufor the EU multi-regionglobalfor the Global location
- PROJECT_ID: the ID of your Google Cloud project.
- LOCATION: the multi-region of your data store:
global,us, oreu - APP_ID: the unique identifier for the Gemini Enterprise app.
- SESSION_ID: the ID of a session from a previous conversation or query.
- FILE_NAME: the name of the file.
- MIME_TYPE: the MIME type of the file, for example,
text/plainorapplication/pdf. For more information on the different media types, see Media Types. - BASE64_ENCODED_FILE: the base64-encoded representation of the file.
For more information on the fields and what is returned, see Response body.
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values:
Use the same session to ask a question about the file you uploaded using the
streamAssistmethod.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" } }'Replace the following:
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values:
usfor the US multi-regioneufor the EU multi-regionglobalfor the Global location
- PROJECT_ID: the ID of your Google Cloud project.
- APP_ID: the unique identifier for the Gemini Enterprise app.
- LOCATION: the multi-region of your data store:
global,us, oreu - QUERY_TEXT_3: the search query text.
- SESSION_ID: the ID of a session from a previous conversation or query.
- FILE_ID: the ID of the file you uploaded to the assistant.
For more information on the fields and what is returned, see Response body.
- ENDPOINT_LOCATION: the multi-region for your API request. Assign one of the following
values: