To get started, you'll need to get an API key.
Set it as an environment variable:
importosos.environ["API_KEY"]="<YOUR API KEY>"
Installing the API Client
In a new directory, initialize a Node.js project using npm and install the
google-auth library:
npminit-ynpminstallgoogle-auth-library
Wrote to /content/package.json:
{
"name": "content",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@google-ai/generativelanguage": "^1.0.1",
"google-auth-library": "^9.0.0"
},
"devDependencies": {},
"description": ""
}
+ google-auth-library@9.0.0
updated 1 package and audited 74 packages in 1.105s
3 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm WARN content@1.0.0 No description
npm WARN content@1.0.0 No repository field.
Next, you'll need to install the Generative Language client library:
npminstall@google-ai/generativelanguage
+ @google-ai/generativelanguage@1.0.1
updated 1 package and audited 74 packages in 2.126s
3 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm WARN content@1.0.0 No description
npm WARN content@1.0.0 No repository field.
Generating Messages
Create a new file index.js and add the following code, supplying your API key
through the API_KEY environment variable:
%%writefileindex.jsconst{TextServiceClient}=require("@google-ai/generativelanguage").v1beta2;const{GoogleAuth}=require("google-auth-library");constMODEL_NAME="models/text-bison-001";constAPI_KEY=process.env.API_KEY;constclient=newTextServiceClient({authClient:newGoogleAuth().fromAPIKey(API_KEY),});constprompt="Repeat after me: one, two,";client.generateText({model:MODEL_NAME,prompt:{text:prompt,},}).then((result)=>{console.log(JSON.stringify(result,null,2));});
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-08-31 UTC."],[],[],null,["# PaLM API: Text Quickstart with Node.js\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [View on ai.google.dev](https://ai.google.dev/palm_docs/text_node_quickstart) | [Try a Colab notebook](https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/palm_docs/text_node_quickstart.ipynb) | [View notebook on GitHub](https://github.com/google/generative-ai-docs/blob/main/site/en/palm_docs/text_node_quickstart.ipynb) |\n\nObtain an API Key\n-----------------\n\nTo get started, you'll need to [get an API key](/palm_docs/setup).\nSet it as an environment variable: \n\n import os\n os.environ[\"API_KEY\"] = \"\u003cYOUR API KEY\u003e\"\n\n| **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=\u003cYOUR API KEY\u003e`.\n\nInstalling the API Client\n-------------------------\n\nIn a new directory, initialize a Node.js project using npm and install the\n`google-auth` library: \n\n npm init -y\n\n npm install google-auth-library\n\n```\nWrote 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\nupdated 1 package and audited 74 packages in 1.105s\n\n3 packages are looking for funding\n run `npm fund` for details\n\nfound 0 vulnerabilities\nnpm WARN content@1.0.0 No description\nnpm WARN content@1.0.0 No repository field.\n```\n\nNext, you'll need to install the Generative Language client library: \n\n npm install @google-ai/generativelanguage\n\n```\n\n+ @google-ai/generativelanguage@1.0.1\nupdated 1 package and audited 74 packages in 2.126s\n\n3 packages are looking for funding\n run `npm fund` for details\n\nfound 0 vulnerabilities\nnpm WARN content@1.0.0 No description\nnpm WARN content@1.0.0 No repository field.\n```\n\nGenerating Messages\n-------------------\n\nCreate a new file `index.js` and add the following code, supplying your API key\nthrough the `API_KEY` environment variable: \n\n %%writefile index.js\n const { TextServiceClient } =\n require(\"@google-ai/generativelanguage\").v1beta2;\n\n const { GoogleAuth } = require(\"google-auth-library\");\n\n const MODEL_NAME = \"models/text-bison-001\";\n const API_KEY = process.env.API_KEY;\n\n const client = new TextServiceClient({\n authClient: new GoogleAuth().fromAPIKey(API_KEY),\n });\n\n const prompt = \"Repeat after me: one, two,\";\n\n client\n .generateText({\n model: MODEL_NAME,\n prompt: {\n text: prompt,\n },\n })\n .then((result) =\u003e {\n console.log(JSON.stringify(result, null, 2));\n });\n\n```\nOverwriting index.js\n```\n\nThen run the script: \n\n\n node index.js\n\n```\n[\n {\n \"candidates\": [\n {\n \"safetyRatings\": [\n {\n \"category\": \"HARM_CATEGORY_DEROGATORY\",\n \"probability\": \"NEGLIGIBLE\"\n },\n {\n \"category\": \"HARM_CATEGORY_TOXICITY\",\n \"probability\": \"NEGLIGIBLE\"\n },\n {\n \"category\": \"HARM_CATEGORY_VIOLENCE\",\n \"probability\": \"NEGLIGIBLE\"\n },\n {\n \"category\": \"HARM_CATEGORY_SEXUAL\",\n \"probability\": \"NEGLIGIBLE\"\n },\n {\n \"category\": \"HARM_CATEGORY_MEDICAL\",\n \"probability\": \"NEGLIGIBLE\"\n },\n {\n \"category\": \"HARM_CATEGORY_DANGEROUS\",\n \"probability\": \"NEGLIGIBLE\"\n }\n ],\n \"output\": \"One, two, three, four.\"\n }\n ],\n \"filters\": [],\n \"safetyFeedback\": []\n },\n null,\n null\n]\n```"]]