PaLM API: Node.js সহ পাঠ্য কুইকস্টার্ট

ai.google.dev-এ দেখুন Google Colab-এ চালান GitHub-এ উৎস দেখুন

একটি API কী পান

শুরু করতে, আপনাকে একটি API কী পেতে হবে। এটি একটি পরিবেশ পরিবর্তনশীল হিসাবে সেট করুন:

import os
os.environ["API_KEY"] = "<YOUR API KEY>"

API ক্লায়েন্ট ইনস্টল করা হচ্ছে

একটি নতুন ডিরেক্টরিতে, npm ব্যবহার করে একটি Node.js প্রকল্প শুরু করুন এবং google-auth লাইব্রেরি ইনস্টল করুন:

npm init -y

npm install google-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.

পরবর্তী, আপনাকে জেনারেটিভ ল্যাঙ্গুয়েজ ক্লায়েন্ট লাইব্রেরি ইনস্টল করতে হবে:

npm install @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.

বার্তা তৈরি করা হচ্ছে

একটি নতুন ফাইল index.js তৈরি করুন এবং API_KEY পরিবেশ ভেরিয়েবলের মাধ্যমে আপনার API কী সরবরাহ করে নিম্নলিখিত কোডটি যোগ করুন:

%%writefile index.js
const { TextServiceClient } =
  require("@google-ai/generativelanguage").v1beta2;

const { GoogleAuth } = require("google-auth-library");

const MODEL_NAME = "models/text-bison-001";
const API_KEY = process.env.API_KEY;

const client = new TextServiceClient({
  authClient: new GoogleAuth().fromAPIKey(API_KEY),
});

const prompt = "Repeat after me: one, two,";

client
  .generateText({
    model: MODEL_NAME,
    prompt: {
      text: prompt,
    },
  })
  .then((result) => {
    console.log(JSON.stringify(result, null, 2));
  });
Overwriting index.js

তারপর স্ক্রিপ্ট চালান:


node index.js
[
  {
    "candidates": [
      {
        "safetyRatings": [
          {
            "category": "HARM_CATEGORY_DEROGATORY",
            "probability": "NEGLIGIBLE"
          },
          {
            "category": "HARM_CATEGORY_TOXICITY",
            "probability": "NEGLIGIBLE"
          },
          {
            "category": "HARM_CATEGORY_VIOLENCE",
            "probability": "NEGLIGIBLE"
          },
          {
            "category": "HARM_CATEGORY_SEXUAL",
            "probability": "NEGLIGIBLE"
          },
          {
            "category": "HARM_CATEGORY_MEDICAL",
            "probability": "NEGLIGIBLE"
          },
          {
            "category": "HARM_CATEGORY_DANGEROUS",
            "probability": "NEGLIGIBLE"
          }
        ],
        "output": "One, two, three, four."
      }
    ],
    "filters": [],
    "safetyFeedback": []
  },
  null,
  null
]