如何在您自己的服务器上使用Google Gemini AI在个人的Telegram机器人中。 First, you will need to set up your own server to host the Telegram bot. Once that is done, you can proceed with integrating Google Gemini AI into your bot. To start, you will need a Google Cloud Platform account. Go to the Google Cloud Console and create a new project if you haven't done so already. Next, enable the Google Gemini API for your project. This can be done by navigating to the APIs & Services section and selecting the Library tab. Search for "Google Gemini API" and click on the enable button. After enabling the API, you will need to create credentials. Go to the Credentials section and click on the Create credentials button. Choose Service account and fill in the necessary details. Once the credentials are created, you will have a JSON file downloaded to your computer. This file contains the necessary authentication information for accessing the Google Gemini API. Keep it secure and note its location. Next, you will need to install the Python Telegram Bot library. Open your terminal and run the following command: `pip install python-telegram-bot` Once the library is installed, you can start coding your Telegram bot. Import the required libraries and create a new bot instance using your bot token. To integrate Google Gemini AI, you will need to use the Google Cloud Client Libraries for Python. Install the libraries by running the following command in your terminal: `pip install --upgrade google-cloud-gemini` After installing the libraries, import the necessary modules in your bot code. You will need the `google.cloud.gemini` module to make text requests to the Google Gemini AI. To use Google Gemini AI, you will need to create an instance of the `LanguageServiceClient` class from the `google.cloud.gemini` module. Use your JSON file with the authentication information to create the client. Once the client is created, you can use the `analyze_text` method to send text for analysis. Pass the text you want to analyze as a parameter to the method and obtain the response. You can then process the response to extract the desired information. Finally, your Telegram bot is ready to use Google Gemini AI on your own server. Start the bot and test it to see the results of the Google Gemini AI analysis. 记住要保持HTML结构,以下是将英文文本翻译为简体中文的内容: 如何在您自己的服务器上使用Google Gemini AI在个人的Telegram机器人中。 首先,您需要设置自己的服务器来托管Telegram机器人。完成这一步后,您可以开始将Google Gemini AI整合到您的机器人中。 首先,您需要一个Google Cloud平台账户。转到Google Cloud控制台,如果尚未创建新项目,请创建一个新项目。 接下来,请为您的项目启用Google Gemini API。您可以在API和服务部分选择库选项卡,搜索“Google Gemini API”,然后单击启用按钮。 启用API后,您需要创建凭据。转到凭据部分,然后单击“创建凭据”按钮。选择“服务帐号”并填写必要的详细信息。 凭据创建完成后,会下载一个JSON文件到您的计算机上。该文件包含访问Google Gemini API所需的身份验证信息。请将其安全地保存并记录其位置。 接下来,您需要安装Python Telegram Bot库。打开您的终端并运行以下命令: `pip install python-telegram-bot` 然后,在安装了库之后,您可以开始编写Telegram机器人的代码。导入所需的库,并使用机器人令牌创建一个新的机器人实例。 为了整合Google Gemini AI功能,您需要使用用于Python的Google云客户端库。通过在终端中运行以下命令安装这些库: `pip install --upgrade google-cloud-gemini` 在安装了这些库之后,将必要的模块导入到您的机器人代码中。您需要`google.cloud.gemini`模块来向Google Gemini AI发送文本请求。 为了使用Google Gemini AI功能,您需要从`google.cloud.gemini`模块中创建一个`LanguageServiceClient`类的实例。使用包含身份验证信息的JSON文件创建客户端。 创建客户端后,您可以使用`analyze_text`方法发送文本进行分析。将要分析的文本作为参数传递给该方法并获取响应。然后,您可以处理响应以提取所需的信息。 最后,您的Telegram机器人已经可以在您自己的服务器上使用Google Gemini AI功能了。启动机器人并进行测试,查看Google Gemini AI分析的结果。

介绍

最近,谷歌将其生成式语言模型从巴德更名为双子座,并引入了官方软件开发工具包(SDK),用于与双子座API进行协作的多种编程语言。这次转变不仅仅是名称的改变,它标志着语言模型的显著升级。根据基准测试,谷歌的免费版本双子座专业版在某些方面甚至具有更好的性能,成为OpenAI的ChatGPT的严肃竞争对手。

作为 Telegram 的日常用户,一款功能丰富且多才多艺的即时通讯应用,我将其作为我的沟通需求工具。Telegram 超越了简单的通信工具,发展成了一个全面的平台。如果你正在阅读这篇文章,很有可能你已经熟悉 Telegram 及其机器人功能了。

所以我使用Gemini官方的Python SDK制作了一个Telegram机器人,以将Gemini的聊天功能无缝集成到Telegram环境中。为了保持独有性,我将GeminiBot的访问权限限制在了我的个人Telegram账户上。这个限制是由于Gemini Pro的使用限制,最大的API调用速率为每分钟60个请求。然而,对于那些想要为公众使用创建Telegram机器人的人,我推荐使用Gemini Ultra,以便去除API调用的限制。

在整个故事中,我将尝试解释如何利用GeminiBot的存储库,提供有关如何构建和维护您的个性化聊天机器人的见解。

特点

  • 与谷歌的Gemini AI聊天机器人参与在线对话
  • 保留对话记录以进行持续或开始新的讨论
  • 发送带有标题的图片以根据图像内容获取回复。例如,机器人可以读取图像中的文本并将其转换为文字。
  • Preview of GeminiBot

项目结构

这是GeminiBot项目的主要结构:

.
├── bot
│ ├── conversation_handlers.py
│ ├── __init__.py
│ └── __pycache__
├── core.py
├── database
│ ├── database.py
│ ├── __init__.py
│ └── __pycache__
├── Docs.md
├── helpers
│ ├── helpers.py
│ ├── __init__.py
│ ├── inline_paginator.py
│ └── __pycache__
├── __init__.py
├── main.py
├── pickles
│ └── __init__.py
├── README.md
├── requirements.txt
├── .env
└── safety_settings.json

为了部署和使用的方便,我选择了SQLite3作为数据库,用于存储每个用户的对话记录。它通过存储与Telegram用户账号ID相关的对话,确保在您的机器人为多个用户提供服务时用户无法访问他人的对话。为了保存Gemini对话对象以便加载以前的对话,我使用了Pickles,并将它们存储在pickles文件夹中。

在 core.py 中,有一个用于与 Gemini 的 Python SDK 进行通信的 Python 类,您可以轻松自定义。safety_settings.json 文件旨在与 Gemini 的安全设置政策保持一致,让您可以限制每个对话的主题,例如仇恨言论等。

在.env文件中,您应该设置您的Telegram机器人令牌、Gemini API密钥和您的Telegram账户ID。如果您需要更改用户限制,请参考bot/conversation_handlers.py文件中的restricted装饰器。

在这个故事中,我不会解释如何使用BotFather创建Telegram机器人,也不会解释如何获取Gemini API的API密钥。作为开发者,你可能已经熟悉了这些步骤。你可以参考Telegram文档和谷歌的文档获取API密钥的指导。

您的.env文件应该类似以下格式:

TELEGRAM_BOT_TOKEN=<Your Telegram Bot Token>
GEMINI_API_TOKEN=<Your Gemini API key>
AUTHORIZED_USER=<Your Telegram account ID number>

为了获取您的Telegram帐户ID,您可以在Telegram中向Show Json Bot发送一条消息。它将以JSON格式显示您的帐户信息,并且您可以在机器人回复的JSON文件的聊天部分中找到您的帐户ID。请注意,帐户ID与您的用户名不同,并且对于Telegram帐户来说是不可更改的,除非您删除帐户并创建另一个。

如果您想修改用户限制,您可以查看bot/conversation_handlers.py文件中的restricted装饰器。

在Linux服务器上部署和运行Telegram机器人

为部署您的机器人,有几种方式可供选择,如Heroku、PythonAnywhere或其他PaaS提供商。我更倾向于使用裸机的Linux VPS和supervisor。首先在您的Linux机器上克隆这个项目:

git clone https://github.com/sudoAlireza/GeminiBot.git

接下来,在项目的根目录中创建.env文件,并使用从谷歌和电报获得的令牌和ID填充变量。下一步,在您的虚拟环境中使用以下代码安装项目要求。

python3 -m venv venv - prompt GeminiBot
source venv/bin/activate
pip install -r requirements.txt

而且要使用Supervisor来将您的机器人作为系统进程运行,请先安装它。

sudo apt update
sudo apt install supervisor -y

并在supervisor conf.d内创建一个以您偏好的项目名称命名的文件。

sudo touch /etc/supervisor/conf.d/gemini_bot.conf # Instead of gemini_bot you can put your prefered name
sudo vim /etc/supervisor/conf.d/gemini_bot.conf # If you don't like vim, you can use nano

将此模板用于gemini_bot.conf,并配置。

[program:gemini_bot]
command=<PROJECT_DIR>/venv/bin/python3 <PROJECT_DIR>/main.py
directory=<PROJECT_DIR>
restarts=4
autostart=true
autorestart=true
log_stderr=true
log_stdout=true
stderr_logfile=/var/log/supervisor/gemini_bot.err.log
stdout_logfile=/var/log/supervisor/gemini_bot.out.log

然后更新和运行Supervisor。

sudo systemctl status supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start gemini_bot

如果您没有错过任何信息,您可以在Telegram中使用您的机器人。

进一步发展的想法

  • 从历史记录中删除特定的对话:在这个机器人的第一个版本中,用户可以看到他们在历史记录中保存的对话,并且通过点击对话代码,他们可以继续之前中断的对话。在将来的版本中,用户将可以通过一个按钮逐个删除对话,或一次性删除全部对话。
  • 为图像部分添加对话功能: 在当前版本中,用户可以向机器人发送带有标题的图像,并使用gemini-pro-vision模型询问或讨论图像。然而,由于当前SDK的限制,他们无法像gemini-pro模型一样进行保持对话。我们可以使用提示来实现自定义逻辑以解决这个问题。
  • 处理多条消息中的长回复:由于Telegram发送超过4096个字符的限制,回复长消息存在一些挑战。我将找到一种解决方案,在多条消息中处理它们,避免出现Markdown问题和其他相关问题。
  • 添加测试和促进部署:测试至关重要,没有测试的项目基本上是毫无价值的。我将致力于添加测试来确保代码的健壮性。此外,我还将致力于简化部署过程,以提高易用性。

请随意在这里分享您的想法,或在GeminiBot GitHub存储库中提出问题和拉取请求。我在项目文档中创建了一个待办事项列表,以跟踪即将发生的发展。

您可以在以下网址找到该项目的英文文档: https://github.com/sudoAlireza/GeminiBot 若需将其翻译为简体中文,请使用翻译工具或Hugging Face的翻译API。

2024-02-15 04:13:29 AI中文站翻译自原文