Auto-GPT是一種開源AI工具,它利用OpenAI的GPT-4或GPT-3.5 API來實現以自然語言表達的用戶定義目標。它通過將主要任務解剖為較小的組件並在循環過程中自主利用各種資源來做到這一點。
在本指南中,我們將探討AutoGPT的各種功能,並概述使用AutoGPT進行編碼的最佳實踐和技巧。
git clone https://github.com/significant-gravitas/auto-gpt.git克隆回購,或單擊存儲庫頁面上的“代碼”按鈕並下載zip文件。autogpt --version來驗證安裝是否成功。您應該看到終端中顯示的版本號。您還可以使用虛擬環境包含AutoGPT的安裝,並確保在安裝依賴項或運行AutoGPT時不會遇到任何錯誤。
確保使用自己的配置和自己的API鍵更新'.ENV'。該文件包含您可能需要的所有API。如果您不使用它們,例如用於圖像生成,但是可以確保您有:
OpenAI API鍵- 此鍵必須使用AutoGPT在頂部構建的OpenAI API。您可以通過註冊OpenAI API程序獲得API密鑰:
github_api_key-需要此鍵才能授權您對GitHub API的訪問。您可以按照GitHub網站上的步驟來獲得此鍵。
您的github_username
您可以在此處添加所有API。 “ .env”是自我解釋的,一旦您獲得了這些鍵,就需要在存儲庫中編輯.env文件並添加上面列出的API鍵。
還要注意以下:
您需要在存儲庫中編輯.env文件,並添加上面列出的API鍵。這是因為API密鑰是不應將其硬編碼用於應用程序的敏感信息。
您還需要將自己的提示添加到prompts.json中。存儲庫中提供的提示是一般提示,可能不適用於您的項目。通過添加自己的提示,您可以訓練AutoGPT來生成針對特定用例的響應。
要提示AutoGPT,請在終端中使用命令python -m autogpt 。確保您處於正確的道路上。如果沒有,請通過“ CD路徑”設置
為了獲得最佳效果,請確保您的提示是具體且定義明確的。這將使Autogpt清楚地了解您要實現的目標,並幫助其產生更準確的響應。
在審查模型的響應時,請記住AutoGPT是一種語言模型,而不是人類。因此,它可能會產生不完全準確或適合您用例的響應。由您決定響應並進行任何必要的修改。
使用AutoGPT的另一種方法是將其集成到您的代碼中。您可以查看克隆文件夾中的命令,並使用它來編程生成響應。如果您想在特定上下文中生成響應,這將很有用。
以下是使用AutoGPT進行及時工程的一些最佳方法:
@command Decorator用於定義自定義命令。它需要三個論點:
name :命令的名稱。description :命令的簡要說明。params :代表命令的預期參數的字符串。這是使用@command Decorator定義稱為execute_python_file的自定義命令的示例:
@ command ( "execute_python_file" , "Execute Python File" , '"filename": "<filename>"' )
def execute_python_file ( filename : str ) -> str :
# Implementation of the command要執行Shell命令,您可以使用subprocess模塊。這是運行殼命令的兩種方法:
subprocess.run() :此方法運行命令並等待完成。它返回命令的輸出(stdout和stderr)。
result = subprocess . run ( command_line , capture_output = True , shell = True )
output = f"STDOUT: n { result . stdout } n STDERR: n { result . stderr } " subprocess.Popen() :此方法將命令啟動為單獨的過程,並立即返回。它對於運行非阻滯命令很有用。
process = subprocess . Popen (
command_line , shell = True , stdout = subprocess . DEVNULL , stderr = subprocess . DEVNULL
)
return f"Subprocess started with PID:' { str ( process . pid ) } '"要創建執行Shell命令的自定義命令,您可以將@command Decorator與subprocess方法一起使用:
@ command ( "execute_shell" , "Execute Shell Command" , '"command_line": "<command_line>"' )
def execute_shell ( command_line : str ) -> str :
result = subprocess . run ( command_line , capture_output = True , shell = True )
output = f"STDOUT: n { result . stdout } n STDERR: n { result . stderr } "
return output analyze_code模塊旨在分析給定的代碼段,並提供改進的建議。它使用@command Decorator來定義稱為analyze_code的自定義命令。該功能採用一個包含要分析的代碼的字符串,並返回建議列表。
這是對代碼的簡化解釋:
@command Decorator定義analyze_code函數: @ command ( "analyze_code" , "Analyze Code" , '"code": "<full_code_string>"' )
def analyze_code ( code : str ) -> list [ str ]: function_string = "def analyze_code(code: str) -> list[str]:"
args = [ code ]
description_string = (
"Analyzes the given code and returns a list of suggestions for improvements."
) return call_ai_function ( function_string , args , description_string ) improve_code功能旨在根據提供的建議列表來改進給定的代碼段。它使用@command Decorator來定義一個稱為improve_code的自定義命令。該函數獲取了建議的列表和一個包含要改進的代碼的字符串,並返回一個新的代碼字符串,並應用了建議的改進。
這是對代碼的簡化解釋:
@command Decorator定義improve_code函數: @ command ( "improve_code" , "Get Improved Code" , '"suggestions": "<list_of_suggestions>", "code": "<full_code_string>"' )
def improve_code ( suggestions : list [ str ], code : str ) -> str : function_string = "def generate_improved_code(suggestions: list[str], code: str) -> str:"
args = [ json . dumps ( suggestions ), code ]
description_string = (
"Improves the provided code based on the suggestions"
" provided, making no other changes."
) return call_ai_function ( function_string , args , description_string )要使用improve_code函數,請傳遞建議列表和一個包含您要改進的代碼段的字符串:
suggestions = [
"Replace the print statement with a return statement." ,
"Add type hints to the function."
]
code_to_improve = '''
def some_function():
print("Hello, World!")
'''
improved_code = improve_code ( suggestions , code_to_improve )
print ( improved_code )然後, improve_code函數將調用AI函數,然後使用OpenAI API將建議的改進應用於代碼並生成代碼的新版本。請注意,改進的質量取決於語言模型對建議及其正確應用的能力的理解。
prompt = "Generate documentation for the function 'Function name'"
generator = response = openai.Completion.create(
engine=engine,
prompt=prompt,
max_tokens=150,
n=1,
stop=None,
temperature=1.2,
presence_penalty=0.5,
frequency_penalty=0.5,
)
使用improve_code函數進行調試,您可以按照以下步驟操作:
確定代碼中的問題或錯誤。您可以手動執行此操作,也可以使用Linter,靜態分析儀或調試器之類的工具來幫助查明問題。
根據確定的問題創建建議列表。每個建議都應描述如何解決代碼中的特定問題或錯誤。
調用improve_code函數,將建議列表和代碼段作為參數:
suggestions = [
"Fix the IndexError by using a conditional statement." ,
"Handle the ZeroDivisionError exception."
]
buggy_code = '''
def buggy_function(a, b):
result = a / b
return result
def another_buggy_function(lst):
return lst[0]
'''
improved_code = improve_code ( suggestions , buggy_code )
print ( improved_code )improve_code函數返回的改進代碼。確保AI生成的改進與您的建議保持一致,並正確解決已確定的問題。驗證改進的代碼是否可以按預期工作。 - Choose the search function: `google_search` for DuckDuckGo or `google_official_search` for the official Google API.
- Call the chosen function with your search query:
```python
query = "example search query"
num_results = 5
# Perform the search using DuckDuckGo
search_results = google_search(query, num_results)
print(search_results)
# Or, perform the search using the official Google API
# search_results = google_official_search(query, num_results)
# print(search_results)
該功能返回匹配給定查詢的搜索結果URL列表。確保您將Google API保存為Secrets.json
使用要刮擦網站的URL調用browse_website函數,並與您要尋找的內容有關的問題:
url = "https://example.com"
question = "What is the main purpose of the website?"
answer_and_links = browse_website ( url , question )
print ( answer_and_links ) browse_website函數使用scrape_text_with_selenium來刮擦網站的文本內容和scrape_links_with_selenium來提取鏈接。
list_files函數: directory = "path/to/directory"
all_files = list_files ( directory ) search_criteria = "example"
matching_files = [ file for file in all_files if search_criteria in file ]
print ( matching_files )在此示例中, matching_files將包含指定目錄(及其子目錄)中具有search_criteria字符串的所有文件的列表。根據需要修改過濾條件,以匹配您的特定搜索要求。
AutoGPT具有一個功能clone_repository ,它允許您從給定的URL到計算機上的本地目錄。要提示它進行輸入並使用clone_repository函數克隆GIT存儲庫,請按照以下步驟操作:
repo_url = "repository URL"
clone_path = "the path to clone the repository"clone_repository函數: result = clone_repository ( repo_url , clone_path )此代碼調用clone_repository函數,該函數使用Config對象的提供的GitHub身份驗證憑據來克隆存儲庫。
使用AutoGPT自動為您的代碼生成描述性評論。這可以幫助您的代碼更具可讀性和易於理解,尤其是對於可能不熟悉代碼庫的團隊成員。
使用AutoGPT為您的項目生成樣板代碼。例如,您可以使用AutoGPT生成代碼來設置記錄,創建配置文件或初始化數據庫。
使用AutoGPT生成代碼的測試用例。這可以有助於確保您的代碼按預期工作,並且您對代碼進行的任何更改都不會引入新的錯誤。
使用AutoGPT為您的項目生成文檔。 AutoGPT可以自動生成API文檔,用戶指南和其他類型的文檔,從而節省您的時間和精力。
AutoGPT是一種強大的工具,可以幫助您進行編碼和及時的工程。它可以生成代碼段,檢查代碼語法,改進代碼,生成測試用例,生成文檔並執行Python文件。通過使用AutoGPT,您可以節省時間並提高生產率。