-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding new Gemini quickstart Colab (#319)
- Loading branch information
Showing
1 changed file
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "Tce3stUlHN0L" | ||
}, | ||
"source": [ | ||
"##### Copyright 2024 Google LLC." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"cellView": "form", | ||
"id": "tuOe1ymfHZPu" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", | ||
"# you may not use this file except in compliance with the License.\n", | ||
"# You may obtain a copy of the License at\n", | ||
"#\n", | ||
"# https://www.apache.org/licenses/LICENSE-2.0\n", | ||
"#\n", | ||
"# Unless required by applicable law or agreed to in writing, software\n", | ||
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n", | ||
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | ||
"# See the License for the specific language governing permissions and\n", | ||
"# limitations under the License." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "-QhPWE1lwZHH" | ||
}, | ||
"source": [ | ||
"# Gemini API Python quickstart\n", | ||
"\n", | ||
"This tutorial shows you how to get started with the Gemini API using the Python SDK." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "NNNg43Ymw54e" | ||
}, | ||
"source": [ | ||
"## Prerequisites\n", | ||
"\n", | ||
"You can run this tutorial in Google Colab, which doesn't require additional environment configuration.\n", | ||
"\n", | ||
"Alternatively, to complete this quickstart locally, see the Python guidance in [Get started with the Gemini API](https://ai.google.dev/tutorials/quickstart)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "kHkHARdb1ZID" | ||
}, | ||
"source": [ | ||
"## Install the SDK\n", | ||
"\n", | ||
"The Python SDK for the Gemini API is contained in the [`google-generativeai`](https://pypi.org/project/google-generativeai/) package. Install the dependency using pip:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "J6Pd9SFJ1yVi" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -q -U google-generativeai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "EeMCtmx9ykyx" | ||
}, | ||
"source": [ | ||
"## Set up your API key\n", | ||
"\n", | ||
"To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio.\n", | ||
"\n", | ||
"<a class=\"button\" href=\"https://aistudio.google.com/app/apikey\" target=\"_blank\" rel=\"noopener noreferrer\">Get an API key</a>\n", | ||
"\n", | ||
"In Colab, add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`. Then pass the key to the SDK:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "HTiaTu6O1LRC" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import the Python SDK\n", | ||
"import google.generativeai as genai\n", | ||
"# Used to securely store your API key\n", | ||
"from google.colab import userdata\n", | ||
"\n", | ||
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", | ||
"genai.configure(api_key=GOOGLE_API_KEY)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "CZPYk29o2No0" | ||
}, | ||
"source": [ | ||
"## Initialize the Generative Model\n", | ||
"\n", | ||
"Before you can make any API calls, you need to initialize the Generative Model." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "s-JqXcDe2hZ_" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = genai.GenerativeModel('gemini-pro')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "nXxypzJH4MUl" | ||
}, | ||
"source": [ | ||
"## Generate text" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "j51mcrLD4Y2W" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"response = model.generate_content(\"Write a story about a magic backpack.\")\n", | ||
"print(response.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "zUUAQS9u4biH" | ||
}, | ||
"source": [ | ||
"## What's next\n", | ||
"\n", | ||
"To learn more about working with the Gemini API, see the [Python tutorial](https://ai.google.dev/tutorials/python_quickstart).\n", | ||
"\n", | ||
"If you're new to generative AI models, you might want to look at the\n", | ||
"[concepts guide](https://ai.google.dev/docs/concepts) and the\n", | ||
"[Gemini API overview](https://ai.google.dev/docs/gemini_api_overview)." | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"name": "quickstart_colab.ipynb", | ||
"toc_visible": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |