AutoGen框架革新:解锁新闻稿写作的新境界

前言

今天带来的仍然是AutoGen基于AssistantAgent和UserProxyAgent的例子,以帮助大家一起消化目前最前卫的AI应用框架。这是一个AIGC最擅长,因为生成新闻稿嘛,同时又需要利用Agent的一个常规Demo。了解LangChain的同学,会通过对比发现,AutoGen做法的高级,并解锁新的AutoGen高级技能,Agent函数调用。

话说笔者当年读大学的时候,突然蹦出了一个要跨考新闻学的大胆想法。随即立马冲向书店,买来全套人大新闻学考研书籍,报了各种考研辅导班…

还记得,当年政治辅导班的老教授,听说我一个双非的筒子想跨考人大的新闻学的时候,一脸的尴尬… 今天,就让我们基于AutoGen,来一起写(生成)一篇新闻稿,弥补当年的遗憾…

主题

谁是最勇敢的人?当然是最可爱的人身边的战地记者,当年我突发奇想,想跨考新闻学也是因为这个。我们今天要写的新闻主题是关于最近最揪心的中东问题,写一篇新闻搞报道哈马斯

开发思路

  • AIGC

    有了大模型,写新闻这种事交给它就可以了,这是AIGC类大模型最擅长的。

  • Agent

    大模型不擅长的是新闻时事,比如chatgpt 3.5训练的数据截止两年前。怎么办呢,我们可以使用一个网络请求Agent,先去访问Google,得到“哈马斯”相关的新闻,再交给LLm来生成新闻。

  • 使用serpapi来获取Google接口数据

QQ图片20231115105159.jpg

上图,我们使用了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超链接

QQ图片20231115111109.png 上图是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…

image.png

3. 执行函数,拿到google API 返回,并获取link 列表

4.assistant 在拿到这些response后, 向user_proxy汇报,将去分析这些链接里的文章,assistant做为agent, 有远程请求网页,并parse的能力,这是我们之前在langChai中的agent能力。

image.png

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 提供了程序能力。

如何学习AI大模型?

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

在这里插入图片描述

👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。

在这里插入图片描述

1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓

在这里插入图片描述

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员辣条

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值