ChatGPT|Auto-GPT实现解析

ChatGPT|Auto-GPT实现解析

什么是Auto-GPT

地址:github.com/Torantulino…
这个程序由GPT-4驱动,将LLM”思想”链接在一起,以自主完成您设定的任何方针。
Auto-GPT是将OpenAI的GPT模型的多个实例链接在一起,使其能够在没有协助的情况下完成任务、编写和调试代码以及纠正自己的编写过错等工作。
Auto-GPT不是简单地要求ChatGPT创建代码,而是让多个AI署理协同工作,因为它将语言模型变成了一个更有能力的署理,能够采取举动并从过错中学习。

装置步骤

gitclonehttps://github.com/Torantulino/Auto-GPT.git
cd'Auto-GPT'
pipinstall-rrequirements.txt
pythonscripts/main.py
//假如需求debug
pythonscripts/main.py--debug

1、必要的装备:
(1)先将.env.template复制.env,必须装备:OPENAI_API_KEY(在openai平台请求platform.openai.com/account/api…
(2)假如您运用的是Azure实例,运用USE_AZURE=True,装备OPENAI_AZURE_API_BASE,OPENAI_AZURE_API_VERSION,OPENAI_AZURE_DEPLOYMENT_ID;
2、非必要的装备:
(1)语音形式需求去elevenlabs.io请求ElevenLabs API密钥,装备:ELEVEN_LABS_API_KEY;
(2)谷歌API密钥装备,主要是假如发问的过程中需求查找,就需求装备;
(3)Redis或者Pinecone装备,主要是存储一些前史问题和回答的向量缓存;
(4)生成图片,默认情况下,Auto-GPT 运用 DALL-e 进行图画生成,也能够装备HUGGINGFACE_API_TOKEN运用Stable Diffusion;
(5)因为国内用户没有GPT4权限,能够走gpt3.5形式:

pythonscripts/main.py--gpt3only

完成原理

AutoGPT完成:举动 -> 观察举动成果 -> 考虑 -> 决议下一步举动的自我循环,怎么完成的呢?

我的装备文件(ai_settings.yaml,运用–gpt3only) :

ai_goals:
-告诉我LangChain怎么运用.
-帮我装置LangChain.
-告诉我一些能够直接在我电脑上运行LangChain的比如.
ai_name:我是GPT
ai_role:LangChain怎么运用

1、Prompt模板

首要AutoGPT会将问题转换为Prompt模板,拼接{ai_name}和{ai_role},填入GOALS:{ai_goals};
然后加上一些约束条件CONSTRAINTS,COMMANDS,RESOURCES和PERFORMANCE EVALUATION模板;
最后供给一个JSON的回来格式,假如ChatGPT回来回来,是一段JSON,而且补充需求用python json.loads能够直接履行的;
详细如下:

Youare我是GPT,LangChain怎么运用
Yourdecisionsmustalwaysbemadeindependentlywithoutseekinguserassistance.PlaytoyourstrengthsasanLLMandpursuesimplestrategieswithnolegalcomplications.
GOALS:
1.告诉我LangChain怎么运用.
2.帮我装置LangChain.
3.告诉我一些能够直接在我电脑上运行LangChain的比如.
4.一切的问题都用中文回答.
CONSTRAINTS:
1.~4000wordlimitforshorttermmemory.Yourshorttermmemoryisshort,soimmediatelysaveimportantinformationtofiles.
2.Ifyouareunsurehowyoupreviouslydidsomethingorwanttorecallpastevents,thinkingaboutsimilareventswillhelpyouremember.
3.Nouserassistance
4.Exclusivelyusethecommandslistedindoublequotese.g."commandname"
COMMANDS:
1.GoogleSearch:"google",args:"input":"<search>"
5.BrowseWebsite:"browse_website",args:"url":"<url>","question":"<what_you_want_to_find_on_website>"
6.StartGPTAgent:"start_agent",args:"name":"<name>","task":"<short_task_desc>","prompt":"<prompt>"
7.MessageGPTAgent:"message_agent",args:"key":"<key>","message":"<message>"
8.ListGPTAgents:"list_agents",args:""
9.DeleteGPTAgent:"delete_agent",args:"key":"<key>"
10.Writetofile:"write_to_file",args:"file":"<file>","text":"<text>"
11.Readfile:"read_file",args:"file":"<file>"
12.Appendtofile:"append_to_file",args:"file":"<file>","text":"<text>"
13.Deletefile:"delete_file",args:"file":"<file>"
14.SearchFiles:"search_files",args:"directory":"<directory>"
15.EvaluateCode:"evaluate_code",args:"code":"<full_code_string>"
16.GetImprovedCode:"improve_code",args:"suggestions":"<list_of_suggestions>","code":"<full_code_string>"
17.WriteTests:"write_tests",args:"code":"<full_code_string>","focus":"<list_of_focus_areas>"
18.ExecutePythonFile:"execute_python_file",args:"file":"<file>"
19.TaskComplete(Shutdown):"task_complete",args:"reason":"<reason>"
20.GenerateImage:"generate_image",args:"prompt":"<prompt>"
21.DoNothing:"do_nothing",args:""
RESOURCES:
1.Internetaccessforsearchesandinformationgathering.
2.LongTermmemorymanagement.
3.GPT-3.5poweredAgentsfordelegationofsimpletasks.
4.Fileoutput.
PERFORMANCEEVALUATION:
1.Continuouslyreviewandanalyzeyouractionstoensureyouareperformingtothebestofyourabilities.
2.Constructivelyself-criticizeyourbig-picturebehaviorconstantly.
3.Reflectonpastdecisionsandstrategiestorefineyourapproach.
4.Everycommandhasacost,sobesmartandefficient.Aimtocompletetasksintheleastnumberofsteps.
YoushouldonlyrespondinJSONformatasdescribedbelow
RESPONSEFORMAT:
{
"thoughts":
{
"text":"thought",
"reasoning":"reasoning",
"plan":"-shortbulleted\
-listthatconveys\
-long-termplan",
"criticism":"constructiveself-criticism",
"speak":"thoughtssummarytosaytouser"
},
"command":{
"name":"commandname",
"args":{
"argname":"value"
}
}
}
EnsuretheresponsecanbeparsedbyPythonjson.loads

源码会调用到:

#这里实际便是将以上组装的数据发送给openai的接口
assistant_reply=create_chat_completion(
model=model,
messages=current_context,
max_tokens=tokens_remaining,
)
#Updatefullmessagehistory
full_message_history.append(
create_chat_message(
"user",user_input))
full_message_history.append(
create_chat_message(
"assistant",assistant_reply))

2、处理获取成果

履行第一步以后就能拿到成果,如下:

{
"thoughts":{
"text":"LangChainisaprogramminglanguagethatcanbeusedforblockchaindevelopment.Touseit,weneedtofirstdownloadandinstallitonourcomputer.Afterinstallation,wecanrunsomecodeexamplestotestitout.",
"reasoning":"IreviewedthepurposeandusageofLangChainandsuggestedaplanforinstallationandtesting",
"plan":"-DownloadandinstallLangChain.\n-Runcodeexamplestotestitout.",
"criticism":"IshouldhaveprovidedmorespecificdetailedstepsonhowtodownloadandinstallLangChain",
"speak":"WeneedtodownloadandinstallLangChainbeforewecanuseit.Doyouhaveanyexperiencewiththis?"
},
"command":{
"name":"browse_website",
"args":{
"url":"https://langchain.org/docs/getting-started/",
"question":"HowtodownloadandinstallLangChain?"
}
}
}

能够看到回来的plan拆分为两个步骤:

-DownloadandinstallLangChain.
-Runcodeexamplestotestitout.

然后也给出了指令:browse_website和参数;

3、履行任务

能够从AutoGPT的源码中看到,供给了许多履行脚本的方法,比如google查找,履行execute_shell脚本等。

try:
ifcommand_name=="google":
#CheckiftheGoogleAPIkeyissetandusetheofficialsearchmethod
#IftheAPIkeyisnotsetorhasonlywhitespaces,usetheunofficialsearchmethod
ifcfg.google_api_keyand(cfg.google_api_key.strip()ifcfg.google_api_keyelseNone):
returngoogle_official_search(arguments["input"])
else:
returngoogle_search(arguments["input"])
elifcommand_name=="memory_add":
returnmemory.add(arguments["string"])
elifcommand_name=="start_agent":
returnstart_agent(
arguments["name"],
arguments["task"],
arguments["prompt"])
elifcommand_name=="message_agent":
returnmessage_agent(arguments["key"],arguments["message"])
elifcommand_name=="list_agents":
returnlist_agents()
elifcommand_name=="delete_agent":
returndelete_agent(arguments["key"])
elifcommand_name=="get_text_summary":
returnget_text_summary(arguments["url"],arguments["question"])
elifcommand_name=="get_hyperlinks":
returnget_hyperlinks(arguments["url"])
elifcommand_name=="read_file":
returnread_file(arguments["file"])
elifcommand_name=="write_to_file":
returnwrite_to_file(arguments["file"],arguments["text"])
elifcommand_name=="append_to_file":
returnappend_to_file(arguments["file"],arguments["text"])
elifcommand_name=="delete_file":
returndelete_file(arguments["file"])
elifcommand_name=="search_files":
returnsearch_files(arguments["directory"])
elifcommand_name=="browse_website":
returnbrowse_website(arguments["url"],arguments["question"])
#TODO:Changethesetotakeinafileratherthanpastedcode,if
#non-fileisgiven,returninstructions"Inputshouldbeapython
#filepath,writeyourcodetofileandtryagain"
elifcommand_name=="evaluate_code":
returnai.evaluate_code(arguments["code"])
elifcommand_name=="improve_code":
returnai.improve_code(arguments["suggestions"],arguments["code"])
elifcommand_name=="write_tests":
returnai.write_tests(arguments["code"],arguments.get("focus"))
elifcommand_name=="execute_python_file":#Addthiscommand
returnexecute_python_file(arguments["file"])
elifcommand_name=="execute_shell":
ifcfg.execute_local_commands:
returnexecute_shell(arguments["command_line"])
else:
return"Youarenotallowedtorunlocalshellcommands.Toexecuteshellcommands,EXECUTE_LOCAL_COMMANDSmustbesetto'True'inyourconfig.Donotattempttobypasstherestriction."
elifcommand_name=="generate_image":
returngenerate_image(arguments["prompt"])
elifcommand_name=="do_nothing":
return"Noactionperformed."
elifcommand_name=="task_complete":
shutdown()
else:
returnf"Unknowncommand'{command_name}'.Pleaserefertothe'COMMANDS'listforavailablecommandsandonlyrespondinthespecifiedJSONformat."
#Allerrors,return"Error:+errormessage"
exceptExceptionase:
return"Error:"+str(e)

依据以上给出的browse_website,参数是https://langchain.org/docs/getting-started/How to download and install LangChain?
依据以上的代码就会履行:

browse_website(arguments["url"],arguments["question"])

因为https://langchain.org/docs/getting-started/这个地址便是过错的,所以履行报错:

图片

这样就结束了么?AutoGPT会将报错的信息catch:

#Allerrors,return"Error:+errormessage"
exceptExceptionase:
return"Error:"+str(e)

图片

将过错组装一下,继续发给ChatGPT,然后得到的回复是- Conduct a Google search to find out how to download and install LangChain.,意思是需求运用Google查找,其间回来需求履行的指令:

"command":{
"name":"google",
"args":{
"input":"howtodownloadandinstallLangChain"
}
}

因而下一步就走到google_search的函数…,因为篇幅原因,详细后续的履行就省掉。

4、循环履行任务

在AutoGPT是一步一步承认的,整个逻辑是在一个大循环中:

#InteractionLoop
whileTrue:
#发送数据给AI,然后取得回应
assistant_reply=chat.chat_with_ai(
prompt,
user_input,
full_message_history,
memory,
cfg.fast_token_limit)
#解析以取得需求履行的指令和参数(因为回来的JSON需求校准,所以这里会有比较多的处理)
#履行指令
#将每次的问题和回复记载memory中
#...

其间chat_with_ai的参数:
(1)prompt是解释规矩给AI的提示,主要有几个信息:
a. AI的名字,每次对话自己设置的
b. 描绘AI需求帮你完成的工作
c. 输入几个需求完成的方针(一般设置5个)
(2)user_input是用户输入
(3)full_message_history是用户和AI之间发送的一切消息的列表
(4)memory是包括永久回忆的内存对象
(5)fast_token_limit是API调用中允许的最大令牌数

5、memory的作用

AutoGPT支持三种向量存储:pinecone,redis,local,默认运用LocalCache。如下:

ifcfg.memory_backend=="pinecone":
ifnotPineconeMemory:
print("Error:Pineconeisnotinstalled.Pleaseinstallpinecone"
"tousePineconeasamemorybackend.")
else:
memory=PineconeMemory(cfg)
ifinit:
memory.clear()
elifcfg.memory_backend=="redis":
ifnotRedisMemory:
print("Error:Redisisnotinstalled.Pleaseinstallredis-pyto"
"useRedisasamemorybackend.")
else:
memory=RedisMemory(cfg)
ifmemoryisNone:
memory=LocalCache(cfg)
ifinit:
memory.clear()

主要是操控token的数量,经过存储对话的上下文,找到全部前史信息中的最近10个关键上下文继续组成下一次上下文,比如LocalCache便是经过ada算法完成topk查找:

defget_relevant(self,text:str,k:int)->List[Any]:
embedding=get_ada_embedding(text)
scores=np.dot(self.data.embeddings,embedding)
top_k_indices=np.argsort(scores)[-k:][::-1]
return[self.data.texts[i]foriintop_k_indices]

所以AutoGPT有点像「套娃」,经过你输入的问题,不断去ChatGPT上查找解决方案,然后履行各个指令,假如指令回来不对,继续指定下一步方案,直到你得到成果为止。
专业一点的话来说,这叫模型堆叠,即模型「一路向下」,去套用其他模型来拆解并解决任务。
除了AutoGPT、BabyAGI这些,还有ViperGPT、SayCan和ToolKit等最新工具,以及前面微软发布的VisualGPT和HugginGPT,都差不多是这个思想。
留意:这里假如问题复杂,消耗openai的token有点多。

ChatGPT|Auto-GPT实现解析