Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: google-gemini/deprecated-generative-ai-python
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.8.3
Choose a base ref
...
head repository: google-gemini/deprecated-generative-ai-python
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.8.4
Choose a head ref
  • 11 commits
  • 303 files changed
  • 6 contributors

Commits on Oct 8, 2024

  1. feat: add py.typed (#509)

    * Update setup.py
    
    * Create py.typed
    
    * Update setup.py to trigger CI.
    
    ---------
    
    Co-authored-by: Mark Daoust <markdaoust@google.com>
    Goldziher and MarkDaoust authored Oct 8, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c8eadc4 View commit details

Commits on Oct 16, 2024

  1. Remove references to Pro 1.0 (#600)

    * remove references to 1.0-pro
    
    Change-Id: I405c87d495c73550cfbd00a13249cb1e30ab0989
    
    * remove references to gemini-pro
    
    Change-Id: Ied2f0b7112dd5d61390da3e84457a2fb3f770665
    
    * Update models.py
    
    * format
    
    Change-Id: Ib3a0c90bfc6ec7f8f793917b3140769e2635a8e9
    MarkDaoust authored Oct 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e9b0cde View commit details

Commits on Oct 24, 2024

  1. Fix GenerationConfig to match the API. (#609)

    * Fix GenerationConfig, this doesn't match the API
    
    Change-Id: I4e0c3b45125023e056c3d7e1549eafb47e42815a
    
    * format
    
    Change-Id: I161dd7b9b371e005b4385faeaa84f6d141e09b43
    MarkDaoust authored Oct 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    3d91916 View commit details
  2. fix: thread-safe discovery API setup (Issues#327) (#583)

    Sumeet213 authored Oct 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    db8221a View commit details

Commits on Oct 29, 2024

  1. Use double quotes for JSON object keys (#612)

    westonruter authored Oct 29, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c16df61 View commit details
  2. Add whitespace to generated tables to fix markdown in tables. (#602)

    * fixes 563
    
    Change-Id: Ifb9743822c744845aac4deea9963a0c6e6389362
    
    * Refresh docs for v0.8.3
    
    Change-Id: I7536e809b9a1b8f26fc970a42c83000b0d607793
    
    * remove align-left
    
    Change-Id: I0462300f198e3de020cbfc032e575e094b88d360
    
    * format
    
    Change-Id: If83605c320d2b7255618737c2cd985a153584343
    
    * refresh
    
    Change-Id: I0776467ac6471b2f8f06f089a54e447d149b7d78
    
    * remove devsite headers
    
    Change-Id: I2d10299b5cffc2e353b71dacfabb3a7e9f22a9fd
    
    * remove devsite headers
    
    Change-Id: I1b9ca5bf2d46dbd39b8cb226d153107851e2e3f0
    MarkDaoust authored Oct 29, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    0e5c5f2 View commit details

Commits on Nov 16, 2024

  1. Fix 'argument list too long' error and add couple vision examples (#634)

    vishal-dharm authored Nov 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a04fcd1 View commit details

Commits on Nov 26, 2024

  1. Move imports into samples (#638)

    * Move imports into samples
    
    Change-Id: I86550a09c6555a36679dad517ed4239f7ded90ff
    
    * format
    
    Change-Id: I0c72841eb2ebad0075d0024624a42fe36fef883f
    MarkDaoust authored Nov 26, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4c00257 View commit details
  2. fix: Pass along model version in GenerateContentResponse (#621)

    * fix: Pass along model_version in GenerateContentResponse.
    
    * Revert autogenerated doc files from 94eb16e.
    
    * Fix 'argument list too long' error and add couple vision examples (#634)
    
    * Update google-ai-generativelanguage version in requirements.
    
    * Format updated generation_types and test using black.
    
    ---------
    
    Co-authored-by: Vishal Dharmadhikari <61256217+vishal-dharm@users.noreply.github.com>
    Annhiluc and vishal-dharm authored Nov 26, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    b8772ed View commit details

Commits on Dec 20, 2024

  1. Copy AIS templates from cookbook. (#635)

    https: //github.com/google-gemini/cookbook/tree/e2623e8587f3d4ae13df65f415a3337c7f7ac97e/templates
    Change-Id: Ic50b279a769d2fe2263b1dee41541d7c950c0cfa
    MarkDaoust authored Dec 20, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    5a3ba73 View commit details

Commits on Jan 21, 2025

  1. Update google-ai-generativelanguage version (#665)

    * Update setup.py
    
    * Update version.py
    MarkDaoust authored Jan 21, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    92b287d View commit details
Showing 303 changed files with 9,439 additions and 5,675 deletions.
332 changes: 332 additions & 0 deletions docs/ais-templates/aistudio_gemini_prompt_freeform.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Tce3stUlHN0L"
},
"source": [
"##### Copyright 2023 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": "FKwyTRdwB8aW"
},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RXInneX6xx7c"
},
"outputs": [],
"source": [
"!pip install -U -q \"google-generativeai>=0.8.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "kWIuwKG2_oWE"
},
"outputs": [],
"source": [
"# import necessary modules.\n",
"import base64\n",
"import copy\n",
"import json\n",
"import pathlib\n",
"import requests\n",
"\n",
"\n",
"import PIL.Image\n",
"import IPython.display\n",
"from IPython.display import Markdown\n",
"\n",
"try:\n",
" # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n",
" # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n",
" import os\n",
" from google.colab import userdata\n",
"\n",
" os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n",
"except ImportError:\n",
" pass\n",
"\n",
"import google.generativeai as genai\n",
"\n",
"# Parse the arguments\n",
"\n",
"model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n",
"contents_b64 = \"W3sicGFydHMiOiBbeyJ0ZXh0IjogIldoYXQncyBpbiB0aGlzIHBpY3R1cmU/In0sIHsiZmlsZV9kYXRhIjogeyJ1cmwiOiAiaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL2dlbmVyYXRpdmVhaS1kb3dubG9hZHMvaW1hZ2VzL3Njb25lcy5qcGciLCAibWltZV90eXBlIjogImltYWdlL2pwZWcifX1dfV0=\" # @param {isTemplate: true}\n",
"generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n",
"safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n",
"\n",
"gais_contents = json.loads(base64.b64decode(contents_b64))\n",
"\n",
"generation_config = json.loads(base64.b64decode(generation_config_b64))\n",
"safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n",
"\n",
"stream = False\n",
"\n",
"# Convert and upload the files\n",
"\n",
"tempfiles = pathlib.Path(f\"tempfiles\")\n",
"tempfiles.mkdir(parents=True, exist_ok=True)\n",
"\n",
"\n",
"drive = None\n",
"def upload_file_data(file_data, index):\n",
" \"\"\"Upload files to the Files API.\n",
"\n",
" For each file, Google AI Studio either sent:\n",
" - a Google Drive ID,\n",
" - a URL,\n",
" - a file path, or\n",
" - The raw bytes (`inline_data`).\n",
"\n",
" The API only understands `inline_data` or it's Files API.\n",
" This code, uploads files to the files API where the API can access them.\n",
" \"\"\"\n",
"\n",
" mime_type = file_data[\"mime_type\"]\n",
" if drive_id := file_data.pop(\"drive_id\", None):\n",
" if drive is None:\n",
" from google.colab import drive\n",
" drive.mount(\"/gdrive\")\n",
"\n",
" path = next(\n",
" pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n",
" )\n",
" print(\"Uploading:\", str(path))\n",
" file_info = genai.upload_file(path=path, mime_type=mime_type)\n",
" file_data[\"file_uri\"] = file_info.uri\n",
" return\n",
"\n",
" if url := file_data.pop(\"url\", None):\n",
" response = requests.get(url)\n",
" data = response.content\n",
" name = url.split(\"/\")[-1]\n",
" path = tempfiles / str(index)\n",
" path.write_bytes(data)\n",
" print(\"Uploading:\", url)\n",
" file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n",
" file_data[\"file_uri\"] = file_info.uri\n",
" return\n",
"\n",
" if name := file_data.get(\"filename\", None):\n",
" if not pathlib.Path(name).exists():\n",
" raise IOError(\n",
" f\"local file: `{name}` does not exist. You can upload files \"\n",
" 'to Colab using the file manager (\"📁 Files\" in the left '\n",
" \"toolbar)\"\n",
" )\n",
" file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n",
" file_data[\"file_uri\"] = file_info.uri\n",
" return\n",
"\n",
" if \"inline_data\" in file_data:\n",
" return\n",
"\n",
" raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n",
"\n",
"\n",
"contents = copy.deepcopy(gais_contents)\n",
"\n",
"index = 0\n",
"for content in contents:\n",
" for n, part in enumerate(content[\"parts\"]):\n",
" if file_data := part.get(\"file_data\", None):\n",
" upload_file_data(file_data, index)\n",
" index += 1\n",
"\n",
"import json\n",
"print(json.dumps(contents, indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "E7zAD69vE92b"
},
"source": [
"## Call `generate_content`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LB2LxPmAB95V"
},
"outputs": [],
"source": [
"from IPython.display import display\n",
"from IPython.display import Markdown\n",
"\n",
"# Call the model and print the response.\n",
"gemini = genai.GenerativeModel(model_name=model)\n",
"\n",
"response = gemini.generate_content(\n",
" contents,\n",
" generation_config=generation_config,\n",
" safety_settings=safety_settings,\n",
" stream=stream,\n",
")\n",
"\n",
"display(Markdown(response.text))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9c9d345e9868"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://ai.google.dev/gemini-api/docs\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />Docs on ai.google.dev</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/google-gemini/cookbook/blob/main/quickstarts\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />More notebooks in the Cookbook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F91AeeGO1ncU"
},
"source": [
"## [optional] Show the conversation\n",
"\n",
"This section displays the conversation received from Google AI Studio."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "yoL3p3KPylFW"
},
"outputs": [],
"source": [
"# @title Show the conversation, in colab.\n",
"import mimetypes\n",
"\n",
"def show_file(file_data):\n",
" mime_type = file_data[\"mime_type\"]\n",
"\n",
" if drive_id := file_data.get(\"drive_id\", None):\n",
" path = next(\n",
" pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n",
" )\n",
" name = path\n",
" # data = path.read_bytes()\n",
" kwargs = {\"filename\": path}\n",
" elif url := file_data.get(\"url\", None):\n",
" name = url\n",
" kwargs = {\"url\": url}\n",
" # response = requests.get(url)\n",
" # data = response.content\n",
" elif data := file_data.get(\"inline_data\", None):\n",
" name = None\n",
" kwargs = {\"data\": data}\n",
" elif name := file_data.get(\"filename\", None):\n",
" if not pathlib.Path(name).exists():\n",
" raise IOError(\n",
" f\"local file: `{name}` does not exist. You can upload files to \"\n",
" 'Colab using the file manager (\"📁 Files\"in the left toolbar)'\n",
" )\n",
" else:\n",
" raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n",
"\n",
" print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n",
" return\n",
"\n",
" format = mimetypes.guess_extension(mime_type).strip(\".\")\n",
" if mime_type.startswith(\"image/\"):\n",
" image = IPython.display.Image(**kwargs, width=256)\n",
" IPython.display.display(image)\n",
" print()\n",
" return\n",
"\n",
" if mime_type.startswith(\"audio/\"):\n",
" if len(data) < 2**12:\n",
" audio = IPython.display.Audio(**kwargs)\n",
" IPython.display.display(audio)\n",
" print()\n",
" return\n",
"\n",
" if mime_type.startswith(\"video/\"):\n",
" if len(data) < 2**12:\n",
" audio = IPython.display.Video(**kwargs, mimetype=mime_type)\n",
" IPython.display.display(audio)\n",
" print()\n",
" return\n",
"\n",
" print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n",
"\n",
"\n",
"for content in gais_contents:\n",
" if role := content.get(\"role\", None):\n",
" print(\"Role:\", role, \"\\n\")\n",
"\n",
" for n, part in enumerate(content[\"parts\"]):\n",
" if text := part.get(\"text\", None):\n",
" print(text, \"\\n\")\n",
"\n",
" elif file_data := part.get(\"file_data\", None):\n",
" show_file(file_data)\n",
"\n",
" print(\"-\" * 80, \"\\n\")"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"Tce3stUlHN0L"
],
"name": "aistudio_gemini_prompt_freeform.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading