GeezerVison is showing github.com — chore: Add evaluation section to Gemini docs. Update SDK doc generati… · hamgor/python-aiplatform@9cb0819
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/conf.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -416,6 +416,7 @@ def setup(*args, **kwargs): | 416 | 416 | # 2. Giving pretty class names to the preview classes | 417 | 417 | # 3. Making Sphinx automodule render the class members instead of | 418 | 418 | # dismissing the exported private classes as "Alias of". | | 419 | + from vertexai import evaluation | 419 | 420 | from vertexai import language_models | 420 | 421 | from vertexai import vision_models | 421 | 422 | from vertexai.preview import ( | @@ -429,6 +430,7 @@ def setup(*args, **kwargs): | 429 | 430 | # We select the publicly exported members that have an internal module ("*._*"). | 430 | 431 |
| 431 | 432 | # Setting the modules of the GA classes | | 433 | + adopt_members_reexported_from_private_modules(evaluation) | 432 | 434 | adopt_members_reexported_from_private_modules(language_models) | 433 | 435 | adopt_members_reexported_from_private_modules(vision_models) | 434 | 436 |
| |
Collapse file Copy file name to clipboardExpand all lines: gemini_docs/README.md +131Lines changed: 131 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -175,6 +175,137 @@ chat = model.start_chat(responder=afc_responder) | 175 | 175 | print(chat.send_message("What is the weather like in Boston?")) | 176 | 176 | ``` | 177 | 177 |
| | 178 | +#### Evaluation | | 179 | + | | 180 | +- To perform bring-your-own-response(BYOR) evaluation, provide the model responses in the `response` column in the dataset. If a pairwise metric is used for BYOR evaluation, provide the baseline model responses in the `baseline_model_response` column. | | 181 | + | | 182 | +``` | | 183 | +import pandas as pd | | 184 | +from vertexai.evaluation import EvalTask, MetricPromptTemplateExamples | | 185 | +
| | 186 | +eval_dataset = pd.DataFrame({ | | 187 | + "prompt" : [...], | | 188 | + "reference": [...], | | 189 | + "response" : [...], | | 190 | + "baseline_model_response": [...], | | 191 | +}) | | 192 | +eval_task = EvalTask( | | 193 | + dataset=eval_dataset, | | 194 | + metrics=[ | | 195 | + "bleu", | | 196 | + "rouge_l_sum", | | 197 | + MetricPromptTemplateExamples.Pointwise.FLUENCY, | | 198 | + MetricPromptTemplateExamples.Pairwise.SAFETY | | 199 | + ], | | 200 | + experiment="my-experiment", | | 201 | +) | | 202 | +eval_result = eval_task.evaluate(experiment_run_name="eval-experiment-run") | | 203 | +``` | | 204 | +- To perform evaluation with Gemini model inference, specify the `model` parameter with a `GenerativeModel` instance. The input column name to the model is `prompt` and must be present in the dataset. | | 205 | + | | 206 | +``` | | 207 | +from vertexai.evaluation import EvalTask | | 208 | +from vertexai.generative_models import GenerativeModel | | 209 | +
| | 210 | +eval_dataset = pd.DataFrame({ | | 211 | + "reference": [...], | | 212 | + "prompt" : [...], | | 213 | +}) | | 214 | +result = EvalTask( | | 215 | + dataset=eval_dataset, | | 216 | + metrics=["exact_match", "bleu", "rouge_1", "rouge_l_sum"], | | 217 | + experiment="my-experiment", | | 218 | +).evaluate( | | 219 | + model=GenerativeModel("gemini-1.5-pro"), | | 220 | + experiment_run_name="gemini-eval-run" | | 221 | +) | | 222 | +``` | | 223 | + | | 224 | +- If a `prompt_template` is specified, the `prompt` column is not required. Prompts can be assembled from the evaluation dataset, and all prompt template variable names must be present in the dataset columns. | | 225 | + | | 226 | +``` | | 227 | +import pandas as pd | | 228 | +from vertexai.evaluation import EvalTask, MetricPromptTemplateExamples | | 229 | +from vertexai.generative_models import GenerativeModel | | 230 | +
| | 231 | +eval_dataset = pd.DataFrame({ | | 232 | + "context" : [...], | | 233 | + "instruction": [...], | | 234 | +}) | | 235 | +result = EvalTask( | | 236 | + dataset=eval_dataset, | | 237 | + metrics=[MetricPromptTemplateExamples.Pointwise.SUMMARIZATION_QUALITY], | | 238 | +).evaluate( | | 239 | + model=GenerativeModel("gemini-1.5-pro"), | | 240 | + prompt_template="{instruction}. Article: {context}. Summary:", | | 241 | +) | | 242 | +``` | | 243 | + | | 244 | +- To perform evaluation with custom model inference, specify the `model` | | 245 | +parameter with a custom inference function. The input column name to the | | 246 | +custom inference function is `prompt` and must be present in the dataset. | | 247 | + | | 248 | +``` | | 249 | +from openai import OpenAI | | 250 | +from vertexai.evaluation import EvalTask, MetricPromptTemplateExamples | | 251 | +
| | 252 | +
| | 253 | +client = OpenAI() | | 254 | +def custom_model_fn(input: str) -> str: | | 255 | + response = client.chat.completions.create( | | 256 | + model="gpt-3.5-turbo", | | 257 | + messages=[ | | 258 | + {"role": "user", "content": input} | | 259 | + ] | | 260 | + ) | | 261 | + return response.choices[0].message.content | | 262 | +
| | 263 | +eval_dataset = pd.DataFrame({ | | 264 | + "prompt" : [...], | | 265 | + "reference": [...], | | 266 | +}) | | 267 | +result = EvalTask( | | 268 | + dataset=eval_dataset, | | 269 | + metrics=[MetricPromptTemplateExamples.Pointwise.SAFETY], | | 270 | + experiment="my-experiment", | | 271 | +).evaluate( | | 272 | + model=custom_model_fn, | | 273 | + experiment_run_name="gpt-eval-run" | | 274 | +) | | 275 | +``` | | 276 | + | | 277 | +- To perform pairwise metric evaluation with model inference step, specify | | 278 | +the `baseline_model` input to a `PairwiseMetric` instance and the candidate | | 279 | +`model` input to the `EvalTask.evaluate()` function. The input column name | | 280 | +to both models is `prompt` and must be present in the dataset. | | 281 | + | | 282 | +``` | | 283 | +import pandas as pd | | 284 | +from vertexai.evaluation import EvalTask, MetricPromptTemplateExamples, PairwiseMetric | | 285 | +from vertexai.generative_models import GenerativeModel | | 286 | +
| | 287 | +baseline_model = GenerativeModel("gemini-1.0-pro") | | 288 | +candidate_model = GenerativeModel("gemini-1.5-pro") | | 289 | +
| | 290 | +pairwise_groundedness = PairwiseMetric( | | 291 | + metric_prompt_template=MetricPromptTemplateExamples.get_prompt_template( | | 292 | + "pairwise_groundedness" | | 293 | + ), | | 294 | + baseline_model=baseline_model, | | 295 | +) | | 296 | +eval_dataset = pd.DataFrame({ | | 297 | + "prompt" : [...], | | 298 | +}) | | 299 | +result = EvalTask( | | 300 | + dataset=eval_dataset, | | 301 | + metrics=[pairwise_groundedness], | | 302 | + experiment="my-pairwise-experiment", | | 303 | +).evaluate( | | 304 | + model=candidate_model, | | 305 | + experiment_run_name="gemini-pairwise-eval-run", | | 306 | +) | | 307 | +``` | | 308 | + | 178 | 309 | ## Documentation | 179 | 310 |
| 180 | 311 | You can find complete documentation for the Vertex AI SDKs and the Gemini model in the Google Cloud [documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/learn/overview) | |
Collapse file Copy file name to clipboardExpand all lines: gemini_docs/conf.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -416,6 +416,7 @@ def setup(*args, **kwargs): | 416 | 416 | # 2. Giving pretty class names to the preview classes | 417 | 417 | # 3. Making Sphinx automodule render the class members instead of | 418 | 418 | # dismissing the exported private classes as "Alias of". | | 419 | + from vertexai import evaluation | 419 | 420 | from vertexai import language_models | 420 | 421 | from vertexai import vision_models | 421 | 422 | from vertexai.preview import ( | @@ -429,6 +430,7 @@ def setup(*args, **kwargs): | 429 | 430 | # We select the publicly exported members that have an internal module ("*._*"). | 430 | 431 |
| 431 | 432 | # Setting the modules of the GA classes | | 433 | + adopt_members_reexported_from_private_modules(evaluation) | 432 | 434 | adopt_members_reexported_from_private_modules(language_models) | 433 | 435 | adopt_members_reexported_from_private_modules(vision_models) | 434 | 436 |
| |
Collapse file Copy file name to clipboardExpand all lines: gemini_docs/vertexai/services.rst +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ Vertex AI SDK | 45 | 45 | :show-inheritance: | 46 | 46 | :inherited-members: | 47 | 47 |
| 48 | | -.. automodule:: vertexai.preview.evaluation | | 48 | +.. automodule:: vertexai.evaluation | 49 | 49 | :members: | 50 | 50 | :show-inheritance: | 51 | 51 | :inherited-members: | |
Collapse file Copy file name to clipboardExpand all lines: noxfile.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -351,6 +351,8 @@ def docs(session): | 351 | 351 | session.install( | 352 | 352 | "sphinx==5.0.2", | 353 | 353 | "alabaster", | | 354 | + "immutabledict", | | 355 | + "google-cloud-aiplatform[evaluation]", | 354 | 356 | "google-cloud-aiplatform[prediction]", | 355 | 357 | "recommonmark", | 356 | 358 | ) | @@ -420,6 +422,8 @@ def gemini_docs(session): | 420 | 422 | session.install( | 421 | 423 | "sphinx==5.0.2", | 422 | 424 | "alabaster", | | 425 | + "immutabledict", | | 426 | + "google-cloud-aiplatform[evaluation]", | 423 | 427 | "google-cloud-aiplatform[prediction]", | 424 | 428 | "recommonmark", | 425 | 429 | ) | |
Collapse file Copy file name to clipboardExpand all lines: setup.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 152 | 152 | evaluation_extra_require = [ | 153 | 153 | "pandas >= 1.0.0, < 2.2.0", | 154 | 154 | "tqdm>=4.23.0", | | 155 | + "immutabledict", | 155 | 156 | ] | 156 | 157 |
| 157 | 158 | langchain_extra_require = [ | |
|