Skip to content

Commit 888f473

Browse files
holtskinnergcf-owl-bot[bot]
authored andcommitted
docs(samples): Created session_sample.py (GoogleCloudPlatform#12568)
* docs(samples): Created session_sample.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Edits to list_sessions_with_filter * Update samples for accuracy * Update region tags * Add get_session sample and test * Update flake8 ignore * Change flake8 lint ignore and disclaimer --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 5c65a52 commit 888f473

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed

discoveryengine/session_sample.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
from uuid import uuid4
18+
19+
from discoveryengine import session_sample
20+
21+
import pytest
22+
23+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
location = "global"
25+
engine_id = "test-unstructured-engine_1697471976692"
26+
user_pseudo_id = f"test-{uuid4()}"
27+
28+
29+
@pytest.fixture(scope="module", autouse=True)
30+
def setup_teardown():
31+
session = session_sample.create_session(
32+
project_id=project_id,
33+
location=location,
34+
engine_id=engine_id,
35+
user_pseudo_id=user_pseudo_id,
36+
)
37+
yield session
38+
39+
session_id = session.name.split("/")[-1]
40+
session_sample.delete_session(
41+
project_id=project_id,
42+
location=location,
43+
engine_id=engine_id,
44+
session_id=session_id,
45+
)
46+
47+
48+
def test_create_session(setup_teardown):
49+
session = setup_teardown
50+
51+
assert session
52+
assert session.user_pseudo_id == user_pseudo_id
53+
54+
55+
def test_get_session(setup_teardown):
56+
session = setup_teardown
57+
58+
session_id = session.name.split("/")[-1]
59+
60+
response = session_sample.get_session(
61+
project_id=project_id,
62+
location=location,
63+
engine_id=engine_id,
64+
session_id=session_id,
65+
)
66+
assert response
67+
68+
69+
def test_update_session(setup_teardown):
70+
session = setup_teardown
71+
session_id = session.name.split("/")[-1]
72+
73+
response = session_sample.update_session(
74+
project_id=project_id,
75+
location=location,
76+
engine_id=engine_id,
77+
session_id=session_id,
78+
)
79+
assert response
80+
81+
82+
def test_list_sessions():
83+
response = session_sample.list_sessions(
84+
project_id=project_id,
85+
location=location,
86+
engine_id=engine_id,
87+
)
88+
assert response.sessions

0 commit comments

Comments
 (0)