{ "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", "id": "09cf87d0", "metadata": { "id": "09cf87d0" }, "source": [ "# PaLM API: Chat Quickstart with Node.js" ] }, { "cell_type": "markdown", "metadata": { "id": "lEXQ3OwKIa-O" }, "source": [ "\n", " \n", " \n", " \n", "
\n", " View on ai.google.dev\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", "
" ] }, { "cell_type": "markdown", "id": "j6AyU5Bv7fAm", "metadata": { "id": "j6AyU5Bv7fAm" }, "source": [ "## Overview\n", "\n", "This quickstart demonstrates how to use the PaLM API, which gives you access to\n", "Google's latest large language models, with the PaLM Node.js SDK specifically\n", "for dialog-focused use cases, such as chatbots." ] }, { "cell_type": "markdown", "id": "ojFlwNm_sLb0", "metadata": { "id": "ojFlwNm_sLb0" }, "source": [ "## Obtain an API Key\n", "\n", "To get started, you'll need to [get an API key](setup).\n", "Set it as an environment variable:" ] }, { "cell_type": "code", "execution_count": 6, "id": "tjxQ-b-wsYP_", "metadata": { "id": "tjxQ-b-wsYP_" }, "outputs": [], "source": [ "import os\n", "os.environ[\"API_KEY\"] = \"\"" ] }, { "cell_type": "markdown", "id": "FVV5j6OUtaCO", "metadata": { "id": "FVV5j6OUtaCO" }, "source": [ "Note: The use of `os.environ` above is only because this is a python based notebook. Working from the command line you would `export API_KEY=`." ] }, { "cell_type": "markdown", "id": "ubDO1H4C7hP1", "metadata": { "id": "ubDO1H4C7hP1" }, "source": [ "## Install the API client\n", "\n", "In a new directory, initialize a Node.js project using npm and install the\n", "`google-auth` library:" ] }, { "cell_type": "code", "execution_count": 9, "id": "78e6054d", "metadata": { "id": "78e6054d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wrote to /content/package.json:\n", "\n", "{\n", " \"name\": \"content\",\n", " \"version\": \"1.0.0\",\n", " \"main\": \"index.js\",\n", " \"scripts\": {\n", " \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n", " },\n", " \"keywords\": [],\n", " \"author\": \"\",\n", " \"license\": \"ISC\",\n", " \"dependencies\": {\n", " \"@google-ai/generativelanguage\": \"^1.0.1\",\n", " \"google-auth-library\": \"^9.0.0\"\n", " },\n", " \"devDependencies\": {},\n", " \"description\": \"\"\n", "}\n", "\n", "\n", "+ google-auth-library@9.0.0\n", "updated 1 package and audited 74 packages in 0.905s\n", "\n", "3 packages are looking for funding\n", " run `npm fund` for details\n", "\n", "found 0 vulnerabilities\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "npm WARN content@1.0.0 No description\n", "npm WARN content@1.0.0 No repository field.\n", "\n" ] } ], "source": [ "%%bash\n", "npm init -y\n", "\n", "npm install google-auth-library" ] }, { "cell_type": "markdown", "id": "0cc1fb88", "metadata": { "id": "0cc1fb88" }, "source": [ "Next, install the client library:" ] }, { "cell_type": "code", "execution_count": 10, "id": "80ce5a41", "metadata": { "id": "80ce5a41" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+ @google-ai/generativelanguage@1.0.1\n", "updated 1 package and audited 74 packages in 2.274s\n", "\n", "3 packages are looking for funding\n", " run `npm fund` for details\n", "\n", "found 0 vulnerabilities\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "npm WARN content@1.0.0 No description\n", "npm WARN content@1.0.0 No repository field.\n", "\n" ] } ], "source": [ "%%bash\n", "npm install @google-ai/generativelanguage" ] }, { "cell_type": "markdown", "id": "0b27f7a9", "metadata": { "id": "0b27f7a9" }, "source": [ "## Obtain an API key\n", "\n", "Follow the instructions on the [setup\n", "page](setup) to create an API key for your app. You will need this API key in\n", "the next step.\n", "\n", "## Generate messages\n", "\n", "Create a new file `index.js` and add the following code, supplying your\n", "PaLM API key through the `API_KEY` environment variable:" ] }, { "cell_type": "code", "execution_count": 11, "id": "9197502c", "metadata": { "id": "9197502c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting index.js\n" ] } ], "source": [ "%%writefile index.js\n", "\n", "const { DiscussServiceClient } = require(\"@google-ai/generativelanguage\");\n", "const { GoogleAuth } = require(\"google-auth-library\");\n", "\n", "const MODEL_NAME = \"models/chat-bison-001\";\n", "const API_KEY = process.env.API_KEY;\n", "\n", "const client = new DiscussServiceClient({\n", " authClient: new GoogleAuth().fromAPIKey(API_KEY),\n", "});\n", "\n", "async function main() {\n", " const result = await client.generateMessage({\n", " model: MODEL_NAME, // Required. The model to use to generate the result.\n", " temperature: 0.5, // Optional. Value `0.0` always uses the highest-probability result.\n", " candidateCount: 1, // Optional. The number of candidate results to generate.\n", " prompt: {\n", " // optional, preamble context to prime responses\n", " context: \"Respond to all questions with a rhyming poem.\",\n", " // Optional. Examples for further fine-tuning of responses.\n", " examples: [\n", " {\n", " input: { content: \"What is the capital of California?\" },\n", " output: {\n", " content:\n", " `If the capital of California is what you seek,\n", "Sacramento is where you ought to peek.`,\n", " },\n", " },\n", " ],\n", " // Required. Alternating prompt/response messages.\n", " messages: [{ content: \"How tall is the Eiffel Tower?\" }],\n", " },\n", " });\n", "\n", " console.log(result[0].candidates[0].content);\n", "}\n", "\n", "main();" ] }, { "cell_type": "markdown", "id": "8a2600ac", "metadata": { "id": "8a2600ac" }, "source": [ "Then run the script:" ] }, { "cell_type": "code", "execution_count": 12, "id": "1a7cc52d", "metadata": { "id": "1a7cc52d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Eiffel Tower is 324 meters tall,\r\n", "It's a sight to behold, tall and not small.\r\n", "It's made of iron and weighs 10,100 tons,\r\n", "It's a wonder of the world, it's a must-see for all.\n" ] } ], "source": [ "%%bash\n", "node index.js" ] }, { "cell_type": "markdown", "id": "1781cccc", "metadata": { "id": "1781cccc" }, "source": [ "### Continue the conversation\n", "\n", "To continue a conversation after an initial prompt message, you need to append\n", "the returned candidate message as well as your next prompt:" ] }, { "cell_type": "code", "execution_count": 25, "id": "yt3FKhN78bTX", "metadata": { "id": "yt3FKhN78bTX" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting index.js\n" ] } ], "source": [ "%%writefile index.js\n", "const { DiscussServiceClient } = require(\"@google-ai/generativelanguage\");\n", "const { GoogleAuth } = require(\"google-auth-library\");\n", "\n", "const MODEL_NAME = \"models/chat-bison-001\";\n", "const API_KEY = process.env.API_KEY;\n", "\n", "const client = new DiscussServiceClient({\n", " authClient: new GoogleAuth().fromAPIKey(API_KEY),\n", "});\n", "\n", "async function main() {\n", " let first = \"Tell me a one short animal fact.\"\n", " let messages = [{ content: first }];\n", "\n", " const result = await client.generateMessage({\n", " model: MODEL_NAME,\n", " prompt: { messages },\n", " });\n", "\n", " console.log(\"User:\\n\\n\", first, \"\\n\\n\")\n", " console.log(\"Palm:\\n\\n\", result[0].candidates[0].content, \"\\n\\n\");\n", "\n", " let second = \"Oh, where do those live?\"\n", "\n", " messages.push({ content: result[0].candidates[0].content });\n", " messages.push({ content: second });\n", "\n", " const secondResult = await client.generateMessage({\n", " model: MODEL_NAME,\n", " prompt: { messages },\n", " });\n", "\n", " console.log(\"User:\\n\\n\", second, \"\\n\\n\")\n", " console.log(\"Palm:\\n\\n\", secondResult[0].candidates[0].content, \"\\n\\n\");\n", "}\n", "\n", "main();" ] }, { "cell_type": "code", "execution_count": 26, "id": "KjX01L3w8cJQ", "metadata": { "id": "KjX01L3w8cJQ" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User:\n", "\n", " Tell me a one short animal fact. \n", "\n", "\n", "Palm:\n", "\n", " The world's smallest mammal is the bumblebee bat, which weighs less than a penny. \n", "\n", "\n", "User:\n", "\n", " Oh, where do those live? \n", "\n", "\n", "Palm:\n", "\n", " The bumblebee bat is found in the rainforests of Thailand, Myanmar, and Laos. It is a small, nocturnal bat that feeds on insects. The bumblebee bat is the smallest mammal in the world, weighing only about 2 grams. It is about the size of a bumblebee, hence its name. The bumblebee bat is a very important part of the rainforest ecosystem. It helps to control insect populations and pollinate plants. \n", "\n", "\n" ] } ], "source": [ "%%bash\n", "node index.js" ] } ], "metadata": { "colab": { "name": "chat_node_quickstart.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }