从LangChain使用Agent的懵懵懂懂,到AutoGen的”放肆”Agents,这周的学习和写作实在太幸福了。假如您是第一次接触AugoGen,请先要查看AutoGen官方文档,或翻阅AutoGen系列的前几篇。
前言
今天带来的仍然是AutoGen基于AssistantAgent和UserProxyAgent的例子,以帮助大家一起消化目前最前卫的AI应用框架。这是一个AIGC最擅长,因为生成新闻稿嘛,同时又需要利用Agent的一个常规Demo。了解LangChain的同学,会通过对比发现,AutoGen做法的高级,并解锁新的AutoGen高级技能,Agent函数调用。
话说笔者当年读大学的时候,突然蹦出了一个要跨考新闻学的大胆想法。随即立马冲向书店,买来全套人大新闻学考研书籍,报了各种考研辅导班…
还记得,当年政治辅导班的老教授,听说我一个双非的筒子想跨考人大的新闻学的时候,一脸的尴尬… 今天,就让我们基于AutoGen,来一起写(生成)一篇新闻稿,弥补当年的遗憾….
主题
谁是最勇敢的人?当然是最可爱的人身边的战地记者,当年我突发奇想,想跨考新闻学也是因为这个。我们今天要写的新闻主题是关于最近最揪心的中东问题,写一篇新闻搞报道哈马斯。
开发思路
-
AIGC
有了大模型,写新闻这种事交给它就可以了,这是AIGC类大模型最擅长的。
-
Agent
大模型不擅长的是新闻时事,比如chatgpt 3.5训练的数据截止两年前。怎么办呢,我们可以使用一个网络请求Agent,先去访问Google,得到“哈马斯”相关的新闻,再交给LLm来生成新闻。
-
使用serpapi来获取Google接口数据
上图,我们使用了serpapi的playgound, 搜索框输入了“哈马斯”,下半部分,左边是google页面显示截图,右边是serpapi返回的接口数据。
notebook代码
我这边AutoGen是跑在Colab上的,一个Google的线上NLP开放平台,巴适的很,一起薅羊毛啊!!!大家也可以点开autogen_news.ipynb – Colaboratory (google.com),边看文章,边点击运行代码。
- 安装依赖
%pip install pyautogen~=0.1.0 google-search-results -q -U
pyautogen即AutoGen,目前还在早期版本0.1.0, 有些同学觉得API换的好勤,这很正常,非常早期。 google-search-results 即serpapi 提供的google api sdk。
- 定义接口函数
AutoGen中UserProxyAgent,具有强大功能,除了可以做我们的代理外,还可以执行我们定义好的函数。在这里我们定义好google search 的函数, 到时由UserProxyAgent 自动调用。赞啊,今天的能力增长点Get!!!
from serpapi import GoogleSearch # 引入 GoogleSearch模块
def search_google_news(keyword): # 定义搜索函数,交给proxy agent调用
print(f"Keyword:{keyword}")
search = GoogleSearch({ #向GoogleSearch传递配置参数
"q":keyword, # 关键字
"tbm": "nws", # 表示搜索的是新闻
"api_key":"" # https://serpapi.com/manage-api-key 处获得
})
result = search.get_dict()
print(result)
return [item['link'] for item in result['news_results']] #只需要返回的每条记录中的link超链接
上图是api_key
{'position': 4, 'link': '<https://news.online.sh.cn/news/gb/content/2023-11/14/content_10141630.htm>', 'title': '内塔尼亚胡说“可能”与哈马斯达成放人协议', 'source': '上海热线', 'date': '16 hours ago', 'snippet': '新华社北京11月13日电以色列总理本雅明·内塔尼亚胡12日接受美国媒体采访时说,“可能”与巴勒斯坦伊斯兰抵抗运动(哈马斯)就释放遭扣押人员达成协议,...', 'thumbnail': '<https://serpapi.com/searches/655439e095bf92860deeae63/images/706261574d4453726e8c40185f13181cccd5b3544a7ebe85168ea4acb6679a89.jpeg>'}
上面是接口返回的一条数据的格式,在search_google_news函数中, 最后我们通过**[item[‘link’] for item in result[‘news_results’]]** 代码只返回链接部分
函数返回的结果是
['https://www.voachinese.com/a/us-britain-impose-sanctions-on-hamas-20231114/7354871.html',
'https://www.rfi.fr/cn/%E5%9B%BD%E9%99%85/20231114-%E8%A7%86%E9%A2%91-%E5%93%88%E9%A9%AC%E6%96%AF%E5%8A%A0%E6%B2%99%E5%A4%9A%E6%9C%BA%E6%9E%84%E5%A4%B1%E5%AE%88-%E4%BB%8D%E7%BB%AD%E5%8F%91%E7%81%AB%E7%AE%AD%E5%BC%B9%E7%82%B8%E4%BB%A5%E8%89%B2%E5%88%97',
'https://chinese.aljazeera.net/palestine-israel-conflict/liveblog/2023/11/15/%E4%BB%A5%E8%89%B2%E5%88%97%E5%AF%B9%E5%8A%A0%E6%B2%99%E6%88%98%E4%BA%89%E7%9A%84%E4%BB%8A%E6%97%A5%E5%8F%91%E5%B1%95%E4%BB%A5%E8%89%B2%E5%88%97%E8%A2%AD%E5%87%BB%E5%B8%8C%E6%B3%95%E5%8C%BB%E9%99%A2'....]
- 接下来引入AutoGen,完成配置,和前几篇文章一样,不懂的看前面文章的介绍
import autogen
config_list = [
{
'model': 'gpt-3.5-turbo',
'api_key': ''
}
]
llm_config={
"timeout": 600,
"seed": 42,
"config_list": config_list,
"temperature": 0,
"functions": [
{
"name": "search_google_news",
"description": "Search google news by keyword",
"parameters": {
"type": "object",
"properties": {
"keyword": {
"type": "string",
"description": "The keyword that's used to search google news",
}
},
"required": ["keyword"],
},
}
],
}
上面的llm_config 配置代码中,比上篇文章多了 functions 部分,即大模型可以使用的函数,name为search_google_news, agent就可以调用上面定义的函数了。并使用description 声明了agent调用哪个函数的自然语义,LLM可以根据用户的输入来对description做embedding的。required 是必须传的参数。
- 创建Agent
assistant = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config )
#create a proxyAgent named user_proxy
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER", #用户输入模式是从命令行
max_consecutive_auto_reply=10, # 代理Agent会代替用户做执行,这里配置最大的连续自动proxy次数是 10 # `lambda`关键字在Python中用于创建匿名函数,也就是没有名字的函数。这个函数接受一个参数`x`,然后返回一个布尔值。检查`x`的"content"字段的值是否以"TERMINATE"结束
code_execution_config={"work_dir":"."}, # 这个有点意思,感觉是我们在向代理agent授权。
system_message="When a link is provided, you should ask the assistant for fetching the content.Reply TERMINATE if the task has been solved at full satisfaction.Otherwise, reply CONTINUE, or the reason why the task is not solved yet." )
function_map={"search_google_news":search_google_news}
- 开始agents对话
user_proxy.initiate_chat(
assistant,
message="""
Search Google news in topic of "哈马斯",and write a news base on them.
You should be avoid bullet points.
Instead, use more expressive words to form a news that's like one written by a well recognized journalist.
Start the keywords now
"""
)
- 研究输出,了解 agent chat 工作流。
1. user_proxy接收到我们的指令,跟assistant聊天,下达任务。
user_proxy (to assistant): Search Google news in topic of "哈马斯",and write a news base on them. You should be avoid bullet points. Instead, use more expressive words to form a news that's like one written by a well recognized journalist. Start the keywords now
借助之前对Prompt 的理解,assistant应用借助了大模型,从user_proxy的命令语义中提取出了任务和关键字哈马斯。
assistant (to user_proxy):
***** Suggested function Call: search_google_news *****
Arguments: {
"keyword": "哈马斯"
}
*******************************************************
2. assistant 结合llm_config配置的functions,chat 告诉user_proxy 执行search_google_news函数,并提供了相应的参数keyword。
实在太nb了。我个人理解AutoGen提供的这种Agent with funciton calls 能力,必然成为超越LangChain等框架的优势,有点像agent的agent….
3. 执行函数,拿到google API 返回,并获取link 列表
4.assistant 在拿到这些response后, 向user_proxy汇报,将去分析这些链接里的文章,assistant做为agent, 有远程请求网页,并parse的能力,这是我们之前在langChai中的agent能力。
5.assistant基于拿到的内容,开始aigc, 按之前用户的需求,写一篇新闻稿(written by a well recognized journalist,这里是prompt的情景设定)。
In the midst of escalating tensions in the Gaza Strip, the militant group 哈马斯 is facing significant challenges as it continues to engage in conflict with Israel. Recent developments have highlighted the complex dynamics at play in the region, with both military and political implications. According to reports, 哈马斯 has lost control over several key institutions in Gaza, raising concerns about its ability to maintain its stronghold in the area. The Israeli military has claimed that 哈马斯' grip on power has weakened, citing the loss of control over strategic locations. This has led to speculation about the group's future and its ability to effectively govern in the face of mounting pressure. In response to the situation, 哈马斯 has continued to launch rocket attacks against Israel, further fueling the conflict. Despite the setbacks, the group remains defiant, vowing to retaliate against what it perceives as Israeli aggression. The ongoing violence has resulted in civilian casualties and widespread destruction, exacerbating an already dire humanitarian situation in the region. The international community has also weighed in on the situation, with the United States and Britain imposing sanctions on 哈马斯. This move is seen as a significant step in isolating the group and holding it accountable for its actions. However, opinions on the effectiveness of such measures remain divided, with some arguing that they may further escalate tensions and hinder efforts towards a peaceful resolution. Meanwhile, the controversy surrounding 哈马斯 has extended beyond the conflict zone. In France, the use of the term "terrorist" to describe the group has sparked a heated debate. French news agency Agence France-Presse (AFP) has decided to refrain from using the term, citing concerns about its potential impact on public perception and the need for impartial reporting. This decision has drawn both support and criticism, highlighting the complexities of addressing the issue from a journalistic standpoint. As the situation in Gaza continues to evolve, the future of 哈马斯 remains uncertain. The group's ability to navigate the challenges it faces, both internally and externally, will undoubtedly shape the course of the conflict and its impact on the region. The international community, meanwhile, faces the daunting task of finding a sustainable and peaceful resolution to the long-standing Israeli-Palestinian conflict. Note: This news article is a fictional representation based on the provided keywords and search results. It does not reflect real events or news reporting.
好的任务完成,太酷了。
总结
本篇主要介绍了AutoGen的agent with function calls的Demo。作者有以下几个感受:
- LLM带来的Prompt Enginner, 让更多非软件专业的同学,能够加入到LLM的“掘金”中来。笔者正在力求与其它专业的同学沟通,AutoGen这种自动聊天的运行方式,让Prompt Enginner 更自然,更接地气。
- agent with function calls 强大了agent 的工程化能力,agent照章办事,又为prompt 提供了程序能力。
参考资料
- AutoGen | AutoGen (microsoft.github.io)
- 五里墩茶社 关于AutoGen的视频,
最后,欢迎和我一样在学习LangChain和AutoGen。走向AI应用开发!!!!