|
| 1 | +# flake8: noqa: E402, I100 |
| 2 | +# Copyright 2024 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +# NOTE: This snippet has been partially generated by `gemini-1.5-pro-001` |
| 18 | + |
| 19 | +# [START genappbuilder_create_session] |
| 20 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 21 | + |
| 22 | + |
| 23 | +def create_session( |
| 24 | + project_id: str, |
| 25 | + location: str, |
| 26 | + engine_id: str, |
| 27 | + user_pseudo_id: str, |
| 28 | +) -> discoveryengine.Session: |
| 29 | + """Creates a session. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_id: The ID of your Google Cloud project. |
| 33 | + location: The location of the app. |
| 34 | + engine_id: The ID of the app. |
| 35 | + user_pseudo_id: A unique identifier for tracking visitors. For example, this |
| 36 | + could be implemented with an HTTP cookie, which should be able to |
| 37 | + uniquely identify a visitor on a single device. |
| 38 | + Returns: |
| 39 | + discoveryengine.Session: The newly created Session. |
| 40 | + """ |
| 41 | + |
| 42 | + client = discoveryengine.ConversationalSearchServiceClient() |
| 43 | + |
| 44 | + session = client.create_session( |
| 45 | + # The full resource name of the engine |
| 46 | + parent=f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}", |
| 47 | + session=discoveryengine.Session(user_pseudo_id=user_pseudo_id), |
| 48 | + ) |
| 49 | + |
| 50 | + # Send Session name in `answer_query()` |
| 51 | + print(f"Session: {session.name}") |
| 52 | + return session |
| 53 | + |
| 54 | + |
| 55 | +# [END genappbuilder_create_session] |
| 56 | + |
| 57 | +# [START genappbuilder_get_session] |
| 58 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 59 | + |
| 60 | + |
| 61 | +def get_session( |
| 62 | + project_id: str, |
| 63 | + location: str, |
| 64 | + engine_id: str, |
| 65 | + session_id: str, |
| 66 | +) -> discoveryengine.Session: |
| 67 | + """Retrieves a session. |
| 68 | +
|
| 69 | + Args: |
| 70 | + project_id: The ID of your Google Cloud project. |
| 71 | + location: The location of the app. |
| 72 | + engine_id: The ID of the app. |
| 73 | + session_id: The ID of the session. |
| 74 | + """ |
| 75 | + |
| 76 | + client = discoveryengine.ConversationalSearchServiceClient() |
| 77 | + |
| 78 | + # The full resource name of the session |
| 79 | + name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}" |
| 80 | + |
| 81 | + session = client.get_session(name=name) |
| 82 | + |
| 83 | + print(f"Session details: {session}") |
| 84 | + return session |
| 85 | + |
| 86 | + |
| 87 | +# [END genappbuilder_get_session] |
| 88 | + |
| 89 | + |
| 90 | +# [START genappbuilder_delete_session] |
| 91 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 92 | + |
| 93 | + |
| 94 | +def delete_session( |
| 95 | + project_id: str, |
| 96 | + location: str, |
| 97 | + engine_id: str, |
| 98 | + session_id: str, |
| 99 | +) -> None: |
| 100 | + """Deletes a session. |
| 101 | +
|
| 102 | + Args: |
| 103 | + project_id: The ID of your Google Cloud project. |
| 104 | + location: The location of the app. |
| 105 | + engine_id: The ID of the app. |
| 106 | + session_id: The ID of the session. |
| 107 | + """ |
| 108 | + |
| 109 | + client = discoveryengine.ConversationalSearchServiceClient() |
| 110 | + |
| 111 | + # The full resource name of the session |
| 112 | + name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}" |
| 113 | + |
| 114 | + client.delete_session(name=name) |
| 115 | + |
| 116 | + print(f"Session {name} deleted.") |
| 117 | + |
| 118 | + |
| 119 | +# [END genappbuilder_delete_session] |
| 120 | + |
| 121 | + |
| 122 | +# [START genappbuilder_update_session] |
| 123 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 124 | +from google.protobuf import field_mask_pb2 |
| 125 | + |
| 126 | + |
| 127 | +def update_session( |
| 128 | + project_id: str, |
| 129 | + location: str, |
| 130 | + engine_id: str, |
| 131 | + session_id: str, |
| 132 | +) -> discoveryengine.Session: |
| 133 | + """Updates a session. |
| 134 | +
|
| 135 | + Args: |
| 136 | + project_id: The ID of your Google Cloud project. |
| 137 | + location: The location of the app. |
| 138 | + engine_id: The ID of the app. |
| 139 | + session_id: The ID of the session. |
| 140 | + Returns: |
| 141 | + discoveryengine.Session: The updated Session. |
| 142 | + """ |
| 143 | + client = discoveryengine.ConversationalSearchServiceClient() |
| 144 | + |
| 145 | + # The full resource name of the session |
| 146 | + name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}" |
| 147 | + |
| 148 | + session = discoveryengine.Session( |
| 149 | + name=name, |
| 150 | + state=discoveryengine.Session.State.IN_PROGRESS, # Options: IN_PROGRESS, STATE_UNSPECIFIED |
| 151 | + ) |
| 152 | + |
| 153 | + # Fields to Update |
| 154 | + update_mask = field_mask_pb2.FieldMask(paths=["state"]) |
| 155 | + |
| 156 | + session = client.update_session(session=session, update_mask=update_mask) |
| 157 | + print(f"Updated session: {session.name}") |
| 158 | + return session |
| 159 | + |
| 160 | + |
| 161 | +# [END genappbuilder_update_session] |
| 162 | + |
| 163 | + |
| 164 | +# [START genappbuilder_list_sessions] |
| 165 | +from google.cloud import discoveryengine_v1 as discoveryengine |
| 166 | + |
| 167 | + |
| 168 | +def list_sessions( |
| 169 | + project_id: str, |
| 170 | + location: str, |
| 171 | + engine_id: str, |
| 172 | +) -> discoveryengine.ListSessionsResponse: |
| 173 | + """Lists all sessions associated with a data store. |
| 174 | +
|
| 175 | + Args: |
| 176 | + project_id: The ID of your Google Cloud project. |
| 177 | + location: The location of the app. |
| 178 | + engine_id: The ID of the app. |
| 179 | + Returns: |
| 180 | + discoveryengine.ListSessionsResponse: The list of sessions. |
| 181 | + """ |
| 182 | + |
| 183 | + client = discoveryengine.ConversationalSearchServiceClient() |
| 184 | + |
| 185 | + # The full resource name of the engine |
| 186 | + parent = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}" |
| 187 | + |
| 188 | + response = client.list_sessions( |
| 189 | + request=discoveryengine.ListSessionsRequest( |
| 190 | + parent=parent, |
| 191 | + filter='state="IN_PROGRESS"', # Optional: Filter requests by userPseudoId or state |
| 192 | + order_by="update_time", # Optional: Sort results |
| 193 | + ) |
| 194 | + ) |
| 195 | + |
| 196 | + print("Sessions:") |
| 197 | + for session in response.sessions: |
| 198 | + print(session) |
| 199 | + |
| 200 | + return response |
| 201 | + |
| 202 | + |
| 203 | +# [END genappbuilder_list_sessions] |
0 commit comments