人工智慧能否僅透過分析螢幕截圖就能檢測並修復編碼錯誤?答案是肯定的。這種創新方法利用人工智慧和推理從影像中識別編碼錯誤,提出準確的解決方案,並解釋其背後的邏輯。該系統的核心是一個分散的多代理系統,在該系統中,自主代理(如人工智慧模型、工具或服務)協同工作。每個代理都會收集資料、做出本地化決策,併為解決複雜的除錯任務做出貢獻。透過將這一過程自動化,開發人員可以節省時間、提高準確性,並避免手動線上搜尋解決方案的麻煩。
- 瞭解“推理多代理系統”及其如何自動檢測錯誤並根據螢幕截圖生成解決方案。
- 探索人工智慧在提高多代理系統(Multi-Agent System with Reasoning)軟體除錯效率方面的作用。
- 瞭解 Griptape 如何透過模組化工作流程簡化多代理系統的開發。
- 利用人工智慧模型實施多代理系統,從截圖中檢測編碼錯誤。
- 利用視覺語言模型和基於推理的 LLM 自動檢測和解釋錯誤。
- 構建和部署專門從事網路搜尋、推理和影像分析的人工智慧代理。
- 開發結構化工作流,以高效提取、分析和解決編碼錯誤。
- 最佳化多代理系統實施的安全性、可擴充套件性和可靠性。
多代理系統簡介
多代理系統(MAS)是由眾多互動式智慧代理組成的複雜框架,每個代理都擁有獨特的技能和目標。這些代理可以採取各種形式,包括軟體應用程式、機器人實體、無人機、感測器甚至人類,或這些元素的混合體。多智慧體系統的主要目的是透過利用集體智慧、協作和代理間協調努力的力量,應對單個代理難以獨立應對的挑戰。
多代理系統的顯著特點
- 自主性:每個代理都具有一定程度的自治功能,可根據對周圍環境的區域性瞭解做出選擇。
- 權力下放:權力分散到各個代理,即使某些部分出現故障,系統也能保持執行。
- 自我組織:代理有能力根據突發行為進行自我調整和安排,從而實現有效的任務分配和衝突管理。
- 即時功能:MAS 可對動態狀況做出迅速反應,無需人工監督,因此非常適合應急響應和交通管制等場景。
多代理系統的一些例項
多代理系統透過實現自主代理之間的智慧協作,正在改變各行各業。以下是一些實際案例,展示了它們在現實世界中的應用。
- 動態解決查詢的代理:這是一個複雜的多代理系統,旨在有效解決客戶諮詢。它首先利用其廣泛的知識庫,並在必要時從整合工具中檢索相關資訊,以提供準確的答案。
- 動態分配票單:這一先進的多代理系統可將收到的支援票據自動分配給最合適的代理,從而簡化客戶支援部門的票據管理工作流程。它利用生成式人工智慧,根據類別、嚴重程度和代理專業化等既定標準對每張票單進行評估。
- 分析知識庫缺口的代理:這個專門的多代理系統旨在透過準確定位需要在現有文章中進行更好覆蓋的重複性支援挑戰來提高知識庫的效率。透過使用生成式人工智慧,該代理可檢查支援票單和客戶諮詢的趨勢,以確定需要改進的領域。
使用Griptape構建多代理系統
Griptape 框架透過模組化設計和安全的工作流程平衡了可預測性和創造性,從而簡化了協作式人工智慧代理的開發過程。
代理專業化與協調
Griptape 使開發人員能夠定義具有不同角色的代理,例如
- 研究型代理:使用網路搜尋和刮擦等工具收集資料
- 寫作代理:將洞察力轉化為針對特定受眾的敘述
- 分析代理:根據預定義模式或業務規則驗證輸出結果
代理透過工作流進行互動,在保持依賴關係的同時並行執行任務。例如,一個研究代理的發現可以觸發多個寫作代理同時生成內容
工作流設計
該框架支援兩種方法:
- 順序流水線:用於線性任務執行(例如,資料攝取 → 分析 → 報告)
- 基於 DAG 的工作流:適用於複雜的分支邏輯,代理可根據中間輸出進行動態調整
安全性和可擴充套件性
主要保障措施包括
- 非提示資料處理:最大限度地減少 LLM 互動過程中敏感資訊的暴露
- 許可權控制:根據代理角色限制工具的使用
- 雲端整合:透過 Griptape Cloud 等服務獨立部署代理,實現橫向擴充套件
實施最佳實踐
- 使用規則集強制執行代理行為(例如,格式化標準、道德準則)
- 充分利用記憶體型別: 短期記憶用於創意任務,長期記憶用於結構化流程
- 在本地測試工作流程,然後再部署到分散式環境中
Griptape 的模組化架構透過優先使用 Python 程式碼進行邏輯定義,減少了對提示工程的依賴,使其成為客戶支援自動化和即時資料分析管道等企業級應用的理想選擇。
開發用於解決編碼錯誤的多代理系統的實踐實施
在本教學中,我們將建立一個多代理系統,旨在從編碼截圖中自動檢測錯誤,具體以 Python 為例。該系統不僅能識別錯誤,還能為使用者提供清晰的解錯說明。在整個過程中,我們將結合使用視覺語言模型和基於推理的大型語言模型,以增強我們的多代理框架的功能。
第 1 步:安裝和匯入必要的庫
首先,我們將安裝以下所有必需的庫:
!pip install griptape !sudo apt update !sudo apt install -y pciutils !pip install langchain-ollama !curl -fsSL https://ollama.com/install.sh | sh !pip install ollama==0.4.2 !pip install "duckduckgo-search>=7.0.1" import os from griptape.drivers.prompt.ollama import OllamaPromptDriver import requests from griptape.drivers.file_manager.local import LocalFileManagerDriver from griptape.drivers.prompt.openai import OpenAiChatPromptDriver from griptape.loaders import ImageLoader from griptape.structures import Agent from griptape.tools import FileManagerTool, ImageQueryTool from griptape.tasks import PromptTask, StructureRunTask from griptape.drivers.structure_run.local import LocalStructureRunDriver from griptape.structures import Agent, Workflow from griptape.drivers.web_search.duck_duck_go import DuckDuckGoWebSearchDriver from griptape.structures import Agent from griptape.tools import PromptSummaryTool, WebSearchTool
第 2 步:執行Ollama伺服器並提取模型
以下程式碼將啟動 ollama 伺服器。我們還從 ollama 提取了“minicpm-v”模型,以便使用該視覺模型從手寫筆記中提取文字。
import threading import subprocess import time def run_ollama_serve(): subprocess.Popen(["ollama", "serve"]) thread = threading.Thread(target=run_ollama_serve) thread.start() time.sleep(5) !ollama pull minicpm-v
現在,我們還需要設定以下開放式人工智慧應用程式介面金鑰,以便與 Griptape 上的 Ollama 模型聊天
import os os.environ["OPENAI_API_KEY"] = ""
我們還將利用功能強大的 LLM,透過解釋編碼錯誤和所提供解決方案的充分上下文來協助推理。為此,我們使用了 phi4-mini 模型。
!ollama pull phi4-mini
第 3 步:建立分析截圖的代理
我們首先建立一個代理來分析 Python 編碼錯誤的螢幕截圖。該代理在後臺利用了視覺語言模型 (minicpm-v)。
images_dir = os.getcwd() def analyze_screenshots(): driver = LocalFileManagerDriver(workdir=images_dir) return Agent( tools=[ FileManagerTool(file_manager_driver=driver), ImageQueryTool( prompt_driver=OllamaPromptDriver(model="minicpm-v"), image_loader=ImageLoader(file_manager_driver=driver) ), ])
第 4 步:建立網路搜尋和推理代理
然後,我們建立了兩個代理,一個用於搜尋編碼錯誤的可能解決方案,另一個用於推理錯誤及其解決方案。
def websearching_agent(): return Agent( tools=[WebSearchTool(web_search_driver=DuckDuckGoWebSearchDriver()), PromptSummaryTool(off_prompt=False)], ) def reasoning_agent(): return Agent( prompt_driver=OllamaPromptDriver( model="phi4-mini", ))
第 5 步:定義分析截圖、查詢解決方案和提供推理的任務
我們使用程式碼片段截圖(見第 6 步)進行自動評估。我們將其儲存在當前工作目錄下,名為“sample.jpg”。這是一份手寫答卷。這個代理系統首先會從編碼截圖中提取錯誤,並找出可能的解決方案。然後,它將提供錯誤及其解決方案背後的充分理由。
image_file_name = "pythonerror1.jpg" team = Workflow() screenshotanalysis_task= StructureRunTask( ( """Extract IN TEXT FORMAT ALL THE LINES FROM THE GIVEN SCREEN SHOT %s"""%(image_file_name), ), id="research", structure_run_driver=LocalStructureRunDriver( create_structure=analyze_screenshots, ), ) findingsolution_task =StructureRunTask( ( """FIND SOLUTION TO ONLY THE CODING ERRORS FOUND in the TEXT {{ parent_outputs["research"] }}. DO NOT INCLUDE ANY ADDITIONAL JUNK NON CODING LINES WHILE FINDING THE SOLUTION. """, ),id="evaluate", structure_run_driver=LocalStructureRunDriver( create_structure=websearching_agent, ) ) reasoningsolution_task = StructureRunTask( ( """ADD TO THE PREVIOUS OUTPUT, EXPANDED VERSION OF REASONING ON HOW TO SOLVE THE ERROR BASED ON {{ parent_outputs["evaluate"] }}. DO INCLUDE THE WHOLE OUTPUT FROM THE PREVIOUS AGENT {{ parent_outputs["evaluate"] }} AS WELL IN THE FINAL OUTPUT. """, ), structure_run_driver=LocalStructureRunDriver( create_structure=reasoning_agent, ) )
第 6 步:執行工作流程
現在,我們將執行工作流程。
screenshotanalysis_task.add_child(findingsolution_task) findingsolution_task.add_child(reasoningsolution_task) screenshotanalysis_task.add_child(reasoningsolution_task) team = Workflow( tasks=[screenshotanalysis_task,findingsolution_task,reasoningsolution_task], ) answer = team.run() print(answer.output)
輸入截圖
代理系統的輸出
Certainly! Here is an expanded explanation of how you can solve this error in Python:When working with strings and integers together, it's important that both elements are either numbers (integers or floats) for numerical operations like addition. In your case, you're trying to concatenate a string ("hello world") The error occurs because Python does not allow direct concatenation of strings and integers without explicitly handling them as separate types first (i.e., by conversion). The solution is straightforward: convert both elements to com Here's an expanded explanation along with your corrected code:```pythontry:# Initialize variable 'a' as 1234 (an integer)a = 1234# Convert 'a' from int to str and then concatenate" hello world" print(str(a) + "hello world")except Exception as error: # Catch any exceptions that might occur print("Oops! An exception has occured: ", error)# Print the type of the caught exception for debugging purposes. print("Exception TYPE:", type (error))# Explicitly stating what class TypeError is expected in this context,# though it's redundant since we've already captured and printed it above.print("Exception TYPE: <class 'TypeError'>")In summary, converting an integer to a string before concatenation solves the issue by ensuring both elements are strings. This allows for seamless addition (concatenation) of these two pieces into one coherent output.Remember that this approach is not limited just to adding integers and strings; it's applicable whenever you need to concatenate different data types in Python, provided they can be converted or handled as compatible formats first.
從上述輸出中可以看出,不僅錯誤得到了正確的解釋和充分的推理,而且解決方案也提供了足夠的推理。
使用其他查詢進行分析
讓我們用另一個查詢截圖試試不同的 Python 編碼問題
代理系統的輸出
Certainly! Here is an expanded explanation of how we can solve any potential issues with this corrected version:1. **Syntax Correction**: The original error was due to improper indentation, which caused a `IndentationError. In Python, proper block structure and consistent indentations are crucial for the interpreter. 2. **Handling ZeroDivisionError Properly**:- A division by zero is an exceptional case that needs special handling.- Using try-except blocks allows us gracefully handle this exception without crashing our program unexpectedly (i.e., `ZeroDivisionError").3. **Logging and Debugging Information**:- The use of the logging module (log.debug`) helps in tracking what values are being processed, which is useful for debugging.Here's a complete example with proper indentation:```pythonimport logging# Configure logger to display debug level messages.logging.basicConfig(level-logging.DEBUG)log = logging.getLogger(____name___)def division(a, b):log.debug(f"Dividing {a} by {b}.")try:return a / b # Attempt the division operation hereexcept ZeroDivisionError as e: # Catch and handle zero-division error specifically. log.exception("Oh noes!") # Log an exception message with traceback# Example usage of function to demonstrate logging outputif __name_ "_main_":print(division(10, 2))try:division (5, 0) except Exception as e:pass# Should work fine# This should raise a ZeroDivisionError and log it.# Catch any unexpected exceptions (though unlikely here)
從上述輸出中可以看出,錯誤得到了正確的解釋和充分的說明。此外,還提供了避免該錯誤的替代編碼解決方案,並給出了充分的理由。
讓我們用另一個不同 Python 編碼問題的查詢截圖來試試。
代理系統的輸出
Certainly! Here's an expanded version of how to solve this error based on the previous output:1. **Identify Missing Dependency:**The error message indicates that there is a missing dependency, specifically for the `packaging module which setuptools requires.2. **Install 'packaging' Package Manually (if not already installed):*****bashpip install packaging3. **Upgrade Setuptools to Ensure Compatibility:**Upgrading setuptools can help resolve any compatibility issues and ensure that all dependencies are correctly managed:***bashpip install --upgrade setuptools4. **Re-run the Installation Command:**After installing `packaging` manually (if it wasn't installed previously) or upgrading setuptools, re-execute your original installation command to see if this resolves any issues.5. **Verify Dependency Resolution and Reinstallation Attempts:**If you encounter further errors related specifically to missing dependencies after following the above steps:- Check for additional required packages by reviewing error messages.- Install those specific requirements using pip, e.g., `pip install <missing-package-name>`.6. **Check Environment Consistency:**Ensure that your Python environment is consistent and not conflicting with other installations or virtual environments:***bash# List installed packages to verify consistency across different setups (if applicable)pip list# If using a specific version of setuptools, ensure it's correctly configured:7. **Consult Documentation:**Refer to the official documentation for both `packaging and `setuptools if you encounter persistent issues or need more detailed guidance on resolving complex dependency problems.8. **Seek Community Help (if needed):**If after following these steps, you're still facing difficulties:- Post a question with specific error messages in relevant forums like Stack Overflow.- Provide details about your environment setup and the commands you've run for better assistance from community members or experts.By carefully addressing each step above based on what you encounter during installation attempts (as indicated by any new errors), you'll be able to resolve missing dependencies effectively. This systematic approach ensures that all required packages are correctly installed
小結
整合多代理系統 (MAS) 從螢幕截圖中自動檢測編碼錯誤可顯著提高開發人員的效率。透過利用人工智慧和 Griptape 等工具,這種方法可以提供及時、準確的解決方案,並提供詳細的推理,從而為開發人員節省寶貴的時間。此外,MAS 的靈活性和可擴充套件性可應用於各行各業,實現無縫任務管理並提高工作效率。
- 整合自動化系統,從螢幕截圖中識別編碼錯誤,透過提供帶有詳細推理的準確解決方案,減少手動錯誤搜尋的需要,從而為開發人員節省大量時間。
- MAS 是一種去中心化架構,使用自主代理協作解決複雜問題,增強了跨行業的任務管理和可擴充套件性。
- Griptape 框架簡化了多代理系統的開發,提供模組化設計、代理專業化、安全工作流和可擴充套件性,是企業級人工智慧解決方案的理想選擇。
- MAS 可動態適應不斷變化的條件,是編碼錯誤檢測、客戶支援自動化和資料分析等即時應用的理想選擇。
評論留言