L♾️pGPT
模块化 Auto-GPT 框架
L♾️pGPT 是流行的 Auto-GPT 项目的重新实现,作为一个适当的 python 包,编写时考虑了模块化和可扩展性。
?这将安装 L♾️pGPT 的最新稳定版本。建议大多数用户这样做:
pip install loopgpt
以下两种方法安装L♾️pGPT的最新开发版本。请注意,此版本可能不稳定:
pip install git+https://www.github.com/farizrahman4u/loopgpt.git@main
git clone https://www.github.com/farizrahman4u/loopgpt.git
cd loopgpt
pip install -e .
git clone https://www.github.com/farizrahman4u/loopgpt.git
cd loopgpt
docker build -t loopgpt:local-dev .
.env
文件在当前工作目录(无论您要从何处运行 L♾️pGPT)中创建一个.env
文件,并向其中添加以下行:
OPENAI_API_KEY= " <your-openai-api-key> "
?重要的?
Windows 用户,请确保您的文件资源管理器中启用了“显示文件扩展名”。否则,您的文件将被命名为.env.txt
而不是.env
。
将名为OPENAI_API_KEY
环境变量设置为您的 OpenAI API 密钥。
如何设置环境变量:
让我们在新的 Python 脚本中创建一个代理。
from loopgpt . agent import Agent
agent = Agent ()
L♾️pGPT 默认使用gpt-3.5-turbo
,此处显示的所有输出都是使用它进行的。 GPT-4 用户可以设置model="gpt-4"
来代替:
agent = Agent ( model = "gpt-4" )
agent . name = "ResearchGPT"
agent . description = "an AI assistant that researches and finds the best tech products"
agent . goals = [
"Search for the best headphones on Google" ,
"Analyze specs, prices and reviews to find the top 5 best headphones" ,
"Write the list of the top 5 best headphones and their prices to a file" ,
"Summarize the pros and cons of each headphone and write it to a different file called 'summary.txt'" ,
]
我们出发了!让我们运行代理的 CLI:
agent . cli ()
将您的 Python 文件保存为research_gpt.py
并运行它:
python research_gpt.py
您可以通过键入“exit”退出 CLI。
如果continuous
设置为True
,代理将不会请求用户执行命令的权限。它可能会进入无限循环,因此使用它需要您自担风险!
agent . cli ( continuous = True )
您可以直接从命令行运行 L♾️pGPT,而无需编写任何 python 代码:
loopgpt run
运行loopgpt --help
以查看所有可用选项。
您可以使用 Docker 以前面提到的模式运行 L♾️pGPT:
# CLI mode
docker run -i --rm loopgpt:local-dev loopgpt run
# Script mode example
docker run -i --rm -v " $( pwd ) /scripts:/scripts " loopgpt:local-dev python /scripts/myscript.py
L♾️pGPT 代理附带了一组内置工具,允许它们执行各种基本任务,例如搜索网络、文件系统操作等。您可以使用print(agent.tools)
查看这些工具。
除了这些内置工具之外,您还可以将自己的工具添加到代理的工具箱中。
让我们创建 WeatherGPT,一个适用于所有天气的人工智能助手。
工具继承自BaseTool
,您只需编写文档字符串即可启动并运行您的工具!
from loopgpt . tools import BaseTool
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
def run ( self , city ):
...
L♾️pGPT 为您的工具提供默认 ID,但如果您愿意,您可以覆盖它们:
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
@ property
def id ( self ):
return "get_weather_command"
现在让我们定义我们的工具将在其run
方法中执行的操作:
import requests
# Define your custom tool
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
def run ( self , city ):
try :
url = "https://wttr.in/{}?format=%l+%C+%h+%t+%w+%p+%P" . format ( city )
data = requests . get ( url ). text . split ( " " )
keys = ( "location" , "condition" , "humidity" , "temperature" , "wind" , "precipitation" , "pressure" )
data = dict ( zip ( keys , data ))
return data
except Exception as e :
return f"An error occurred while getting the weather: { e } ."
就是这样!您已经构建了第一个自定义工具。让我们用新代理注册它并运行它:
from loopgpt . tools import WriteToFile
import loopgpt
# Register custom tool type
# This is actually not required here, but is required when you load a saved agent with custom tools.
loopgpt . tools . register_tool_type ( GetWeather )
# Create Agent
agent = loopgpt . Agent ( tools = [ GetWeather , WriteToFile ])
agent . name = "WeatherGPT"
agent . description = "an AI assistant that tells you the weather"
agent . goals = [
"Get the weather for NewYork and Beijing" ,
"Give the user tips on how to dress for the weather in NewYork and Beijing" ,
"Write the tips to a file called 'dressing_tips.txt'"
]
# Run the agent's CLI
agent . cli ()
我们来看看WeatherGPT为我们编写的dressing_tips.txt
文件:
敷料提示.txt
- It's Clear outside with a temperature of +10°C in Beijing. Wearing a light jacket and pants is recommended.
- It's Overcast outside with a temperature of +11°C in New York. Wearing a light jacket, pants, and an umbrella is recommended.
与 Auto-GPT 不同,当用户拒绝执行命令时,代理不会终止。相反,它会要求用户提供反馈来纠正其路线。
要纠正代理的行为,只需拒绝执行并提供反馈即可:
该代理已更新其行动方针:
您可以使用以下命令将代理的状态保存到 json 文件中:
agent . save ( "ResearchGPT.json" )
这保存了代理的配置(模型、名称、描述等)及其内部状态(对话状态、内存、工具状态等)。您还可以通过将include_state=False
传递给agent.save()
来仅保存配置:
agent . save ( "ResearchGPT.json" , include_state = False )
然后从上次停下的地方继续:
import loopgpt
agent = loopgpt . Agent . load ( "ResearchGPT.json" )
agent . cli ()
或者从命令行运行保存的代理:
loopgpt run ResearchGPT.json
您可以将代理状态转换为 json 兼容的 python 字典,而不是写入文件:
agent_config = agent . config ()
要仅获取配置而不获取内部状态:
agent_config = agent . config ( include_state = False )
要从配置重新加载代理,请使用:
import loopgpt
agent = loopgpt . Agent . from_config ( agent_config )
对于官方谷歌搜索支持,您需要设置两个环境变量键GOOGLE_API_KEY
和CUSTOM_SEARCH_ENGINE_ID
,以下是获取它们的方法:
CUSTOM_SEARCH_ENGINE_ID
环境变量的值。GOOGLE_API_KEY
环境变量。如果缺少这些,L♾️pGPT 将转而使用 DuckDuckGo 搜索。
我们需要很多帮助!如果您想做出贡献,请提出问题或 PR。
需要帮助吗?加入我们的不和谐。