Skip to content

Commit 92bc4fd

Browse files
samples(generative_ai): Create Reasoning engine samples (GoogleCloudPlatform#11738)
* samples(generative_ai): Create Reasoning Engine Samples * Added Staging bucket and update requirements to include reasoning engine * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix Typing and test errors * Replace remote_app with reasoning_engine * Fix lint error * Fix lint errors * Remove extra import * Update requirements * Update requirements * remove python version specification * Fix typing for 3.8 --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 534d2fc commit 92bc4fd

File tree

4 files changed

+283
-3
lines changed

4 files changed

+283
-3
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
15+
from typing import Dict, List, Union
16+
17+
from vertexai.preview import reasoning_engines
18+
19+
20+
def create_reasoning_engine_basic(
21+
project_id: str, staging_bucket: str
22+
) -> reasoning_engines.ReasoningEngine:
23+
# [START generativeaionvertexai_create_reasoning_engine_basic]
24+
import vertexai
25+
from vertexai.preview import reasoning_engines
26+
27+
# TODO(developer): Update and un-comment below lines
28+
# project_id = "PROJECT_ID"
29+
# staging_bucket = "gs://YOUR_BUCKET_NAME"
30+
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+
39+
Args:
40+
a: The first input number
41+
b: The second input number
42+
43+
Returns:
44+
int: The additional result.
45+
"""
46+
47+
return f"{int(a)} + {int(b)} is {int(a + b)}"
48+
49+
# Locally test
50+
app = SimpleAdditionApp()
51+
app.query(a=1, b=2)
52+
53+
# Create a remote app with reasoning engine.
54+
# This may take 1-2 minutes to finish.
55+
reasoning_engine = reasoning_engines.ReasoningEngine.create(
56+
SimpleAdditionApp(),
57+
display_name="Demo Addition App",
58+
description="A simple demo addition app",
59+
requirements=[],
60+
extra_packages=[],
61+
)
62+
# [END generativeaionvertexai_create_reasoning_engine_basic]
63+
return reasoning_engine
64+
65+
66+
def create_reasoning_engine_advanced(
67+
project_id: str, location: str, staging_bucket: str
68+
) -> reasoning_engines.ReasoningEngine:
69+
# [START generativeaionvertexai_create_reasoning_engine_advanced]
70+
71+
from typing import List
72+
73+
import vertexai
74+
from vertexai.preview import reasoning_engines
75+
76+
# TODO(developer): Update and un-comment below lines
77+
# project_id = "PROJECT_ID"
78+
# location = "us-central1"
79+
# staging_bucket = "gs://YOUR_BUCKET_NAME"
80+
81+
vertexai.init(project=project_id, location=location, staging_bucket=staging_bucket)
82+
83+
class LangchainApp:
84+
def __init__(self, project: str, location: str) -> None:
85+
self.project_id = project
86+
self.location = location
87+
88+
def set_up(self) -> None:
89+
from langchain_core.prompts import ChatPromptTemplate
90+
from langchain_google_vertexai import ChatVertexAI
91+
92+
system = (
93+
"You are a helpful assistant that answers questions "
94+
"about Google Cloud."
95+
)
96+
human = "{text}"
97+
prompt = ChatPromptTemplate.from_messages(
98+
[("system", system), ("human", human)]
99+
)
100+
chat = ChatVertexAI(project=self.project_id, location=self.location)
101+
self.chain = prompt | chat
102+
103+
def query(self, question: str) -> Union[str, List[Union[str, Dict]]]:
104+
"""Query the application.
105+
106+
Args:
107+
question: The user prompt.
108+
109+
Returns:
110+
str: The LLM response.
111+
"""
112+
return self.chain.invoke({"text": question}).content
113+
114+
# Locally test
115+
app = LangchainApp(project=project_id, location=location)
116+
app.set_up()
117+
print(app.query("What is Vertex AI?"))
118+
119+
# Create a remote app with reasoning engine
120+
# This may take 1-2 minutes to finish because it builds a container and turn up HTTP servers.
121+
reasoning_engine = reasoning_engines.ReasoningEngine.create(
122+
LangchainApp(project=project_id, location=location),
123+
requirements=[
124+
"google-cloud-aiplatform==1.50.0",
125+
"langchain-google-vertexai",
126+
"langchain-core",
127+
],
128+
display_name="Demo LangChain App",
129+
description="This is a simple LangChain app.",
130+
# sys_version="3.10", # Optional
131+
extra_packages=[],
132+
)
133+
# [END generativeaionvertexai_create_reasoning_engine_advanced]
134+
return reasoning_engine
135+
136+
137+
def query_reasoning_engine(project_id: str, reasoning_engine_id: str) -> object:
138+
# [START generativeaionvertexai_query_reasoning_engine]
139+
import vertexai
140+
from vertexai.preview import reasoning_engines
141+
142+
# TODO(developer): Update and un-comment below lines
143+
# project_id = "PROJECT_ID"
144+
# reasoning_engine_id = "REASONING_ENGINE_ID"
145+
146+
vertexai.init(project=project_id, location="us-central1")
147+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
148+
149+
# Replace with kwargs for `.query()` method.
150+
response = reasoning_engine.query(a=1, b=2)
151+
print(response)
152+
# [END generativeaionvertexai_query_reasoning_engine]
153+
return response
154+
155+
156+
def list_reasoning_engines(project_id: str) -> List[reasoning_engines.ReasoningEngine]:
157+
# [START generativeaionvertexai_list_reasoning_engines]
158+
import vertexai
159+
from vertexai.preview import reasoning_engines
160+
161+
# TODO(developer): Update and un-comment below lines
162+
# project_id = "PROJECT_ID"
163+
164+
vertexai.init(project=project_id, location="us-central1")
165+
166+
reasoning_engine_list = reasoning_engines.ReasoningEngine.list()
167+
print(reasoning_engine_list)
168+
# [END generativeaionvertexai_list_reasoning_engines]
169+
return reasoning_engine_list
170+
171+
172+
def get_reasoning_engine(
173+
project_id: str, reasoning_engine_id: str
174+
) -> reasoning_engines.ReasoningEngine:
175+
# [START generativeaionvertexai_get_reasoning_engine]
176+
import vertexai
177+
from vertexai.preview import reasoning_engines
178+
179+
# TODO(developer): Update and un-comment below lines
180+
# project_id = "PROJECT_ID"
181+
# reasoning_engine_id = "REASONING_ENGINE_ID"
182+
183+
vertexai.init(project=project_id, location="us-central1")
184+
185+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
186+
print(reasoning_engine)
187+
# [END generativeaionvertexai_get_reasoning_engine]
188+
return reasoning_engine
189+
190+
191+
def delete_reasoning_engine(project_id: str, reasoning_engine_id: str) -> None:
192+
# [START generativeaionvertexai_delete_reasoning_engine]
193+
import vertexai
194+
from vertexai.preview import reasoning_engines
195+
196+
# TODO(developer): Update and un-comment below lines
197+
# project_id = "PROJECT_ID"
198+
# reasoning_engine_id = "REASONING_ENGINE_ID"
199+
200+
vertexai.init(project=project_id, location="us-central1")
201+
202+
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
203+
reasoning_engine.delete()
204+
# [END generativeaionvertexai_delete_reasoning_engine]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
15+
import os
16+
import sys
17+
from typing import Generator
18+
19+
import pytest
20+
21+
import gemini_reasoning_engine
22+
23+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
24+
REGION = "us-central1"
25+
STAGING_BUCKET = "gs://ucaip-samples-us-central1"
26+
27+
28+
@pytest.fixture(scope="module")
29+
def reasoning_engine_id() -> Generator[str, None, None]:
30+
reasoning_engine = gemini_reasoning_engine.create_reasoning_engine_basic(
31+
PROJECT_ID, STAGING_BUCKET
32+
)
33+
yield reasoning_engine.resource_name
34+
print("Deleting Reasoning Engine...")
35+
gemini_reasoning_engine.delete_reasoning_engine(
36+
PROJECT_ID, reasoning_engine.resource_name
37+
)
38+
39+
40+
@pytest.mark.skipif(
41+
sys.version_info >= (3, 12), reason="requires Python version lower than 3.12"
42+
)
43+
def test_create_reasoning_engine_basic(reasoning_engine_id: str) -> None:
44+
assert reasoning_engine_id
45+
46+
47+
def test_create_reasoning_engine_advanced() -> None:
48+
reasoning_engine = gemini_reasoning_engine.create_reasoning_engine_advanced(
49+
PROJECT_ID, REGION, STAGING_BUCKET
50+
)
51+
assert reasoning_engine
52+
gemini_reasoning_engine.delete_reasoning_engine(
53+
PROJECT_ID, reasoning_engine.resource_name
54+
)
55+
56+
57+
def test_query_reasoning_engine(reasoning_engine_id: str) -> None:
58+
response = gemini_reasoning_engine.query_reasoning_engine(
59+
PROJECT_ID, reasoning_engine_id
60+
)
61+
assert response
62+
assert response == "1 + 2 is 3"
63+
64+
65+
def test_list_reasoning_engines() -> None:
66+
response = gemini_reasoning_engine.list_reasoning_engines(PROJECT_ID)
67+
assert response
68+
69+
70+
def test_get_reasoning_engine(reasoning_engine_id: str) -> None:
71+
response = gemini_reasoning_engine.get_reasoning_engine(
72+
PROJECT_ID, reasoning_engine_id
73+
)
74+
assert response
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
backoff==2.2.1
2-
google-api-core==2.17.1
2+
google-api-core==2.19.0
33
pytest==8.1.1
44
pytest-asyncio==0.23.6

generative_ai/requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pandas==1.3.5; python_version == '3.7'
22
pandas==2.0.1; python_version > '3.7'
33
pillow==9.5.0; python_version < '3.8'
44
pillow==10.3.0; python_version >= '3.8'
5-
google-cloud-aiplatform[pipelines]==1.50.0
6-
google-auth==2.17.3
5+
google-cloud-aiplatform[pipelines,reasoningengine]==1.50.0
6+
google-auth==2.29.0
77
anthropic[vertex]==0.25.6
8+
langchain-core==0.1.52
9+
langchain-google-vertexai==1.0.3

0 commit comments

Comments
 (0)