首页 后端开发 Python教程 使用 Python 和 Gemini (gemini--flash) 自动生成 Debian 软件包更新摘要

使用 Python 和 Gemini (gemini--flash) 自动生成 Debian 软件包更新摘要

Dec 18, 2024 pm 02:54 PM

Automating Debian Package Update Summaries with Python and Gemini (gemini--flash)

如果您正在使用类似 Debian 的发行版并且是新用户或刚刚开始作为 系统管理员 的职业生涯,您可能已经知道更新软件包的重要性使用 apt 更新。您可能还想了解每个软件包的用途,以了解有关 Linux 的更多信息。此外,系统管理员 (sysadmin) 经常需要与利益相关者沟通或记录哪些更新是紧急的或与安全相关的。

在这篇文章中,我将向您展示如何结合 Python、apt list -u 命令和 Gemini AI 来创建人类可读的待处理软件包更新摘要。


目标?

  • 使用 apt list -u 命令检索 Debian 上待处理更新的列表。 注意: 如果需要,您可以使用以下命令修改输出:
  apt list -u | awk '{ print  }' | sed 's|/.*||'
登录后复制
  • 将此列表发送给 Gemini AI(使用 Google 的生成库)。
  • 使用AI对每个包更新的重要性进行分类和总结。
  • 将结果保存为 Markdown 文件以便于分享。

要求 ?

  • Python 3.8
  • Google Gemini API 密钥
  • 所需库:pip install google-generativeai 环境
  • 基于 Debian 的系统:此脚本依赖于 apt 命令。

守则

以下是两个脚本的解决方案的细分:

apt_list.py

此脚本运行 apt list -u 来获取挂起的更新,处理输出,并使用提示功能从 Gemini AI 获取分类摘要。

import subprocess
from utils.gemini_cfg import prompt

try:
    # Run 'apt list -u' to list upgradable packages
    result = subprocess.run(["apt", "list", "-u"], capture_output=True, text=True, check=True)
    output = result.stdout  # Get command output

    # Use the Gemini AI model to summarize the updates
    summary = prompt(output)

    # Save the AI-generated summary to a Markdown file
    with open("./gemini_result.md", "w") as file:
        file.write(summary)

    print("Summary saved to gemini_result.md")

except subprocess.CalledProcessError as e:
    print("Error while running apt list:", e)
登录后复制

gemini_cfg.py

此脚本配置 Gemini API 并定义 AI 生成内容的提示功能。

import google.generativeai as genai
from environs import Env

# Load API key from .env file
env = Env()
env.read_env()
key = env("TOKEN")  # Replace with your environment variable key name

# Configure Gemini API
genai.configure(api_key=key)
model = genai.GenerativeModel("gemini-1.5-flash")

# Function to prompt Gemini AI for summaries
def prompt(content):
    message = (
        "You work as a sysadmin (Debian server infrastructure). "
        "You must create a list categorizing the importance in terms of security and priority, "
        "providing a brief summary for each package so that business managers can understand "
        "what each library is from this output of the `apt list -u` command: "
        f"{content}"
    )
    response = model.generate_content([message])
    return response.text
登录后复制
  1. 运行 apt_list.py 脚本:python apt_list.py
  2. 该脚本执行以下操作:

    • 检索待处理的 Debian 软件包更新。
    • 将列表传递给 Gemini AI 进行分类和解释。
    • 将 AI 生成的输出保存到 gemini_result.md。
  3. 打开gemini_result.md 可查看清晰、分类的更新摘要,以便于沟通。


示例输出

以下是生成的摘要的示例:

## Debian Package Update List: Priority and Security

The list below categorizes the packages available for update, considering their importance in terms of security and business operation priority. The classification is subjective and may vary depending on your company's specific context.

**Category 1: High Priority - Critical Security (update immediately)**
- **linux-generic, linux-headers-generic:** Critical kernel updates to fix security vulnerabilities.  
- **libcurl4:** Resolves potential security issues for data transfer operations.  
...

**Category 2: High Priority - Maintenance and Stability (update soon)**

* **`e2fsprogs`, `logsave`:** Packages related to ext2/ext3/ext4 file systems. Update to ensure data integrity and file system stability. **Medium-High priority.**
...

**Category 3: Medium Priority - Applications (update as needed)**

* **`code`:** Visual Studio Code editor. Update for new features and bug fixes, but not critical for system security.
* **`firefox`, `firefox-locale-en`, `firefox-locale-pt`:** Firefox browser. Updates for security fixes and new functionalities. Priority depends on Firefox usage in your infrastructure.
...

登录后复制

结论

借助一点 Python 和 Gemini AI,您可以自动化并改进 Debian 软件包更新的沟通方式。该脚本是将 AI 集成到系统管理工作流程中的良好基础。这篇文章出于教育目的,因此请注意 Gemini API 资源以及系统的安全处理。

感谢您的阅读! ?

以上是使用 Python 和 Gemini (gemini--flash) 自动生成 Debian 软件包更新摘要的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? 如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在Linux终端中使用python --version命令时如何解决权限问题? 在Linux终端中使用python --version命令时如何解决权限问题? Apr 02, 2025 am 06:36 AM

Linux终端中使用python...

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? 如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何绕过Investing.com的反爬虫机制获取新闻数据? 如何绕过Investing.com的反爬虫机制获取新闻数据? Apr 02, 2025 am 07:03 AM

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...

Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Apr 02, 2025 am 06:27 AM

Python3.6环境下加载pickle文件报错:ModuleNotFoundError:Nomodulenamed...

使用Scapy爬虫时,管道文件无法写入的原因是什么? 使用Scapy爬虫时,管道文件无法写入的原因是什么? Apr 02, 2025 am 06:45 AM

使用Scapy爬虫时管道文件无法写入的原因探讨在学习和使用Scapy爬虫进行数据持久化存储时,可能会遇到管道文�...

See all articles