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,您可以节省时间并提高生产率。