forked from google-gemini/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a template notebook. (google-gemini#263)
This is for new contributors or Googlers to use as a base for new quickstarts or examples. It's largely forked from the [TensorFlow template](https://github.com/tensorflow/docs/blob/master/tools/templates/notebook.ipynb), but I've updated the guidance to apply to the Gemini API and this cookbook.
- Loading branch information
Showing
3 changed files
with
240 additions
and
2 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
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
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,232 @@ | ||
{ | ||
"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": "qFdPvlXBOdUN" | ||
}, | ||
"source": [ | ||
"# Gemini API: Name of your guide" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "MfBg1C5NB3X0" | ||
}, | ||
"source": [ | ||
"<table align=\"left\">\n", | ||
" <td>\n", | ||
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/templates/Template.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", | ||
" </td>\n", | ||
"</table>\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "r6P32iYYV27b" | ||
}, | ||
"source": [ | ||
"_[remove me] Be sure to update the Colab link!_" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "xHxb-dlhMIzW" | ||
}, | ||
"source": [ | ||
"[Include a paragraph or two here explaining what this example demonstrates, who should be interested in it, and what you need to know before you get started.]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "AQJjzmYgH3sX" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -U -q google-generativeai # Install the Python SDK\n", | ||
"\n", | ||
"# If you are relying on a new feature, be sure to include the minimum\n", | ||
"# SDK version here too - e.g. 'google-generativeai>=0.9.9'." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "iQSKjF5WH5N9" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import google.generativeai as genai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "MUXex9ctTuDB" | ||
}, | ||
"source": [ | ||
"## Set up your API key\n", | ||
"\n", | ||
"To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see the [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) quickstart for an example." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "wltbMJLIIXGk" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"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": "w5i8tMZKIeUu" | ||
}, | ||
"source": [ | ||
"## [Write your guide]\n", | ||
"\n", | ||
"[Add as many high level sections as needed to step through your guide. Try to introduce new concepts incrementally, and remember that notebooks need to be executable from start to finish using `Runtime -> Run all` in Colab.]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "UhNtHfuxCGVy" | ||
}, | ||
"source": [ | ||
"## Resources\n", | ||
"\n", | ||
"* Follow the [Google developer documentation style guide](https://developers.google.com/style/highlights)\n", | ||
"* The [TensorFlow documentation style guide](https://www.tensorflow.org/community/contribute/docs_style) has useful guidance for notebooks.\n", | ||
"* Read the [Cookbook contributor guide](https://github.com/google-gemini/cookbook/blob/main/CONTRIBUTING.md) and the [Cookbook Examples contributor guide](https://github.com/google-gemini/cookbook/blob/main/examples/CONTRIBUTING.md)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "2V22fKegUtF9" | ||
}, | ||
"source": [ | ||
"## Notebook style\n", | ||
"\n", | ||
"* Include the collapsed license at the top (uses the Colab \"Form\" mode to hide the cells).\n", | ||
"* Save the notebook with the table of contents open.\n", | ||
"* Use one `H1` header for the title.\n", | ||
"* Include the button-bar immediately after the `H1`.\n", | ||
"* Include an overview section before any code.\n", | ||
"* Put all your installs and imports in a setup section.\n", | ||
"* Keep code and text cells as brief as possible.\n", | ||
"* Break text cells at headings\n", | ||
"* Break code cells between \"building\" and \"running\", and between \"printing one result\" and \"printing another result\".\n", | ||
"* Necessary but uninteresting code should be hidden in a toggleable code cell by putting `# @title` as the first line." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "QKp40qS-DGEZ" | ||
}, | ||
"source": [ | ||
"### Code style\n", | ||
"\n", | ||
"* Notebooks are for people. Write code optimized for clarity.\n", | ||
"* Keep examples quick and concise.\n", | ||
"* Use the [Google Python Style Guide](http://google.github.io/styleguide/pyguide.html), where applicable. Code formatted by [`pyink`](https://github.com/google/pyink) will always be accepted.\n", | ||
"* If you define a function, run it and show us what it does before using it in another function.\n", | ||
"* Demonstrate small parts before combining them into something more complex.\n", | ||
"* To ensure notebook text remains accurate, present model metadata by executing code.\n", | ||
" * For example, instead of saying \"1M token context\" in the text, display the output of `genai.get_model('models/...').input_token_limit`.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "78HBT9cQXJko" | ||
}, | ||
"source": [ | ||
"### Text\n", | ||
"\n", | ||
"* Use an imperative style. \"Run a prompt using the API.\"\n", | ||
"* Use sentence case in titles/headings.\n", | ||
"* Use short titles/headings: \"Download the data\", \"Call the API\", \"Process the results\".\n", | ||
"* Use the [Google developer documentation style guide](https://developers.google.com/style/highlights).\n", | ||
"* Use [second person](https://developers.google.com/style/person): \"you\" rather than \"we\".\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "YrsKXcPRUvK9" | ||
}, | ||
"source": [ | ||
"## GitHub workflow\n", | ||
"\n", | ||
"* Be consistent about how you save your notebooks, otherwise the JSON diffs are messy. [`nbfmt` and `nblint`](https://github.com/tensorflow/docs/blob/master/tools/tensorflow_docs/tools/README.md) help here.\n", | ||
"* This notebook has the \"Omit code cell output when saving this notebook\" option set. GitHub refuses to diff notebooks with large diffs (inline images).\n", | ||
"* [ReviewNB.com](http://reviewnb.com) can help with diffs. This is linked in a comment on a notebook pull request.\n", | ||
"* Use the [Open in Colab](https://chrome.google.com/webstore/detail/open-in-colab/iogfkhleblhcpcekbiedikdehleodpjo) extension to open a GitHub notebook in Colab.\n", | ||
"* The easiest way to edit a notebook in GitHub is to open it with Colab from the branch you want to edit. Then use File --> Save a copy in GitHub, which will save it back to the branch you opened it from.\n", | ||
"* For PRs it's helpful to post a direct Colab link to the PR head: https://colab.research.google.com/github/{USER}/{REPO}/blob/{BRANCH}/{PATH}.ipynb" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"collapsed_sections": [ | ||
"Tce3stUlHN0L" | ||
], | ||
"name": "Template.ipynb", | ||
"toc_visible": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |