|
| 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] |
0 commit comments