• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • 3 月 19 日下午 2 點,鎖定 NVIDIA AI 網絡中文專場。立即注冊觀看
    生成式人工智能/大語言模型

    使用 NVIDIA AgentIQ 開源工具包改進 AI 代碼生成

    隨著 NVIDIA AgentIQ (用于連接和優化 AI 智能體團隊的開源庫) 的發布,開發者、專業人士和研究人員可以創建自己的代理式 AI 應用。本教程通過 AI 代碼生成示例,向您展示如何在 AgentIQ 中開發應用。我們將使用 LangGraph 和推理模型構建測試驅動的編碼代理,以擴展測試時計算。

    在預訓練、后訓練和推理中, Scaling laws 正在推動更智能的 AI 系統的發展。 大語言模型 (LLMs) 的大規模預訓練提供了令人印象深刻的結果,但進一步擴展具有挑戰性。 自主 AI 智能體 和測試時計算方法 (例如 Deepseek-R1 使用的方法) 通過擴展后訓練和推理計算提供了顯著的改進。在為邏輯、數學或編碼等復雜任務構建 agentic 工作流時,這一點勢在必行。

    這些新穎的擴展方法更易于與 AgentIQ 結合使用,因為組織可以更好地設計、測試、部署和優化其 AI 智能體應用。我們來深入探討如何在 AgentIQ 中改進 AI 代碼生成工作流。

    為何使用 AgentIQ 構建編碼智能體

    LLM 擅長編碼任務,但僅限于聊天界面,缺乏自主性和與現實世界的集成。相比之下,由這些 LLM 提供支持的 AI 智能體旨在實現現實世界的目標。他們通常使用工具、內存和規劃來執行文件編輯、代碼執行或信息搜索等任務,從而與環境進行交互。

    AI 智能體設計注意事項

    AI 智能體是擴展推理時計算以提高 AI 性能的示例之一。要構建智能體或 多智能體系統 ,您必須平衡靈活性和結構。

    靈活的智能體可能會被賦予 shell、代碼編輯器和 Web 瀏覽器,并且只需執行最少的指令即可。相比之下,結構化智能體可能包含預定義的步驟,例如在更大的代碼庫中定位失敗的測試用例,然后執行代碼更改,直到錯誤得到解決。流工程是一個熱門的中間地帶,其中定義了狀態和轉換,并在每個狀態內執行代理或工具。

    推理模型和搜索方法是另一個需要推理時間計算的示例。在提供最終輸出之前,DeepSeek-r1OpenAI o1 等推理模型會花費額外的時間在單個思維鏈中探索各種推理路徑和解決方案。Beam Search 等搜索方法還會利用評分函數 (例如可驗證的結果或近似值) 來探索各種分支。

    借助 AgentIQ 輕松開發 AI 智能體

    評估、部署和優化是開發者可以使用 AgentIQ 解決的一些常見挑戰。下表總結了 AgentIQ 的一些特性和優勢。

    特征 優勢
    包含 Agent Framework 生態系統 繼續使用您喜歡的工具(例如 LangGraph 和 CrewAI)進行構建。
    通用規格 支持跨項目的可復用性和兼容性,包括 AgentIQ 中的許多示例。項目可以通過 AgentIQ 注冊系統共享。
    評估工具 工作流程的快速開發和迭代。定義一組預期輸出,并通過更新配置文件輕松測試不同的模型、工具和工作流程。
    內置部署選項 使用 aiq serve 輕松啟動微服務,或利用開源的 chatbot 風格用戶界面。
    優化功能 使用工作流分析器識別瓶頸,并利用并行工具調用和與 NVIDIA Dynamo 集成等功能實現出色性能。
    可觀察性 通過與 Phoenix、OpenTelemetry Collector 和自定義提供程序緊密集成進行監控和調試。
    表 1。AgentIQ 的特性和優勢

    請參閱文檔或 GitHub ,了解功能的詳細列表。

    教程預備知識

    您需要以下設置:

    如何在 NVIDIA AgentIQ 中構建 AI 代碼生成智能體

    您將學習如何集成 AI 智能體和推理模型,以在 AgentIQ 中創建 AI 代碼生成智能體。我們使用 LangGraph 構建核心代理,集成用于安全和控制的沙盒代碼執行工具,并使用 DeepSeek-r1 增強糾錯功能。最后,我們展示了如何使用監督代理將智能體集成到更大的系統中。

    設置項目支架

    首先,克隆 NVIDIA AgentIQ GitHub 資源庫。按照 README 中的說明安裝 AgentIQ 庫。

    現在,使用 AIQ 支架命令創建新項目模板。支架將包含一個默認工作流程和配置文件。

    aiq workflow create code_gen_example

    NVIDIA AgentIQ 將智能體工作流和可調用工具的概念統一到一個類(即函數)下。我們可以將代碼生成代理作為函數實現,并將其用作監督代理(例如 ReACT 代理 )中的可調用工具。其他代理(例如研究代理、錯誤定位代理或測試生成代理)可以由 supervisor 管理并異步啟動,以處理復雜的任務。

    代碼生成代理的輸入將是問題語句、待修復代碼和單元測試。該代理遵循一個簡單的過程:

    1. 給出問題陳述 (例如 GitHub issue) 、代碼修復和單元測試,智能體使用代碼 LLM 進行代碼生成,以創建 git 補丁來解決問題。
    2. 更新后的代碼將在安全代碼執行沙盒中針對單元測試運行。
    3. 如果測試失敗,推理模型將根據輸出提出更改建議。
    4. 重復執行第 1-3 步,直到生成的代碼通過所需的單元測試,或者超過迭代次數上限。

    更新配置文件

    AgentIQ 中的配置文件定義了整個工作流程。通過更新配置文件 (例如添加工具 (函數) 、交換 LLM 或更改其他組件) ,可以通過 aiq eval cli 命令快速迭代代理工作流。

    scaffold 命令會創建一個默認配置文件。更新三個部分:functions,llms 和 workflow。functions 部分包含可供 agents 使用的工具,llms 部分定義可供 agents 和 tools 使用的模型,workflow 是主要入口點。在此處,將 workflow 類型指定為 react_agent ,這將使用 AgentIQ 工具包中的默認 ReACT 代理。

    functions:
      code_gen_tool:
        _type: code_gen_tool
        debug_llm: reasoning_llm
         code_llm: code_generation_llm
        max_iterations: 3
     
    llms:
      reasoning_llm:
        _type: nim
        model_name: deepseek-ai/deepseek-r1
        max_tokens: 8000
      code_generation_llm:
        _type: nim
        model_name: qwen/qwen2.5-coder-32b-instruct
        max_tokens: 2048
      general_llm:
        _type: nim
        model_name: meta/llama-3.3-70b-instruct 
        max_tokens: 2048
     
     
    workflow:
      _type: react_agent
      tool_names:
        - code_gen_tool
      llm_name: general_llm
      verbose: true
      retry_parsing_errors: true

    在本示例中,所有三個 LLM 均配備 NVIDIA NIM ,可通過 NVIDIA API Catalog 訪問或在本地托管。OpenAI 和其他 LLM 提供商也得到支持。訪問文檔了解詳情。

    實現代碼生成函數

    創建配置文件中引用的代碼生成函數。在項目支架中,打開 register.py 文件并添加:

    class CodeGenToolConfig(FunctionBaseConfig, name="code_gen_tool"):
     
        reasoning_llm: str
     
        code_llm: str
     
        max_iterations: int = 5
     
    @register_function(config_type=CodeGenToolConfig)
     
    async def code_generation(config: CodeGenToolConfig, builder: Builder):

    在此函數中,我們將定義輔助函數和主要可運行函數 _code_gen_tool,以便在調用工具時運行。通過四個步驟實施 LangGraph 工作流程:

    1. 用戶 (或其他智能體) 輸入問題陳述 (例如 GitHub issue) 、待修復的代碼以及應通過或待修復的單元測試。系統會提示代理使用配置的編碼 LLM 創建一個 git 補丁來解決問題。

    2. 更新后的代碼在代碼執行工具中運行,以評估結果。

    3. 如果測試失敗,系統會提示推理模型根據問題陳述、代碼和測試輸出提出更改建議。4。重復執行第 1-3 步,直到生成的代碼通過所需的單元測試,或者超過迭代次數上限。

    workflow = StateGraph(CodeState)
     
    workflow.add_node("code_generation", generate_code)
     
    workflow.add_node("run_unit_test", test_code)
     
    workflow.add_node("debug", debug_code)
     
    workflow.add_edge(START, "code_generation")
     
    workflow.add_edge("code_generation", "run_unit_test")
     
    workflow.add_conditional_edges(
     
        "run_unit_test",
     
        should_continue,
     
        {
     
            "end": END,
     
            "debug": "debug"
     
        }
     
    )
     
    workflow.add_edge("debug", "code_generation")
     
    agent = workflow.compile()

    LangGraph 智能體中的每個節點都在 Python 函數中定義,該函數可以是自主智能體、工具調用或任何其他函數。 generate_code 節點使用 Qwen NIM 生成代碼 run_unit_test 節點針對沙盒環境中更新的代碼運行測試 debug 節點使用 Deepseek-R1 進行故障高級推理

    AgentIQ 使用 yield 將函數注冊為可從任何其他函數調用的函數。提供詳細且準確的函數描述對于開發相互有效交互的智能體至關重要。

    yield FunctionInfo.from_fn(
     
         _code_generation,
     
            description=("This tool is a code generation agent using test driven development. Provide input including the issue, current code, and unit tests."))
    Flowchart showing the code modification agent workflow: user input, code generation with NVIDIA NIM, unit test execution with a sandbox code execution tool, and reflection and debugging with NVIDIA NIM reasoning model.
    圖 1。代碼修改代理圖

    在本教程中,我們省略了 LangGraph 工作流的一些實現細節。 AgentIQ 示例目錄 包含各種完整示例,可供開始使用。

    運行示例工作流程

    AgentIQ 為 CLI 提供各種功能,包括運行工作流程、啟動服務器和執行評估。

    直接運行工作流程:

    aiq run --config_file=examples/code_gen_example/configs/config.yml --input 'Write a Python function named largest_rectangle that computes the area of the largest rectangle in the histogram. Given an array heights of non-negative integers representing the histogram bar heights where the width of each bar is 1, return the area of the largest rectangle that can be formed within the histogram. Use the following files:
    test_path: "/home/aiq/rectangle_tests.py",
    solution_path: “/home/aiq/rectangle_solution.py"'

    日志將顯示在控制臺中,并且智能體可以輕松地與 AgentIQ 用戶界面集成。

    以下是輸出示例。

    Configuration Summary:
    --------------------
    Workflow Type: react_agent
    Number of Functions: 1
    Number of LLMs: 3
    Number of Embedders: 0
    Number of Memory: 0
    Number of Retrievers: 0
     
    2025-02-27 17:33:27,459 - aiq.agent.react_agent.agent - INFO - The user's question was: 'Write a Python function named largest_rectangle that computes the area of the largest rectangle in the histogram. Given an array heights of non-negative integers representing the histogram bar heights where the width of each bar is 1, return the area of the largest rectangle that can be formed within the histogram. Use the following files:
    test_path: "/home/aiq/rectangle_tests.py",
    solution_path: “/home/aiq/rectangle_solution.py"'
    2025-02-27 17:33:27,460 - aiq.agent.react_agent.agent - INFO - The agent's thoughts are:
    Thought: To solve this problem, we need to write a Python function that calculates the area of the largest rectangle in a histogram.
     
    Action: code_gen_tool
    Action Input: {"problem_statement": "Write a Python function named largest_rectangle that computes the area of the largest rectangle in the histogram. Given an array heights of non-negative integers representing the histogram bar heights where the width of each bar is 1, return the area of the largest rectangle that can be formed within the histogram.", "solution_path": "/home/cmunley/aiq-225-2/rectangle_solution.py","test_path": "/home/cmunley/aiq-225-2/rectangle_tests.py"}
    ===============================================================================
    STARTING NEW CODE GENERATION TASK
    ===============================================================================
    Initial Code:
     
    def largest_rectangle(heights):
     
    -------------------------------------------------------------------------------
    Generating solution...
     
    largest_rectangle(heights):
        stack = []
        max_area = 0
        index = 0
        while index < len(heights):
            if not stack or heights[index] >= heights[stack[-1]]:
                stack.append(index)
                index += 1
            else:
                top_of_stack = stack.pop()
                width = index if not stack else index - stack[-1] - 1
                area = heights[top_of_stack] * width
                max_area = max(max_area, area)
     
        while stack:
            top_of_stack = stack.pop()
            width = index if not stack else len(heights) - stack[-1] - 1
            area = heights[top_of_stack] * width
            max_area = max(max_area, area)
     
        return max_area
    -------------------------------------------------------------------------------
    Test Results:
     
    FAILED: Expected -1, got 0
    PASS
    PASS
    -------------------------------------------------------------------------------
    Test Failed - Attempt 1/3
    -------------------------------------------------------------------------------
    Analyzing errors:
     
    The error is likely due to the fact that the function is not handling the case where the input list is empty. In this case, the function should return -1, but it's currently returning 0. [truncated for the sake of this post]
    -------------------------------------------------------------------------------
    Generating updated solution...
     
    largest_rectangle(heights):
        if not heights:
            return -1
        stack = []
        max_area = 0
        index = 0
        while index < len(heights):
            if not stack or heights[index] >= heights[stack[-1]]:
                stack.append(index)
                index += 1
            else:
                top_of_stack = stack.pop()
     
                width = index if not stack else index - stack[-1] - 1
                area = heights[top_of_stack] * width
                max_area = max(max_area, area)
     
        while stack:
            top_of_stack = stack.pop()
            width = index if not stack else len(heights) - stack[-1] - 1
            area = heights[top_of_stack] * width
            max_area = max(max_area, area)
     
        return max_area
    -------------------------------------------------------------------------------
    Updated Test Results:
     
    PASS
    PASS
    PASS
    -------------------------------------------------------------------------------
    Tests passed successfully!
     
    The agent's thoughts are:
    Thought: The code generation tool has generated the Python function largest_rectangle and the unit tests have passed, indicating that the function is correct.
     
    Final Answer: The final answer is that the Python function largest_rectangle has been successfully generated and tested, and it correctly calculates the area of the largest rectangle in a histogram.

    在配置文件中添加函數以執行各種任務

    向監督代理添加功能(例如 Web 搜索或使用計算器)就像在配置文件中添加功能一樣簡單。AgentIQ 提供了許多實用工具,助您快速入門。在 AgentIQ 工具文件夾 中,查看代理默認可用工具的完整列表。

    結語

    代碼生成問題是測試時計算擴展的絕佳候選對象,因為可以確定解決方案何時正確。例如,測試驅動的開發代理可以對提出的解決方案進行迭代,而迭代次數僅受計算預算的限制。推理 LLMs(例如 DeepSeek 的 R1 模型)提供的反射可以準確地引導代碼生成模型完成調試過程。可以集成代理式工具使用、內存和規劃來改進系統。

    NVIDIA AgentIQ 庫簡化了代理式系統的開發,提供可重復使用的組件和簡單的工具包,與整個生態系統兼容,并經過優化以實現最佳性能。 通過在一個全面且經過優化的工具包中編排不同的模型、框架和工具,我們正在通過解決復雜的現實任務來改變未來的工作。

    觀看此視頻,了解如何使用 AgentIQ 分析器 注冊 AgentIQ 黑客松,學習使用開源工具包來構建實操技能,幫助您提升代理式系統。

    ?

    ?

    0

    標簽

    人人超碰97caoporen国产