load_dotenv()
google_api_key=os.getenv("GOOGLE_API_KEY")
open_api_key=os.getenv("OPENAI_API_KEY")
os.environ["GOOGLE_API_KEY"]=google_api_key.strip('"')
os.environ["OPENAI_API_KEY"]=open_api_key.strip('"')
seed=42对于GeminiAI和OpenAI,我们可以根据实际情况进行配置。例如,如果使用GeminiAI免费版本代码,可以将geminisafety设置为NONE:
safety_settings=[
{"category":"HARM_CATEGORY_HARASENT","threshold":"LOCK_NONE"},
{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"LOCK_NONE"},
{"category":"HARM_CATEGORY_UALLY_EXPLICIT","threshold":"LOCK_NONE"},
{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"LOCK_NONE"},
](三)配置LLM
根据使用的模型不同,需要进行相应的配置。例如,将LLM配置为Gemini-1.5-flash:
llm_config={
"config_list":[
"model":"gemini-1.5-flash",
"api_key":os.environ["GOOGLE_API_KEY"],
"api_type":"google",
"safety_settings":safety_settings,
]
}或者配置为OpenAI:
llm_config={
"config_list":[{"model":"gpt-4","api_key":os.getenv("OPENAI_API_KEY")}]
}(四)定义编码任务
coding_task=[
"""downloaddatafroms://raw.githuusercontent/vega/vega-datasets/main/data/penguins.json""",
"""finddesccriptivestatistiofthedataset,plotachartoftheirrelationetweenspsandeaklengthandsetheplottoeak_length_depth.png""",
"""Developeashortreportusingthedatafromthedataset,seittoafilenamedpenguin_report.md."""
](五)设计AssistantAgent
在这个项目中,我们将使用四个助手,分别是UserProxy、Coder、Writer和Critic。
<>UserProxyAgent>:它是AutoGenUserAgent,是ConversaleAgent的子类。其human_input_mode原本是ALWAYS,意味着它会作为人工Agent工作,但在这里我们将其设置为NEVER,使其能够自主工作。
user_proxy=autogen.UserProxyAgent(
name="User_proxy",
_message="Ahuman.",
code_execution_config={
"last_n_messages":3,
"work_dir":"groupchat",
"use_docker":False
},
human_input_mode="NEVER"
)<>Code和WriterAgent>:这两个Agent利用AutoGenAssistantAgent构建,它是ConversaleAgent的子类,旨在解决LLM的任务,human_input_mode为NEVER。我们可以为Agent设置消息提示。
coder=autogen.AssistantAgent(
name="Coder",
llm_config=llm_config
)
writer=autogen.AssistantAgent(
name="writer",
llm_config=llm_config,
_message="""
Youareaprofessionalreportwriter,knownfor
yoinsightfulandengagingreportforclients.
Youtranormcomplexconceptsintocompellingnarratives.
Reply"TERMINATE"intheendwheneverythingisdone.
"""
)<>CriticAgent>:它是一个Agent,主要负责评估编码Agent创建的代码质量,并提出改进建议。
_message="""Critic.Youareahelpfulassistanthighlyskilledin
evaluatingthequalityofagivenvisualizationcodeyprovidingascore
from1(ad)-10(good)whileprovidingclearrationale.YOUMUSTCONSIDER
VUALIZATIONESTPRACTSforeachevaluation.Specifically,youcan
carefullyevaluatethecodeacrossthefollowingdimensions
-ugs(ugs):arethereugs,logicerrors,syntaxerrorortypos?Are
thereanyreasonswhythecodemayfailtocompile?Howshoulditefixed?
IfANYugexists,theugscoreMUSTelessthan5.
-Datatranormation(tranormation):Isthedatatranormed
appropriayforthevisualizationtype?E.g.,isthedatasetappropriated
filtered,aggregated,orgroupedifneeded?Ifadatefieldisused,isthe
datefieldfirstconvertedtoadateojectetc?
-Goalcompliance(compliance):howwellthecodemeetsthespecified
visualizationgoals?
-Visualizationtype(type):CONSIDERINGESTPRACTS,isthe
visualizationtypeappropriateforthedataandintent?Istherea
visualizationtypethatwouldemoreeffectiveinconveyinginsights?
Ifadifferentvisualizationtypeismoreappropriate,thescoreMUST
ELESSTHAN5.
-Dataencoding(encoding):Isthedataencodedappropriayforthe
visualizationtype?
-aestheti(aestheti):Aretheaesthetiofthevisualization
appropriateforthevisualizationtypeandthedata?
YOUMUSTPROVIDEASCOREforeachoftheaovedimensions.
{ugs:0,tranormation:0,compliance:0,type:0,encoding:0,
aestheti:0}
Donotsuggestcode.
Finally,asedonthecritiqueaove,suggestaconcretelistofactions
thatthecodershouldtaketoimprovethecode.
"""
critic=autogen.AssistantAgent(
name="Critic",
_message=_message,
llm_config=llm_config
)(六)群聊和器创建
在AutoGen中,我们使用GroupChat功能将多个Agent组合在一起,以执行特定任务,并使用GroupChatManager控制GroupChat行为。
groupchat_coder=autogen.GroupChat(
agents=[user_proxy,coder,critic],messages=[],max_round=10
)
groupchat_writer=autogen.GroupChat(
agents=[user_proxy,writer,critic],messages=[],max_round=10
)
manager_1=autogen.GroupChatManager(
groupchat=groupchat_coder,
llm_config=llm_config,
is_termination_msg=lamdax:x.get("content","").find("TERMINATE")>=0,
code_execution_config={
"last_n_messages":1,
"work_dir":"groupchat",
"use_docker":False
}
)
manager_2=autogen.GroupChatManager(
groupchat=groupchat_writer,
name="Writing_manager",
llm_config=llm_config,
is_termination_msg=lamdax:x.get("content","").find("TERMINATE")>=0,
code_execution_config={
"last_n_messages":1,
"work_dir":"groupchat",
"use_docker":False
}
)最后,创建一个用户Agent来启动聊天过程并检测终止命令:
user=autogen.UserProxyAgent(
name="User",
human_input_mode="NEVER",
is_termination_msg=lamdax:x.get("content","").find("TERMINATE")>=0,
code_execution_config={
"last_n_messages":1,
"work_dir":"tasks",
"use_docker":False
}
)
user.initiate_chats(
{"recipient":coder,"message":coding_task[0],"summary_method":"last_msg"},
{"recipient":manager_1,"message":coding_task[1],"summary_method":"last_msg"},
{"recipient":manager_2,"message":coding_task[2]}
)在这个项目中,Agent会按照以下步骤工作:首先下载企鹅数据集,然后由coderAgent创建代码,criticAgent对代码进行评估和建议改进,接着coderAgent根据建议重新运行代码进行优化,之后writerAgent利用处理后的数据编写报告,criticAgent同样会对报告内容进行评估和指导,最终完成整个任务流程。
s://zhuanlan.zhihu/p/17121470813
s://zhuanlan.zhihu/p/16403050798
同类推荐