|
| 1 | +--- |
| 2 | +id: vertex |
| 3 | +title: Vertex AI |
| 4 | +sidebar_position: 3.23 |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | + |
| 11 | +You can deploy the following Mistral AI models from Google Cloud Vertex AI's Model Garden: |
| 12 | + |
| 13 | +- Mistral NeMo |
| 14 | +- Codestral (instruct and FIM modes) |
| 15 | +- Mistral Large |
| 16 | + |
| 17 | +## Pre-requisites |
| 18 | + |
| 19 | +In order to query the model you will need: |
| 20 | + |
| 21 | +- Access to a Google Cloud Project with the Vertex AI API enabled |
| 22 | +- Relevant IAM permissions to be able to enable the model and query endpoints through the following roles: |
| 23 | + - [Vertex AI User IAM role](https://cloud.google.com/vertex-ai/docs/general/access-control#aiplatform.user). |
| 24 | + - Consumer Procurement Entitlement Manager role |
| 25 | + |
| 26 | +On the client side, you will also need: |
| 27 | +- The `gcloud` CLI to authenticate against the Google Cloud APIs, please refer to |
| 28 | +[this page](https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp) |
| 29 | +for more details. |
| 30 | +- A Python virtual environment with the `mistralai-google-cloud` client package installed. |
| 31 | +- The following environment variables properly set up: |
| 32 | + - `GOOGLE_PROJECT_ID`: a Google Cloud Project ID with the the Vertex AI API enabled |
| 33 | + - `GOOGLE_REGION`: a Google Cloud region where Mistral models are available |
| 34 | + (e.g. `europe-west4`) |
| 35 | + |
| 36 | +## Querying the models (instruct mode) |
| 37 | + |
| 38 | + |
| 39 | +<Tabs> |
| 40 | + <TabItem value="python" label="Python"> |
| 41 | + |
| 42 | + ```python |
| 43 | + import httpx |
| 44 | + import google.auth |
| 45 | + from google.auth.transport.requests import Request |
| 46 | + import os |
| 47 | + |
| 48 | + |
| 49 | + def get_credentials() -> str: |
| 50 | + credentials, project_id = google.auth.default( |
| 51 | + scopes=["https://www.googleapis.com/auth/cloud-platform"] |
| 52 | + ) |
| 53 | + credentials.refresh(Request()) |
| 54 | + return credentials.token |
| 55 | + |
| 56 | + |
| 57 | + def build_endpoint_url( |
| 58 | + region: str, |
| 59 | + project_id: str, |
| 60 | + model_name: str, |
| 61 | + model_version: str, |
| 62 | + streaming: bool = False, |
| 63 | + ) -> str: |
| 64 | + base_url = f"https://{region}-aiplatform.googleapis.com/v1/" |
| 65 | + project_fragment = f"projects/{project_id}" |
| 66 | + location_fragment = f"locations/{region}" |
| 67 | + specifier = "streamRawPredict" if streaming else "rawPredict" |
| 68 | + model_fragment = f"publishers/mistralai/models/{model_name}@{model_version}" |
| 69 | + url = f"{base_url}{'/'.join([project_fragment, location_fragment, model_fragment])}:{specifier}" |
| 70 | + return url |
| 71 | + |
| 72 | + |
| 73 | + # Retrieve Google Cloud Project ID and Region from environment variables |
| 74 | + project_id = os.environ.get("GOOGLE_PROJECT_ID") |
| 75 | + region = os.environ.get("GOOGLE_REGION") |
| 76 | + |
| 77 | + # Retrieve Google Cloud credentials. |
| 78 | + access_token = get_credentials() |
| 79 | + |
| 80 | + model = "mistral-nemo" # Replace with the model you want to use |
| 81 | + model_version = "2407" # Replace with the model version you want to use |
| 82 | + is_streamed = False # Change to True to stream token responses |
| 83 | + |
| 84 | + # Build URL |
| 85 | + url = build_endpoint_url( |
| 86 | + project_id=project_id, |
| 87 | + region=region, |
| 88 | + model_name=model, |
| 89 | + model_version=model_version, |
| 90 | + streaming=is_streamed |
| 91 | + ) |
| 92 | + |
| 93 | + # Define query headers |
| 94 | + headers = { |
| 95 | + "Authorization": f"Bearer {access_token}", |
| 96 | + "Accept": "application/json", |
| 97 | + } |
| 98 | + |
| 99 | + # Define POST payload |
| 100 | + data = { |
| 101 | + "model": model, |
| 102 | + "messages": [{"role": "user", "content": "Who is the best French painter?"}], |
| 103 | + "stream": is_streamed, |
| 104 | + } |
| 105 | + # Make the call |
| 106 | + with httpx.Client() as client: |
| 107 | + resp = client.post(url, json=data, headers=headers, timeout=None) |
| 108 | + print(resp.text) |
| 109 | + |
| 110 | + ``` |
| 111 | + |
| 112 | + </TabItem> |
| 113 | + <TabItem value="curl" label="cURL"> |
| 114 | + |
| 115 | + ```bash |
| 116 | + MODEL="mistral-nemo" |
| 117 | + MODEL_VERSION="2407" |
| 118 | + |
| 119 | + url="https://$GOOGLE_REGION-aiplatform.googleapis.com/v1/projects/$GOOGLE_PROJECT_ID/locations/$GOOGLE_REGION/publishers/mistralai/models/$MODEL@$MODEL_VERSION:rawPredict" |
| 120 | + |
| 121 | + curl \ |
| 122 | + -X POST \ |
| 123 | + -H "Authorization: Bearer $(gcloud auth print-access-token)" \ |
| 124 | + -H "Content-Type: application/json" \ |
| 125 | + $url \ |
| 126 | + --data '{ |
| 127 | + "model": "'"$MODEL"'", |
| 128 | + "temperature": 0, |
| 129 | + "messages": [ |
| 130 | + {"role": "user", "content": "What is the best French cheese?"} |
| 131 | + ] |
| 132 | + }' |
| 133 | + |
| 134 | + ``` |
| 135 | + </TabItem> |
| 136 | +</Tabs> |
| 137 | + |
| 138 | +## Querying Codestral in FIM mode |
| 139 | + |
| 140 | + |
| 141 | +<Tabs> |
| 142 | + <TabItem value="python" label="Python"> |
| 143 | + |
| 144 | + ```python |
| 145 | + import httpx |
| 146 | + import google.auth |
| 147 | + from google.auth.transport.requests import Request |
| 148 | + import os |
| 149 | + |
| 150 | + |
| 151 | + def get_credentials() -> str: |
| 152 | + credentials, project_id = google.auth.default( |
| 153 | + scopes=["https://www.googleapis.com/auth/cloud-platform"] |
| 154 | + ) |
| 155 | + credentials.refresh(Request()) |
| 156 | + return credentials.token |
| 157 | + |
| 158 | + |
| 159 | + def build_endpoint_url( |
| 160 | + region: str, |
| 161 | + project_id: str, |
| 162 | + model_name: str, |
| 163 | + model_version: str, |
| 164 | + streaming: bool = False, |
| 165 | + ) -> str: |
| 166 | + base_url = f"https://{region}-aiplatform.googleapis.com/v1/" |
| 167 | + project_fragment = f"projects/{project_id}" |
| 168 | + location_fragment = f"locations/{region}" |
| 169 | + specifier = "streamRawPredict" if streaming else "rawPredict" |
| 170 | + model_fragment = f"publishers/mistralai/models/{model_name}@{model_version}" |
| 171 | + url = f"{base_url}{'/'.join([project_fragment, location_fragment, model_fragment])}:{specifier}" |
| 172 | + return url |
| 173 | + |
| 174 | + |
| 175 | + # Retrieve Google Cloud Project ID and Region from environment variables |
| 176 | + project_id = os.environ.get("GOOGLE_PROJECT_ID") |
| 177 | + region = os.environ.get("GOOGLE_REGION") |
| 178 | + |
| 179 | + # Retrieve Google Cloud credentials. |
| 180 | + access_token = get_credentials() |
| 181 | + |
| 182 | + model = "codestral" |
| 183 | + model_version = "2405" |
| 184 | + is_streamed = False # Change to True to stream token responses |
| 185 | + |
| 186 | + # Build URL |
| 187 | + url = build_endpoint_url( |
| 188 | + project_id=project_id, |
| 189 | + region=region, |
| 190 | + model_name=model, |
| 191 | + model_version=model_version, |
| 192 | + streaming=is_streamed |
| 193 | + ) |
| 194 | + |
| 195 | + # Define query headers |
| 196 | + headers = { |
| 197 | + "Authorization": f"Bearer {access_token}", |
| 198 | + "Accept": "application/json", |
| 199 | + } |
| 200 | + |
| 201 | + # Define POST payload |
| 202 | + data = { |
| 203 | + "model": model, |
| 204 | + "prompt": "def say_hello(name: str) -> str:", |
| 205 | + "suffix": "return n_words" |
| 206 | + } |
| 207 | + # Make the call |
| 208 | + with httpx.Client() as client: |
| 209 | + resp = client.post(url, json=data, headers=headers, timeout=None) |
| 210 | + print(resp.text) |
| 211 | + |
| 212 | + |
| 213 | + ``` |
| 214 | + |
| 215 | + </TabItem> |
| 216 | + <TabItem value="curl" label="cURL"> |
| 217 | + |
| 218 | + ```bash |
| 219 | + MODEL="codestral" |
| 220 | + MODEL_VERSION="2405" |
| 221 | + |
| 222 | + url="https://$GOOGLE_REGION-aiplatform.googleapis.com/v1/projects/$GOOGLE_PROJECT_ID/locations/$GOOGLE_REGION/publishers/mistralai/models/$MODEL@$MODEL_VERSION:rawPredict" |
| 223 | + |
| 224 | + |
| 225 | + curl \ |
| 226 | + -X POST \ |
| 227 | + -H "Authorization: Bearer $(gcloud auth print-access-token)" \ |
| 228 | + -H "Content-Type: application/json" \ |
| 229 | + $url \ |
| 230 | + --data '{ |
| 231 | + "model":"'"$MODEL"'", |
| 232 | + "prompt": "def count_words_in_file(file_path: str) -> int:", |
| 233 | + "suffix": "return n_words" |
| 234 | + }' |
| 235 | + |
| 236 | + ``` |
| 237 | + </TabItem> |
| 238 | +</Tabs> |
| 239 | + |
| 240 | + |
| 241 | +## Going further |
| 242 | + |
| 243 | +For more information and examples, you can check: |
| 244 | + |
| 245 | +- The Google Cloud [Partner Models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/mistral) |
| 246 | + documentation page. |
| 247 | +- The Vertex Model Cards for [Mistral Large](https://console.cloud.google.com/vertex-ai/publishers/mistralai/model-garden/mistral-large), |
| 248 | + [Mistral-NeMo](https://console.cloud.google.com/vertex-ai/publishers/mistralai/model-garden/mistral-nemo) and |
| 249 | + [Codestral](https://console.cloud.google.com/vertex-ai/publishers/mistralai/model-garden/codestral). |
| 250 | +- The [Getting Started Colab Notebook](https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/generative_ai/mistralai_intro.ipynb) |
| 251 | + for Mistral models on Vertex, along with the [source file on GitHub](https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/generative_ai/mistralai_intro.ipynb). |
| 252 | + |
0 commit comments