使用 AI 助理自動化 Azure 文檔

Susan Sarandon
發布: 2024-10-25 06:38:29
原創
423 人瀏覽過

Automating Azure Documentation with an AI Assistant

在大規模環境中管理和記錄 Azure 資源組 (RG) 可能非常耗時且複雜。但是,如果您可以自動化產生文件的過程,不僅解釋存在哪些資源,還解釋它們之間的關係,會怎麼樣?

在本文中,我們將探討簡單的Python 腳本如何利用LLM(大型語言模型),例如OpenAIAzure OpenAI 自動從ARM 範本建立全面的Markdown 文件。這個工具的強大之處不是使用複雜的代理框架或繁重的基礎設施,而是純Python與Azure CLI和OpenAI的API等成熟工具的結合。它甚至可以與其他人工智慧提供者以及使用 Ollama 或其他類似工具的本地法學碩士一起使用。

無需複雜的代理框架

一個常見的誤解是您需要複雜的代理框架才能有效地利用法學碩士的力量。實際上,您可以使用現有工具和簡單的腳本實現強大的自動化工作流程。在這個解決方案中,我們結合了:

  1. Python:作為腳本語言,它被普遍安裝和廣泛使用。
  2. Azure CLI:從 Azure 資源組取得 ARM 範本。
  3. OpenAI API 呼叫:從 ARM 範本產生人類可讀的文件。
  4. Markdown:作為文件的輸出格式,可以輕鬆整合到任何知識庫中。

結果呢?一個乾淨、高效的腳本,無需複雜的工具或人工智慧驅動的編排即可建立文件。

Azure 助理原始碼

原始程式碼可在此 Github 儲存庫中找到:itlackey/azure-assistants。目前,它包含一個 Python 腳本,該腳本利用 Azure CLI 和 OpenAI API 從 ARM 範本產生 Markdown 文件。如果有興趣,或者我有需要,可以使用其他工具和腳本更新儲存庫以自動執行其他任務。

腳本如何工作

這個工具的核心是 document_resource_groups.py 腳本。它做了以下四件事:

  1. 取得目前 Azure 訂閱中的所有資源群組
  2. 使用 az CLI 從 Azure 資源組匯出 ARM 範本
  3. 我們解析範本並將它們傳送到OpenAI相容的API。
  4. 法學碩士用於產生可包含在知識庫中的 Markdown 文件

列出資源組

第一步是取得 Azure 訂閱中的所有資源群組。這是使用 Python 腳本中的 az CLI 命令完成的。然後我們循環遍歷它們以獲取 ARM 模板。

result = subprocess.run(
    ["az", "group", "list", "--query", "[].name", "-o", "tsv"],
    stdout=subprocess.PIPE,
    text=True,
)
resource_groups = result.stdout.splitlines()
登入後複製
登入後複製

導出 ARM 模板

同樣,使用 Azure CLI,該腳本會擷取目前訂閱中每個資源組的 ARM 範本。這些範本包含所有資源的詳細配置信息,包括其網路和安全性設定。

export_command = [
    "az", "group", "export",
    "--name", resource_group_name,
    "--include-parameter-default-value",
    "--output", "json",
]
登入後複製
登入後複製

與LLM總結

接下來,腳本將 ARM 範本傳送到 OpenAI(或 Azure OpenAI)進行匯總。這就是奇蹟發生的地方。無需深入研究複雜的代理工作流程,簡單的系統訊息使用者提示為法學碩士提供足夠的上下文來產生有洞察力的文件。

response = client.chat.completions.create(model=model, messages=messages)
登入後複製
登入後複製

提示提供了預期的輸出範本並指示法學碩士:

  • 列出並描述每個資源。
  • 解釋資源如何相互關聯。
  • 突出顯示重要的網路配置。

這使得法學碩士能夠產生結構化、易於閱讀的文檔,而不需要任何花俏的編排。

產生 Markdown 文檔

最後一步是產生包含資源組詳細資訊的 Markdown 檔案。前面的內容包括資源組名稱、日期和標籤等元資料。然後將 AI 產生的文檔新增為文檔的內容。

front_matter = f"---\n"
front_matter += f'title: "{resource_group_name}"\n'
front_matter += f"date: {date}\n"
front_matter += f"internal: true\n"
登入後複製

Markdown 是一種通用格式,可讓此輸出輕鬆整合到許多文件系統或知識管理系統中。

自訂 AI 提示

此腳本的一個關鍵功能是能夠自訂發送給 LLM 的提示。使用者可以在這裡微調他們想要的輸出類型:

  • 系統訊息:指導法學碩士產生著重於解釋資源、關係和網路的文件。

範例:

    You are an experienced Azure cloud architect helping to create reference documentation that explains the resources within an Azure Resource Manager (ARM) template.

    The documentation you create is intended for use in a knowledge base. Your role is to describe the resources in a clear and human-readable way, providing details on the following:

    - What resources exist in the ARM template.
    - How the resources relate to each other.
    - The purpose of each resource (if possible).
    - Highlighting network configurations and data locations such as storage accounts and databases.
    - Be sure to include IP addresses in the documentation when they are available.
    - Include information about virtual network peering.
    - It is very important that you also include any potential security issues that you may find.
登入後複製
  • 使用者提示:根據正在匯總的資源群組動態產生。

範例:

    Provide detailed documentation of the following ARM template for resource group: 


    {template_content}


    The purpose of this documentation is to...
登入後複製

透過保持這些提示的靈活性和簡單性,腳本可以避免過度設計,同時仍提供高品質的文件。

運行腳本

注意:在執行此腳本之前,您需要在電腦上安裝 az CLI 和 python3。

設定和執行腳本非常簡單:

  1. 登入 Azure:確保您已透過 Azure CLI 進行驗證:
result = subprocess.run(
    ["az", "group", "list", "--query", "[].name", "-o", "tsv"],
    stdout=subprocess.PIPE,
    text=True,
)
resource_groups = result.stdout.splitlines()
登入後複製
登入後複製
  1. 運行腳本產生markdown文件:
export_command = [
    "az", "group", "export",
    "--name", resource_group_name,
    "--include-parameter-default-value",
    "--output", "json",
]
登入後複製
登入後複製

此腳本處理每個資源組,產生其 ARM 模板,並在輸出目錄中建立一個 markdown 檔案。

範例輸出

以下是腳本產生的範例:

response = client.chat.completions.create(model=model, messages=messages)
登入後複製
登入後複製

此輸出簡潔可讀易於理解 - 正是您內部文件或知識庫條目所需的內容。

結論

Azure 助理是一個完美的範例,展示如何使用現有工具和基本的Python技能透過法學碩士取得強大的成果。當簡單的腳本與 Azure CLI 和 OpenAI 的 API 結合可為您的 Azure 資源組產生清晰、全面的文件時,無需複雜的代理框架。

該工具表明,透過正確的提示和堅實的結構,任何具有基本腳本編寫技能的都可以利用人工智慧來自動化雲端文檔- 使其成為任何DevOps 或基礎設施團隊的寶貴助手。

以上是使用 AI 助理自動化 Azure 文檔的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!