发布于 2026-01-06 1 阅读
0

如何使用 Node.js 和 Google Gemini 创建 AI 聊天机器人 Google Gemini Node.js 聊天机器人

如何使用 Node.js 和 Google Gemini 创建 AI 聊天机器人

Google Gemini Node.js 聊天机器人

介绍

过去一年,生成式人工智能已成为科技领域的热门话题。许多开发者正在利用这项技术创建有趣的项目。谷歌也开发了自己的生成式人工智能模型,名为Gemini。

本文将构建一个简单的 Node.js 聊天机器人,并将其与 Google Gemini 集成。我们将使用 Google Gemini SDK 来实现这一目标。

什么是双子座?

双子座谷歌

Google Gemini是 Google AI 开发的一款高级 AI 模型。与传统 AI 模型不同,Gemini 不仅限于处理文本,它还能理解和处理代码、音频、图像和视频等多种格式。这一特性为您的 Node.js 项目带来了无限可能。

项目设置

1. 创建一个 Node.js 项目

要开始我们的项目,我们需要搭建一个 Node.js 环境。让我们通过在终端运行以下命令来创建一个新的 Node.js 项目:

npm init -y
Enter fullscreen mode Exit fullscreen mode

此命令将初始化一个新的Node.js项目。

2. 添加来自 Google AI Studio 的样板代码

从Google AI Studio获取初始代码,然后点击“获取代码”,复制 Node.js 代码并将其粘贴到名为“Get Code”的文件中。index.js

aistudio-boilerplate

此外,请务必从 AI Studio 生成 API 密钥。选择您的 Google Cloud 项目,然后单击“创建”。

API密钥

3. 安装依赖项:

现在,我们将安装项目所需的依赖项。

npm install @google/generative-ai ora chalk prompt-sync
Enter fullscreen mode Exit fullscreen mode

这些软件包用于以下用途:

  1. @google/generative-ai:谷歌用于生成式人工智能任务(例如文本和图像生成)的软件包。
  2. ora在终端中创建加载指示器和进度条。
  3. chalk:为终端中的文本设置颜色和样式。
  4. prompt-sync允许在Node.js应用程序中同步用户输入提示。

4. 创建聊天机器人并进行自定义

在聊天机器人脚本的初始版本中,我们使用 CommonJS 的 require 语句导入模块。然而,在最终版本中,我们采用了 ES6 的导入语法,以获得更简洁、更现代化的代码组织。

打开你的package.json文件,并在下方添加以下代码行"main": "index.js",

"type": "module",
Enter fullscreen mode Exit fullscreen mode

它应该看起来像这样(仅上半部分):

{
  "name": "devarshishimpi-google-gemini-nodejs-chatbot",
  "version": "1.0.0",
  "description": "Simple Google Gemini ChatBot for Nodejs",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
Enter fullscreen mode Exit fullscreen mode

并继续……

然后,我们从使用更新const为使用import

import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
import chalk from 'chalk';
import ora from 'ora';
import prompt from 'prompt-sync';
Enter fullscreen mode Exit fullscreen mode

其中一项重大改进是引入了使用prompt-sync库的用户输入机制。这使得用户可以与聊天机器人实时交互,直接在控制台中输入消息。

const userInput = promptSync(chalk.green('You: '));
Enter fullscreen mode Exit fullscreen mode

然后,我们对用户界面进行自定义orachalk使其更具用户响应性和易用性。

while (true) {
    const userInput = promptSync(chalk.green('You: '));
    if (userInput.toLowerCase() === 'exit') {
        console.log(chalk.yellow('Goodbye!'));
        process.exit(0);
    }
    const result = await chat.sendMessage(userInput);
    const response = result.response;
    console.log(chalk.blue('AI:'), response.text());
}
Enter fullscreen mode Exit fullscreen mode

然后,我们在代码中添加一些错误处理逻辑:

if (result.error) {
    console.error(chalk.red('AI Error:'), result.error.message);
    continue;
}
const response = result.response.text();
console.log(chalk.blue('AI:'), response);
Enter fullscreen mode Exit fullscreen mode

5. 最终输出

项目最终代码请见此处,如果您觉得有用,请给仓库点个星⭐️:

GitHub 标志 devarshishimpi / google-gemini-nodejs-chatbot

适用于 Node.js 的简易 Google Gemini 聊天机器人

Google Gemini Node.js 聊天机器人

适用于 Node.js 的简易 Google Gemini 聊天机器人

安装

npm install @google/generative-ai ora chalk prompt-sync
Enter fullscreen mode Exit fullscreen mode

API_KEY请将“ in”替换index.js为您自己的 API 密钥。

node index.js
Enter fullscreen mode Exit fullscreen mode

贡献

欢迎提交 Pull Request。对于重大更改,请先提交 Issue 进行讨论。更多信息请参阅CONTRIBUTING.md文件。






输出:

我们使用node index.js此功能运行代码。请务必将 `<API Key>` 替换YOUR_API_KEY为您从 Google API Studio 生成的 API 密钥。

双子座聊天机器人输出

结论

感谢阅读!如果您觉得这篇文章对您有所帮助,请考虑分享给其他可能需要的人。欢迎浏览我的其他博客文章并访问我的社交媒体账号!

阅读更多

文章来源:https://dev.to/devarshishimpi/how-to-create-an-ai-chatbot-with-google-gemini-using-nodejs-pn6