Last active
March 11, 2024 05:34
-
-
Save gilbertalgordo/83cc1afd42216f4fe75c3c97ae474a2a to your computer and use it in GitHub Desktop.
aistudio_gemini_prompt_chat_b64.ipynb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/gilbertalgordo/83cc1afd42216f4fe75c3c97ae474a2a/aistudio_gemini_prompt_chat_b64.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"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": "code", | |
"source": [ | |
"from google.colab import drive\n", | |
"drive.mount('/content/drive')" | |
], | |
"metadata": { | |
"id": "S_v8RqxzlSJG" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "FKwyTRdwB8aW" | |
}, | |
"source": [ | |
"## Setup" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "rlE8UqxrDIez" | |
}, | |
"source": [ | |
"### Install & import\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "cZiU4TKzznh9", | |
"outputId": "9ff1404b-590c-4d10-a26d-baae9c8feee1" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", | |
"\u001b[0m" | |
] | |
} | |
], | |
"source": [ | |
"!pip install -U -q google-generativeai" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "kWIuwKG2_oWE" | |
}, | |
"outputs": [], | |
"source": [ | |
"# import necessary modules.\n", | |
"import google.generativeai as genai\n", | |
"import json\n", | |
"import base64\n", | |
"import pathlib\n", | |
"import pprint\n", | |
"import requests\n", | |
"import mimetypes\n", | |
"from IPython.display import Markdown" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Fet3lFjdKHEM" | |
}, | |
"source": [ | |
"## Set the API key" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZoRWILAtCzBE" | |
}, | |
"source": [ | |
"Add your API_KEY to the secrets manager in the left pannel \"🔑\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "LaLCwNlkCyQd" | |
}, | |
"outputs": [], | |
"source": [ | |
"from google.colab import userdata\n", | |
"\n", | |
"API_KEY=userdata.get('API_KEY')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "_SvYoR3WCeKr" | |
}, | |
"outputs": [], | |
"source": [ | |
"# Configure the client library by providing your API key.\n", | |
"genai.configure(api_key=API_KEY)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "weo-o73WDpdm" | |
}, | |
"source": [ | |
"### Parse the arguments" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "uIog-0SyDuIF" | |
}, | |
"outputs": [], | |
"source": [ | |
"model = 'gemini-1.0-pro' # @param {isTemplate: true}\n", | |
"contents_b64 = 'W10=' # @param {isTemplate: true}\n", | |
"generation_config_b64 = 'eyJ0ZW1wZXJhdHVyZSI6MC45LCJ0b3BfcCI6MSwidG9wX2siOjEsIm1heF9vdXRwdXRfdG9rZW5zIjoyMDQ4LCJzdG9wX3NlcXVlbmNlcyI6W119' # @param {isTemplate: true}\n", | |
"safety_settings_b64 = 'W3siY2F0ZWdvcnkiOiJIQVJNX0NBVEVHT1JZX0hBUkFTU01FTlQiLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfSEFURV9TUEVFQ0giLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfU0VYVUFMTFlfRVhQTElDSVQiLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfREFOR0VST1VTX0NPTlRFTlQiLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn1d' # @param {isTemplate: true}\n", | |
"user_input_b64 = '' # @param {isTemplate: true}\n", | |
"\n", | |
"contents = json.loads(base64.b64decode(contents_b64))\n", | |
"generation_config = json.loads(base64.b64decode(generation_config_b64))\n", | |
"safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n", | |
"user_input = base64.b64decode(user_input_b64).decode()\n", | |
"stream = False" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "wBS8xNhN0x62", | |
"outputId": "f74a5478-c108-4a20-9142-71cff8d8bfbd" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'role': 'user', 'parts': [{'text': 'hello'}]},\n", | |
" {'role': 'model', 'parts': [{'text': 'Hello! How can I assist you today?'}]}]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"contents" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "1681593ef561", | |
"outputId": "f63e84ca-9d44-44c3-c459-f6e017e014a3" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"generation_config" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "a2c31f8f1894", | |
"outputId": "42a4fa26-5f62-40c4-df01-761704fbfad9" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{}" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"safety_settings" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "4d17bac9fefc", | |
"outputId": "b0fb2f4f-2c92-4c11-f62e-9d4eaea67b97" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'How does electricity work?'" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"user_input" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "E7zAD69vE92b" | |
}, | |
"source": [ | |
"### Call the API" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "LB2LxPmAB95V" | |
}, | |
"outputs": [], | |
"source": [ | |
"# Call the model and print the response.\n", | |
"gemini = genai.GenerativeModel(model_name=model)\n", | |
"\n", | |
"chat = gemini.start_chat(history=contents)\n", | |
"\n", | |
"response = chat.send_message(\n", | |
" user_input,\n", | |
" stream=stream)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Lm3RXwYuGtZK", | |
"outputId": "65db2e1b-ee4a-4cb6-c6b9-295919808c1e" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/markdown": [ | |
"Electricity is the flow of electric charge. It is a fundamental part of nature and is responsible for a wide range of phenomena, from lightning and static electricity to the operation of electronic devices.\n", | |
"\n", | |
"**Basic Principles of Electricity:**\n", | |
"\n", | |
"1. **Electric Charge:** Matter is made up of atoms, which have a positively charged nucleus surrounded by negatively charged electrons. The movement of these charged particles creates electric currents.\n", | |
"\n", | |
"2. **Electric Field:** Electric charges create an electric field around them. The strength and direction of this field depend on the amount and type of charge.\n", | |
"\n", | |
"3. **Electric Potential:** Electric potential is the amount of electrical energy stored in a region of space. It is measured in volts (V).\n", | |
"\n", | |
"4. **Electric Current:** Electric current is the flow of electric charge through a conductor. It is measured in amperes (A).\n", | |
"\n", | |
"**How Electricity Works:**\n", | |
"\n", | |
"1. **Generation of Electricity:** Electricity can be generated in various ways, including:\n", | |
" - **Mechanical Energy:** Generators convert mechanical energy into electrical energy by spinning a conductor in a magnetic field. This is the principle behind power plants that use turbines driven by steam, water, or wind.\n", | |
" - **Chemical Energy:** Batteries generate electricity through chemical reactions.\n", | |
" - **Solar Energy:** Solar cells convert sunlight directly into electricity using the photovoltaic effect.\n", | |
"\n", | |
"2. **Transmission of Electricity:** Electricity is transmitted over long distances through power lines. These lines are made of conductive materials, such as copper or aluminum, which allow electrons to flow easily.\n", | |
"\n", | |
"3. **Distribution of Electricity:** Once electricity reaches populated areas, it is distributed to homes, businesses, and other consumers through a network of local power lines and transformers.\n", | |
"\n", | |
"4. **Utilization of Electricity:** Electricity is used to power various devices and appliances by converting electrical energy into other forms of energy, such as light, heat, or motion.\n", | |
"\n", | |
"The flow of electricity in a circuit is driven by the difference in electric potential between two points. When a complete circuit is formed, electrons can flow from a region of higher potential to a region of lower potential, creating an electric current.\n", | |
"\n", | |
"Electricity is a versatile form of energy that has revolutionized modern society. It is used in a wide range of applications, from lighting and heating to powering computers and transportation systems." | |
], | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"display(Markdown(response.text))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "JbKuUc3NGxYD", | |
"outputId": "52c4b38a-511d-4340-8dab-a10c45848e84" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_HATE_SPEECH\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_HARASSMENT\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_DANGEROUS_CONTENT\n", | |
" probability: NEGLIGIBLE\n", | |
"}" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"response.prompt_feedback" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "SLAaIq3kgwwJ", | |
"outputId": "ea0569eb-8242-45fc-80cc-eea37173f4e3" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[index: 0\n", | |
"content {\n", | |
" parts {\n", | |
" text: \"Electricity is the flow of electric charge. It is a fundamental part of nature and is responsible for a wide range of phenomena, from lightning and static electricity to the operation of electronic devices.\\n\\n**Basic Principles of Electricity:**\\n\\n1. **Electric Charge:** Matter is made up of atoms, which have a positively charged nucleus surrounded by negatively charged electrons. The movement of these charged particles creates electric currents.\\n\\n2. **Electric Field:** Electric charges create an electric field around them. The strength and direction of this field depend on the amount and type of charge.\\n\\n3. **Electric Potential:** Electric potential is the amount of electrical energy stored in a region of space. It is measured in volts (V).\\n\\n4. **Electric Current:** Electric current is the flow of electric charge through a conductor. It is measured in amperes (A).\\n\\n**How Electricity Works:**\\n\\n1. **Generation of Electricity:** Electricity can be generated in various ways, including:\\n - **Mechanical Energy:** Generators convert mechanical energy into electrical energy by spinning a conductor in a magnetic field. This is the principle behind power plants that use turbines driven by steam, water, or wind.\\n - **Chemical Energy:** Batteries generate electricity through chemical reactions.\\n - **Solar Energy:** Solar cells convert sunlight directly into electricity using the photovoltaic effect.\\n\\n2. **Transmission of Electricity:** Electricity is transmitted over long distances through power lines. These lines are made of conductive materials, such as copper or aluminum, which allow electrons to flow easily.\\n\\n3. **Distribution of Electricity:** Once electricity reaches populated areas, it is distributed to homes, businesses, and other consumers through a network of local power lines and transformers.\\n\\n4. **Utilization of Electricity:** Electricity is used to power various devices and appliances by converting electrical energy into other forms of energy, such as light, heat, or motion.\\n\\nThe flow of electricity in a circuit is driven by the difference in electric potential between two points. When a complete circuit is formed, electrons can flow from a region of higher potential to a region of lower potential, creating an electric current.\\n\\nElectricity is a versatile form of energy that has revolutionized modern society. It is used in a wide range of applications, from lighting and heating to powering computers and transportation systems.\"\n", | |
" }\n", | |
" role: \"model\"\n", | |
"}\n", | |
"finish_reason: STOP\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_HATE_SPEECH\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_HARASSMENT\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"safety_ratings {\n", | |
" category: HARM_CATEGORY_DANGEROUS_CONTENT\n", | |
" probability: NEGLIGIBLE\n", | |
"}\n", | |
"]" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"response.candidates" | |
] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"collapsed_sections": [ | |
"Tce3stUlHN0L" | |
], | |
"name": "aistudio_gemini_prompt_chat_b64.ipynb", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"name": "python3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment