Skip to content

Commit be3a8bc

Browse files
docs(samples): Document AI Warehouse - Add New Sample Codes (GoogleCloudPlatform#9190)
* Add files via upload * fix: Ran Linter & Black Format * fix: Ran isort * Apply suggestions from code review Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Update contentwarehouse/snippets/create_rule_set_sample.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * ran black/isort * Update contentwarehouse/snippets/update_document_schema_sample_test.py Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> * Add files via upload * Add files via upload * Add files via upload * fix lint errors * deleted unused --------- Co-authored-by: Holt Skinner <holtskinner@google.com> Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com>
1 parent b446cd2 commit be3a8bc

8 files changed

+466
-2
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Copyright 2023 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+
17+
# [START contentwarehouse_create_folder_link_document]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = "YOUR_PROJECT_NUMBER"
23+
# location = "us" # Format is 'us' or 'eu'
24+
# user_id = "user:xxxx@example.com" # Format is "user:xxxx@example.com"
25+
26+
27+
def create_folder(
28+
project_number: str, location: str, user_id: str
29+
) -> contentwarehouse.Document:
30+
# Create a Schema Service client
31+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
32+
33+
# The full resource name of the location, e.g.:
34+
# projects/{project_number}/locations/{location}
35+
parent = document_schema_client.common_location_path(
36+
project=project_number, location=location
37+
)
38+
39+
# Define Folder Schema Request
40+
create_folder_schema_request = contentwarehouse.CreateDocumentSchemaRequest(
41+
parent=parent,
42+
document_schema=contentwarehouse.DocumentSchema(
43+
display_name="Test Folder Schema ",
44+
document_is_folder=True,
45+
),
46+
)
47+
48+
# Create a Folder Schema
49+
folder_schema = document_schema_client.create_document_schema(
50+
request=create_folder_schema_request
51+
)
52+
53+
# Create a Document(Folder) Service client
54+
folder_client = contentwarehouse.DocumentServiceClient()
55+
56+
# Define Folder
57+
folder = contentwarehouse.Document(
58+
display_name="My Test Folder",
59+
document_schema_name=folder_schema.name,
60+
)
61+
62+
# Define Request to create Folder
63+
create_folder_request = contentwarehouse.CreateDocumentRequest(
64+
parent=parent,
65+
document=folder,
66+
request_metadata=contentwarehouse.RequestMetadata(
67+
user_info=contentwarehouse.UserInfo(id=user_id)
68+
),
69+
)
70+
71+
# Create a Folder for the given schema
72+
folder_response = folder_client.create_document(request=create_folder_request)
73+
74+
# Read the output
75+
print(f"Rule Engine Output: {folder_response.rule_engine_output}")
76+
print(f"Folder Created: {folder_response.document}")
77+
78+
return folder_response
79+
80+
81+
def create_document(
82+
project_number: str, location: str, user_id: str
83+
) -> contentwarehouse.Document:
84+
# Create a Schema Service client
85+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
86+
87+
# The full resource name of the location, e.g.:
88+
# projects/{project_number}/locations/{location}
89+
parent = document_schema_client.common_location_path(
90+
project=project_number, location=location
91+
)
92+
93+
# Define Schema Property of Text Type
94+
property_definition = contentwarehouse.PropertyDefinition(
95+
name="stock_symbol", # Must be unique within a document schema (case insensitive)
96+
display_name="Searchable text",
97+
is_searchable=True,
98+
text_type_options=contentwarehouse.TextTypeOptions(),
99+
)
100+
101+
# Define Document Schema Request
102+
create_document_schema_request = contentwarehouse.CreateDocumentSchemaRequest(
103+
parent=parent,
104+
document_schema=contentwarehouse.DocumentSchema(
105+
display_name="My Test Schema",
106+
property_definitions=[property_definition],
107+
),
108+
)
109+
110+
# Create a Document schema
111+
document_schema = document_schema_client.create_document_schema(
112+
request=create_document_schema_request
113+
)
114+
115+
# Create a Document Service client
116+
document_client = contentwarehouse.DocumentServiceClient()
117+
118+
# The full resource name of the location, e.g.:
119+
# projects/{project_number}/locations/{location}
120+
parent = document_client.common_location_path(
121+
project=project_number, location=location
122+
)
123+
124+
# Define Document Property Value
125+
document_property = contentwarehouse.Property(
126+
name=document_schema.property_definitions[0].name,
127+
text_values=contentwarehouse.TextArray(values=["GOOG"]),
128+
)
129+
130+
# Define Document
131+
document = contentwarehouse.Document(
132+
display_name="My Test Document",
133+
document_schema_name=document_schema.name,
134+
plain_text="This is a sample of a document's text.",
135+
properties=[document_property],
136+
)
137+
138+
# Define Request
139+
create_document_request = contentwarehouse.CreateDocumentRequest(
140+
parent=parent,
141+
document=document,
142+
request_metadata=contentwarehouse.RequestMetadata(
143+
user_info=contentwarehouse.UserInfo(id=user_id)
144+
),
145+
)
146+
147+
# Create a Document for the given schema
148+
document_response = document_client.create_document(request=create_document_request)
149+
150+
# Read the output
151+
print(f"Rule Engine Output: {document_response.rule_engine_output}")
152+
print(f"Document Created: {document_response.document}")
153+
154+
return document_response
155+
156+
157+
def create_folder_link_document(
158+
project_number: str, location: str, user_id: str
159+
) -> None:
160+
# Function call to create a folder
161+
folder = create_folder(project_number, location, user_id)
162+
163+
# Function call to create a Document
164+
document = create_document(project_number, location, user_id)
165+
166+
# Create a Link Service client
167+
link_client = contentwarehouse.DocumentLinkServiceClient()
168+
169+
# Initialize request argument(s)
170+
link = contentwarehouse.DocumentLink(
171+
source_document_reference=contentwarehouse.DocumentReference(
172+
document_name=folder.document.name,
173+
),
174+
target_document_reference=contentwarehouse.DocumentReference(
175+
document_name=document.document.name,
176+
),
177+
)
178+
179+
# Initialize document link request
180+
create_document_link_request = contentwarehouse.CreateDocumentLinkRequest(
181+
parent=folder.document.name,
182+
document_link=link,
183+
request_metadata=contentwarehouse.RequestMetadata(
184+
user_info=contentwarehouse.UserInfo(id=user_id)
185+
),
186+
)
187+
188+
# Make the Document Link request
189+
create_link_response = link_client.create_document_link(
190+
request=create_document_link_request
191+
)
192+
193+
print(f"Link Created: {create_link_response}")
194+
195+
# Initialize list linked targets request
196+
linked_targets_request = contentwarehouse.ListLinkedTargetsRequest(
197+
parent=folder.document.name,
198+
request_metadata=contentwarehouse.RequestMetadata(
199+
user_info=contentwarehouse.UserInfo(id=user_id)
200+
),
201+
)
202+
203+
# Make the request
204+
linked_targets_response = link_client.list_linked_targets(
205+
request=linked_targets_request
206+
)
207+
208+
print(f"Validate Link Created: {linked_targets_response}")
209+
210+
211+
# [END contentwarehouse_create_folder_link_document]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# # Copyright 2023 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+
18+
from contentwarehouse.snippets import create_folder_link_document_sample
19+
from contentwarehouse.snippets import test_utilities
20+
import pytest
21+
22+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
location = "us" # Format is 'us' or 'eu'
24+
user_id = "user:xxxx@example.com" # Format is "user:xxxx@example.com"
25+
26+
27+
def test_create_folder_link_document(capsys: pytest.CaptureFixture) -> None:
28+
project_number = test_utilities.get_project_number(project_id)
29+
create_folder_link_document_sample.create_folder_link_document(
30+
project_number=project_number,
31+
location=location,
32+
user_id=user_id,
33+
)
34+
out, _ = capsys.readouterr()
35+
36+
assert "Rule Engine Output" in out
37+
assert "Folder Created" in out
38+
assert "Rule Engine Output" in out
39+
assert "Document Created" in out
40+
assert "Link Created" in out
41+
assert "Validate Link Created" in out
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2023 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+
17+
# [START contentwarehouse_create_rule_set]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = "YOUR_PROJECT_NUMBER"
23+
# location = "us" # Format is 'us' or 'eu'
24+
25+
26+
def create_rule_set(project_number: str, location: str) -> None:
27+
# Create a client
28+
client = contentwarehouse.RuleSetServiceClient()
29+
30+
# The full resource name of the location, e.g.:
31+
# projects/{project_number}/locations/{location}
32+
parent = client.common_location_path(project=project_number, location=location)
33+
34+
actions = contentwarehouse.Action(
35+
delete_document_action=contentwarehouse.DeleteDocumentAction(
36+
enable_hard_delete=True
37+
)
38+
)
39+
40+
rules = contentwarehouse.Rule(
41+
trigger_type="ON_CREATE",
42+
condition="documentType == 'W9' && STATE =='CA'",
43+
actions=[actions],
44+
)
45+
46+
rule_set = contentwarehouse.RuleSet(
47+
description="W9: Basic validation check rules.",
48+
source="My Organization",
49+
rules=[rules],
50+
)
51+
52+
# Initialize request argument(s)
53+
request = contentwarehouse.CreateRuleSetRequest(parent=parent, rule_set=rule_set)
54+
55+
# Make the request
56+
response = client.create_rule_set(request=request)
57+
58+
# Handle the response
59+
print(f"Rule Set Created: {response}")
60+
61+
# Initialize request argument(s)
62+
request = contentwarehouse.ListRuleSetsRequest(
63+
parent=parent,
64+
)
65+
66+
# Make the request
67+
page_result = client.list_rule_sets(request=request)
68+
69+
# Handle the response
70+
for response in page_result:
71+
print(f"Rule Sets: {response}")
72+
73+
74+
# [END contentwarehouse_create_rule_set]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# # Copyright 2023 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+
18+
from contentwarehouse.snippets import create_rule_set_sample
19+
from contentwarehouse.snippets import test_utilities
20+
import pytest
21+
22+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
location = "us"
24+
25+
26+
def test_create_rule_set(capsys: pytest.CaptureFixture) -> None:
27+
project_number = test_utilities.get_project_number(project_id)
28+
create_rule_set_sample.create_rule_set(
29+
project_number=project_number,
30+
location=location,
31+
)
32+
out, _ = capsys.readouterr()
33+
34+
assert "Rule Set Created" in out
35+
assert "Rule Sets" in out

contentwarehouse/snippets/quickstart_sample.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525

2626
def quickstart(project_number: str, location: str) -> None:
27-
2827
# Create a Schema Service client
2928
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
3029

contentwarehouse/snippets/search_documents_sample.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def search_documents_sample(
2929
location: str,
3030
document_query_text: str,
3131
) -> None:
32-
3332
# Create a client
3433
client = contentwarehouse.DocumentServiceClient()
3534

0 commit comments

Comments
 (0)