|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "Tce3stUlHN0L" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "##### Copyright 2023 Google LLC" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": { |
| 16 | + "cellView": "form", |
| 17 | + "id": "tuOe1ymfHZPu" |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 22 | + "# you may not use this file except in compliance with the License.\n", |
| 23 | + "# You may obtain a copy of the License at\n", |
| 24 | + "#\n", |
| 25 | + "# https://www.apache.org/licenses/LICENSE-2.0\n", |
| 26 | + "#\n", |
| 27 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 28 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 29 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 30 | + "# See the License for the specific language governing permissions and\n", |
| 31 | + "# limitations under the License." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": { |
| 38 | + "id": "kWIuwKG2_oWE" |
| 39 | + }, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "# Install the client library and import necessary modules.\n", |
| 43 | + "!pip install google-generativeai\n", |
| 44 | + "import google.generativeai as palm\n", |
| 45 | + "import base64\n", |
| 46 | + "import json\n", |
| 47 | + "import pprint" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "metadata": { |
| 54 | + "id": "_SvYoR3WCeKr" |
| 55 | + }, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "# Configure the client library by providing your API key.\n", |
| 59 | + "palm.configure(api_key=\"YOUR API KEY\")" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "metadata": { |
| 66 | + "id": "Pwab0i0RAHBN" |
| 67 | + }, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "# These parameters for the model call can be set by URL parameters.\n", |
| 71 | + "model = \"\" # @param {isTemplate: true}\n", |
| 72 | + "temperature = 0.7 # @param {isTemplate: true}\n", |
| 73 | + "candidate_count = 1 # @param {isTemplate: true}\n", |
| 74 | + "top_k = 40 # @param {isTemplate: true}\n", |
| 75 | + "top_p = 0.95 # @param {isTemplate: true}\n", |
| 76 | + "max_output_tokens = 1024 # @param {isTemplate: true}\n", |
| 77 | + "text_b64 = \"\" # @param {isTemplate: true}\n", |
| 78 | + "stop_sequences_b64 = \"\" # @param {isTemplate: true}\n", |
| 79 | + "safety_settings_b64 = \"\" # @param {isTemplate: true}\n", |
| 80 | + "\n", |
| 81 | + "# Convert the prompt text param from a bae64 string to a string.\n", |
| 82 | + "text = base64.b64decode(text_b64).decode(\"utf-8\")\n", |
| 83 | + "\n", |
| 84 | + "# Convert the stop_sequences and safety_settings params from base64 strings to lists.\n", |
| 85 | + "stop_sequences = json.loads(base64.b64decode(stop_sequences_b64).decode(\"utf-8\"))\n", |
| 86 | + "safety_settings = json.loads(base64.b64decode(safety_settings_b64).decode(\"utf-8\"))\n", |
| 87 | + "\n", |
| 88 | + "defaults = {\n", |
| 89 | + " 'model': model,\n", |
| 90 | + " 'temperature': temperature,\n", |
| 91 | + " 'candidate_count': candidate_count,\n", |
| 92 | + " 'top_k': top_k,\n", |
| 93 | + " 'top_p': top_p,\n", |
| 94 | + " 'max_output_tokens': max_output_tokens,\n", |
| 95 | + " 'stop_sequences': stop_sequences,\n", |
| 96 | + " 'safety_settings': safety_settings,\n", |
| 97 | + "}\n", |
| 98 | + "\n", |
| 99 | + "# Show what will be sent with the API call.\n", |
| 100 | + "pprint.pprint(defaults | {'prompt': text})" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": { |
| 107 | + "id": "LB2LxPmAB95V" |
| 108 | + }, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "# Call the model and print the response.\n", |
| 112 | + "response = palm.generate_text(\n", |
| 113 | + " **defaults,\n", |
| 114 | + " prompt=text\n", |
| 115 | + ")\n", |
| 116 | + "print(response.candidates[0]['output'])" |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "metadata": { |
| 121 | + "colab": { |
| 122 | + "name": "aistudio_palm_prompt_text.ipynb", |
| 123 | + "toc_visible": true |
| 124 | + }, |
| 125 | + "kernelspec": { |
| 126 | + "display_name": "Python 3", |
| 127 | + "name": "python3" |
| 128 | + } |
| 129 | + }, |
| 130 | + "nbformat": 4, |
| 131 | + "nbformat_minor": 0 |
| 132 | +} |
0 commit comments