PaLM API: การเริ่มต้นใช้งานแชทอย่างรวดเร็วด้วย Node.js

ดูใน ai.google.dev เรียกใช้ใน Google Colab ดูซอร์สบน GitHub

ภาพรวม

การเริ่มต้นอย่างรวดเร็วนี้จะสาธิตวิธีใช้ PaLM API ซึ่งช่วยให้คุณเข้าถึงโมเดลภาษาขนาดใหญ่ล่าสุดของ Google พร้อมด้วย PaLM Node.js SDK สำหรับกรณีการใช้งานที่เน้นกล่องโต้ตอบ เช่น แชทบ็อตโดยเฉพาะ

รับคีย์ API

คุณจะต้องรับคีย์ API เพื่อเริ่มต้นใช้งาน ตั้งค่าเป็นตัวแปรสภาพแวดล้อมโดยทำดังนี้

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

ติดตั้งไคลเอ็นต์ API

ในไดเรกทอรีใหม่ ให้เริ่มต้นโปรเจ็กต์ Node.js โดยใช้ npm และติดตั้งไลบรารี 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 0.905s

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.274s

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.

รับคีย์ API

ทำตามวิธีการในหน้าการตั้งค่าเพื่อสร้างคีย์ API สำหรับแอป คุณจะต้องใช้คีย์ API นี้ในขั้นตอนถัดไป

สร้างข้อความ

สร้างไฟล์ใหม่ index.js และเพิ่มโค้ดต่อไปนี้ โดยจะป้อนคีย์ PaLM API ผ่านตัวแปรสภาพแวดล้อม API_KEY ดังนี้

%%writefile index.js

const { DiscussServiceClient } = require("@google-ai/generativelanguage");
const { GoogleAuth } = require("google-auth-library");

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

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

async function main() {
  const result = await client.generateMessage({
    model: MODEL_NAME, // Required. The model to use to generate the result.
    temperature: 0.5, // Optional. Value `0.0` always uses the highest-probability result.
    candidateCount: 1, // Optional. The number of candidate results to generate.
    prompt: {
      // optional, preamble context to prime responses
      context: "Respond to all questions with a rhyming poem.",
      // Optional. Examples for further fine-tuning of responses.
      examples: [
        {
          input: { content: "What is the capital of California?" },
          output: {
            content:
              `If the capital of California is what you seek,
Sacramento is where you ought to peek.`,
          },
        },
      ],
      // Required. Alternating prompt/response messages.
      messages: [{ content: "How tall is the Eiffel Tower?" }],
    },
  });

  console.log(result[0].candidates[0].content);
}

main();
Overwriting index.js

จากนั้นเรียกใช้สคริปต์ด้วยคำสั่งต่อไปนี้

node index.js
The Eiffel Tower is 324 meters tall,
It's a sight to behold, tall and not small.
It's made of iron and weighs 10,100 tons,
It's a wonder of the world, it's a must-see for all.

สนทนาต่อ

หากต้องการสนทนาต่อหลังจากได้รับข้อความแจ้งเบื้องต้น คุณต้องต่อท้ายข้อความผู้สมัครที่ส่งคืนและพรอมต์ถัดไปของคุณด้วย ดังนี้

%%writefile index.js
const { DiscussServiceClient } = require("@google-ai/generativelanguage");
const { GoogleAuth } = require("google-auth-library");

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

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

async function main() {
  let first = "Tell me a one short animal fact."
  let messages = [{ content: first }];

  const result = await client.generateMessage({
    model: MODEL_NAME,
    prompt: { messages },
  });

  console.log("User:\n\n", first, "\n\n")
  console.log("Palm:\n\n", result[0].candidates[0].content, "\n\n");

  let second = "Oh, where do those live?"

  messages.push({ content: result[0].candidates[0].content });
  messages.push({ content: second });

  const secondResult = await client.generateMessage({
    model: MODEL_NAME,
    prompt: { messages },
  });

  console.log("User:\n\n", second, "\n\n")
  console.log("Palm:\n\n", secondResult[0].candidates[0].content, "\n\n");
}

main();
Overwriting index.js
node index.js
User:

 Tell me a one short animal fact. 


Palm:

 The world's smallest mammal is the bumblebee bat, which weighs less than a penny. 


User:

 Oh, where do those live? 


Palm:

 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.