Skip to content

Commit 574cf10

Browse files
Thoughtseize1riathakkar
authored andcommitted
refactor: (GenAI) Reorganized Reasoning Engine Samples (Group С) (GoogleCloudPlatform#12613)
* Created new reasoning_engine folder and new Samples * Try to pass tests * Renamed files for clarify and preventing conflicts * Fix comments and dependencies
1 parent 8678ca6 commit 574cf10

7 files changed

+418
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
# https://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+
import os
15+
16+
from typing import Dict, Union
17+
18+
from vertexai.preview import reasoning_engines
19+
20+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
21+
22+
23+
def create_reasoning_engine_advanced(
24+
staging_bucket: str,
25+
) -> reasoning_engines.ReasoningEngine:
26+
# [START generativeaionvertexai_create_reasoning_engine_advanced]
27+
28+
from typing import List
29+
30+
import vertexai
31+
from vertexai.preview import reasoning_engines
32+
33+
# TODO(developer): Update and un-comment below lines
34+
# PROJECT_ID = "your-project-id"
35+
# staging_bucket = "gs://YOUR_BUCKET_NAME"
36+
37+
vertexai.init(
38+
project=PROJECT_ID, location="us-central1", staging_bucket=staging_bucket
39+
)
40+
41+
class LangchainApp:
42+
def __init__(self, project: str, location: str) -> None:
43+
self.project_id = project
44+
self.location = location
45+
46+
def set_up(self) -> None:
47+
from langchain_core.prompts import ChatPromptTemplate
48+
from langchain_google_vertexai import ChatVertexAI
49+
50+
system = (
51+
"You are a helpful assistant that answers questions "
52+
"about Google Cloud."
53+
)
54+
human = "{text}"
55+
prompt = ChatPromptTemplate.from_messages(
56+
[("system", system), ("human", human)]
57+
)
58+
chat = ChatVertexAI(project=self.project_id, location=self.location)
59+
self.chain = prompt | chat
60+
61+
def query(self, question: str) -> Union[str, List[Union[str, Dict]]]:
62+
"""Query the application.
63+
Args:
64+
question: The user prompt.
65+
Returns:
66+
str: The LLM response.
67+
"""
68+
return self.chain.invoke({"text": question}).content
69+
70+
# Locally test
71+
app = LangchainApp(project=PROJECT_ID, location="us-central1")
72+
app.set_up()
73+
print(app.query("What is Vertex AI?"))
74+
75+
# Create a remote app with Reasoning Engine
76+
# Deployment of the app should take a few minutes to complete.
77+
reasoning_engine = reasoning_engines.ReasoningEngine.create(
78+
LangchainApp(project=PROJECT_ID, location="us-central1"),
79+
requirements=[
80+
"google-cloud-aiplatform[langchain,reasoningengine]",
81+
"cloudpickle==3.0.0",
82+
"pydantic==2.7.4",
83+
],
84+
display_name="Demo LangChain App",
85+
description="This is a simple LangChain app.",
86+
# sys_version="3.10", # Optional
87+
extra_packages=[],
88+
)
89+
# Example response:
90+
# Model_name will become a required arg for VertexAIEmbeddings starting...
91+
# ...
92+
# Create ReasoningEngine backing LRO: projects/123456789/locations/us-central1/reasoningEngines/...
93+
# ReasoningEngine created. Resource name: projects/123456789/locations/us-central1/reasoningEngines/...
94+
# ...
95+
96+
# [END generativeaionvertexai_create_reasoning_engine_advanced]
97+
return reasoning_engine
98+
99+
100+
if __name__ == "__main__":
101+
create_reasoning_engine_advanced("gs://your-bucket-unique-name")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
# https://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+
import os
15+
16+
from vertexai.preview import reasoning_engines
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def create_reasoning_engine_basic(
22+
staging_bucket: str,
23+
) -> reasoning_engines.ReasoningEngine:
24+
# [START generativeaionvertexai_create_reasoning_engine_basic]
25+
import vertexai
26+
from vertexai.preview import reasoning_engines
27+
28+
# TODO(developer): Update and un-comment below lines
29+
# PROJECT_ID = "your-project-id"
30+
# staging_bucket = "gs://YOUR_BUCKET_NAME"
31+
vertexai.init(
32+
project=PROJECT_ID, location="us-central1", staging_bucket=staging_bucket
33+
)
34+
35+
class SimpleAdditionApp:
36+
def query(self, a: int, b: int) -> str:
37+
"""Query the application.
38+
Args:
39+
a: The first input number
40+
b: The second input number
41+
Returns:
42+
int: The additional result.
43+
"""
44+
return f"{int(a)} + {int(b)} is {int(a + b)}"
45+
46+
# Locally test
47+
app = SimpleAdditionApp()
48+
app.query(a=1, b=2)
49+
50+
# Create a remote app with Reasoning Engine.
51+
# This may take 1-2 minutes to finish.
52+
reasoning_engine = reasoning_engines.ReasoningEngine.create(
53+
SimpleAdditionApp(),
54+
display_name="Demo Addition App",
55+
description="A simple demo addition app",
56+
requirements=["cloudpickle==3"],
57+
extra_packages=[],
58+
)
59+
# Example response:
60+
# Using bucket YOUR_BUCKET_NAME
61+
# Writing to gs://YOUR_BUCKET_NAME/reasoning_engine/reasoning_engine.pkl
62+
# ...
63+
# ReasoningEngine created. Resource name: projects/123456789/locations/us-central1/reasoningEngines/123456
64+
# To use this ReasoningEngine in another session:
65+
# reasoning_engine = vertexai.preview.reasoning_engines.ReasoningEngine('projects/123456789/locations/...
66+
67+
# [END generativeaionvertexai_create_reasoning_engine_basic]
68+
return reasoning_engine
69+
70+
71+
if __name__ == "__main__":
72+
create_reasoning_engine_basic("gs://your-bucket-unique-name")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
# https://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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def delete_reasoning_engine(reasoning_engine_id: str) -> None:
20+
# [START generativeaionvertexai_delete_reasoning_engine]
21+
import vertexai
22+
from vertexai.preview import reasoning_engines
23+
24+
# TODO(developer): Update and un-comment below lines
25+
# PROJECT_ID = "your-project-id"
26+
# reasoning_engine_id = "1234567890123456"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
29+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
30+
reasoning_engine.delete()
31+
# Example response:
32+
# Deleting ReasoningEngine:projects/[PROJECT_ID]/locations/us-central1/reasoningEngines/1234567890123456
33+
# ...
34+
# ... resource projects/[PROJECT_ID]/locations/us-central1/reasoningEngines/1234567890123456 deleted.
35+
36+
# [END generativeaionvertexai_delete_reasoning_engine]
37+
38+
39+
if __name__ == "__main__":
40+
delete_reasoning_engine("1234567890123456")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# https://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+
import os
15+
16+
from vertexai.preview import reasoning_engines
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def get_reasoning_engine(reasoning_engine_id: str) -> reasoning_engines.ReasoningEngine:
22+
# [START generativeaionvertexai_get_reasoning_engine]
23+
import vertexai
24+
from vertexai.preview import reasoning_engines
25+
26+
# TODO(developer): Update and un-comment below lines
27+
# PROJECT_ID = "your-project-id"
28+
# reasoning_engine_id = "1234567890123456"
29+
vertexai.init(project=PROJECT_ID, location="us-central1")
30+
31+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
32+
print(reasoning_engine)
33+
# Example response:
34+
# <vertexai.reasoning_engines._reasoning_engines.ReasoningEngine object at 0x757999a63c40>
35+
# resource name: projects/[PROJECT_ID]/locations/us-central1/reasoningEngines/1234567890123456
36+
37+
# [END generativeaionvertexai_get_reasoning_engine]
38+
return reasoning_engine
39+
40+
41+
if __name__ == "__main__":
42+
get_reasoning_engine("1234567890123456")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
# https://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+
import os
15+
16+
from typing import List
17+
18+
from vertexai.preview import reasoning_engines
19+
20+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
21+
22+
23+
def list_reasoning_engines() -> List[reasoning_engines.ReasoningEngine]:
24+
# [START generativeaionvertexai_list_reasoning_engines]
25+
import vertexai
26+
from vertexai.preview import reasoning_engines
27+
28+
# TODO(developer): Update and un-comment below line
29+
# PROJECT_ID = "your-project-id"
30+
vertexai.init(project=PROJECT_ID, location="us-central1")
31+
32+
reasoning_engine_list = reasoning_engines.ReasoningEngine.list()
33+
print(reasoning_engine_list)
34+
# Example response:
35+
# [<vertexai.reasoning_engines._reasoning_engines.ReasoningEngine object at 0x71a0e5cb99c0>
36+
# resource name: projects/123456789/locations/us-central1/reasoningEngines/111111111111111111,
37+
# <vertexai.reasoning_engines._reasoning_engines.ReasoningEngine object at 0x71a0e5cbac80>
38+
# resource name: projects/123456789/locations/us-central1/reasoningEngines/222222222222222222]
39+
40+
# [END generativeaionvertexai_list_reasoning_engines]
41+
return reasoning_engine_list
42+
43+
44+
if __name__ == "__main__":
45+
list_reasoning_engines()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
# https://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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def query_reasoning_engine(reasoning_engine_id: str) -> object:
20+
# [START generativeaionvertexai_query_reasoning_engine]
21+
import vertexai
22+
from vertexai.preview import reasoning_engines
23+
24+
# TODO(developer): Update and un-comment below lines
25+
# PROJECT_ID = "your-project-id"
26+
# reasoning_engine_id = "1234567890123456"
27+
vertexai.init(project=PROJECT_ID, location="us-central1")
28+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
29+
30+
# Replace with kwargs for `.query()` method.
31+
response = reasoning_engine.query(a=1, b=2)
32+
print(response)
33+
# Example response:
34+
# 1 + 2 is 3
35+
36+
# [END generativeaionvertexai_query_reasoning_engine]
37+
return response
38+
39+
40+
if __name__ == "__main__":
41+
query_reasoning_engine("1234567890123456")

0 commit comments

Comments
 (0)