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.
- Loading branch information
1 parent
c6aba18
commit 6a92d52
Showing
1 changed file
with
278 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,278 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "Tce3stUlHN0L" | ||
}, | ||
"source": [ | ||
"##### Copyright 2024 Google LLC." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"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": "084u8u0DpBlo" | ||
}, | ||
"source": [ | ||
"# Prompting with an Apollo 11 transcript" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "wnQ_LVlzIeXo" | ||
}, | ||
"source": [ | ||
"<table align=\"left\">\n", | ||
" <td>\n", | ||
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Video.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", | ||
" </td>\n", | ||
"</table>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "q7QvXQMrIhuZ" | ||
}, | ||
"source": [ | ||
"This notebook provides a quick example of how to prompt Gemini 1.5 Pro using a text file. In this case, you'll use a 400 page transcript from [Apollo 11](https://www.nasa.gov/history/alsj/a11/a11trans.html)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"metadata": { | ||
"id": "qLuL9m7KhvxR" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -U -q google-generativeai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"metadata": { | ||
"id": "ATIbQM0NHhkj" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import google.generativeai as genai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "l8g4hTRotheH" | ||
}, | ||
"source": [ | ||
"### Setup 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 [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"metadata": { | ||
"id": "d6lYXRcjthKV" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from google.colab import userdata\n", | ||
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", | ||
"genai.configure(api_key=GOOGLE_API_KEY)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "MNvhBdoDFnTC" | ||
}, | ||
"source": [ | ||
"Download the transcript." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 22, | ||
"metadata": { | ||
"id": "V4XeFdX1rxaE" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"--2024-05-14 20:12:01-- https://storage.googleapis.com/generativeai-downloads/data/a11.txt\n", | ||
"Resolving storage.googleapis.com (storage.googleapis.com)... 142.250.99.207, 142.250.107.207, 173.194.202.207, ...\n", | ||
"Connecting to storage.googleapis.com (storage.googleapis.com)|142.250.99.207|:443... connected.\n", | ||
"HTTP request sent, awaiting response... 200 OK\n", | ||
"Length: 847790 (828K) [text/plain]\n", | ||
"Saving to: ‘a11.txt.2’\n", | ||
"\n", | ||
"\ra11.txt.2 0%[ ] 0 --.-KB/s \ra11.txt.2 100%[===================>] 827.92K --.-KB/s in 0.009s \n", | ||
"\n", | ||
"2024-05-14 20:12:01 (90.5 MB/s) - ‘a11.txt.2’ saved [847790/847790]\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "ZoiKgO2CswzA" | ||
}, | ||
"source": [ | ||
"Prepare it for use in a prompt." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 23, | ||
"metadata": { | ||
"id": "_HzrDdp2Q1Cu" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Uploading file...\n", | ||
"Completed upload: https://generativelanguage.googleapis.com/v1beta/files/7w6ag8dszigf\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"text_file_name = \"a11.txt\"\n", | ||
"print(f\"Uploading file...\")\n", | ||
"text_file = genai.upload_file(path=text_file_name)\n", | ||
"print(f\"Completed upload: {text_file.uri}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "EPPOECHzsIGJ" | ||
}, | ||
"source": [ | ||
"## Generate Content\n", | ||
"\n", | ||
"After the file has been uploaded, you can make `GenerateContent` requests that reference the File API URI. We'll ask the model to find a few lighthearted moments." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"metadata": { | ||
"id": "ZYVFqmLkl5nE" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"This is a very technical document and doesn't contain many explicitly lighthearted moments, but here are four instances where humor can be found:\n", | ||
"\n", | ||
"1. **00 00 03 36 CDR\n", | ||
"Yes. They finally gave me a window to look out.** - Neil Armstrong's dry humor about finally getting a window to see out of is a bit of lightheartedness during a very serious procedure.\n", | ||
"\n", | ||
"2. **00 05 20 31 CMP\n", | ||
"If we're late in answering you, it's because we're munching sandwiches.** - This comment from Michael Collins shows the astronauts taking a moment to enjoy a snack, even amidst their mission duties.\n", | ||
"\n", | ||
"3. **01 03 23 00 CC\n", | ||
"(Laughing.) A lot of gray paint.** - The capsule communicator's laughter at Buzz Aldrin's question about a possible Navy term for the reflection of the sun on the ocean is a lighthearted moment of shared humor.\n", | ||
"\n", | ||
"4. **02 08 03 53 CC\n", | ||
"Roger. We haven't quite got that before the 50,000,000 TV audience, yet.** - This humorous remark by the capsule communicator in response to Buzz Aldrin's comment about his pants falling down is another example of shared humor and a lighthearted moment during the LM inspection.\n", | ||
"\n", | ||
"These moments, though subtle, offer a glimpse into the camaraderie and good humor of the astronauts and ground crew, even under the immense pressure of the Apollo 11 mission. \n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"prompt = \"Find four lighthearted moments in this text file.\"\n", | ||
"\n", | ||
"model = genai.GenerativeModel(model_name=\"models/gemini-1.5-pro-latest\")\n", | ||
"\n", | ||
"response = model.generate_content([prompt, text_file],\n", | ||
" request_options={\"timeout\": 600})\n", | ||
"print(response.text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "IrPDYdQSKTg4" | ||
}, | ||
"source": [ | ||
"## Delete File\n", | ||
"\n", | ||
"Files are automatically deleted after 2 days or you can manually delete them using `files.delete()`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"metadata": { | ||
"id": "d4eO8ZXoKdZf" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"genai.delete_file(text_file.name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "K5oUCqb6IUnH" | ||
}, | ||
"source": [ | ||
"## Learning more\n", | ||
"\n", | ||
"The File API accepts files under 2GB in size and can store up to 20GB of files per project. Learn more about the [File API](https://github.com/google-gemini/cookbook/blob/main/quickstarts/File_API.ipynb) here." | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"name": "Apollo_11.ipynb", | ||
"toc_visible": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |