极客时间:本地Autogen创建基于图的FSM群聊基于llama-3.1–8b-instruct在Nvidia NIM

  每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行! 订阅:https://rengongzhineng.io/

在启动AutoGen Studio后,构建了带有“GroupChat”的代理,结果在工作流阶段遇到了问题——只能添加一个接收者。

花了一个小时搜索发现,这在AutoGen Studio 2.0中并不支持。于是转向了非Studio的方法。

首先,启动AutoGen
创建一个虚拟环境并安装所需的依赖:

# 创建虚拟环境
python -m venv autogen_env
# 激活虚拟环境
source autogen_env/bin/activate
# 安装依赖
pip install autogen

项目结构


计划使用AutoGen搭建一个基于图形的有限状态机(FSM)群聊系统。场景设定在中世纪王国,角色扮演包括国王、王后、骑士、巫师和将军。每个角色根据预定义的转换图进行互动,使对话动态且结构化。FSM图如下:

项目结构组织成多个Python文件:

project/
│
├── config.py
├── group_chat.py
├── main.py
└── agents/
    ├── __init__.py
    ├── king.py
    ├── knight.py
    ├── wizard.py
    ├── queen.py
    └── general.py

配置


计划使用托管在Nvidia NIM上的llama-3.1。(稍后讨论一些小问题)

config.py

config_list = [
    {
        "model": "meta/llama-3.1-8b-instruct",
        "base_url": "https://integrate.api.nvidia.com/v1",
        "api_key": "nvapi-API key",
        "temperature": 0.2,
        "top_p": 0.7,
        "max_tokens": 1024,
    }
]
llm_config = {"config_list": config_list, "seed": 42}

代理


agents/init.py

此文件可以是空的,也可以包含必要的导入语句

agents/king.py

python
import autogen
from config import llm_config
king = autogen.AssistantAgent(
    name="King",
    llm_config=llm_config,
    system_message="You are the King of the medieval kingdom, wise and just. You are discussing how to protect your kingdom from a dragon."
)

agents/knight.py


import autogen
from config import llm_config
knight = autogen.AssistantAgent(
    name="Knight",
    llm_config=llm_config,
    system_message="You are a brave Knight, skilled in combat. You are discussing strategies to defend the kingdom from a dragon."
)

agents/wizard.py

import autogen
from config import llm_config
wizard = autogen.AssistantAgent(
    name="Wizard",
    llm_config=llm_config,
    system_message="You are a wise Wizard, knowledgeable in magic. You are discussing magical solutions to protect the kingdom from a dragon."
)


 

agents/queen.py

import autogen
from config import llm_config
queen = autogen.AssistantAgent(
    name="Queen",
    llm_config=llm_config,
    system_message="You are the Queen of the medieval kingdom, diplomatic and strategic. You are discussing how to protect your kingdom from a dragon."
)


 

agents/general.py

import autogen
from config import llm_config
general = autogen.AssistantAgent(
    name="General",
    llm_config=llm_config,
    system_message="You are the General of the medieval kingdom, experienced in warfare. You are discussing military strategies to protect the kingdom from a dragon."
)


 

群聊设置


group_chat.py

import autogen
from agents.king import king
from agents.knight import knight
from agents.wizard import wizard
from agents.queen import queen
from agents.general import general
from config import llm_config

# 使用代理对象定义转换图
graph_dict = {}
graph_dict[king] = [queen, knight]
graph_dict[queen] = [king, wizard]
graph_dict[knight] = [general, wizard]
graph_dict[wizard] = [king, knight]
graph_dict[general] = [queen, knight]
agents = [king, queen, knight, wizard, general]

# 创建群聊
group_chat = autogen.GroupChat(
    agents=agents, messages=[], max_round=5,
    allowed_or_disallowed_speaker_transitions=graph_dict,
    allow_repeat_speaker=None,
    speaker_transitions_type="allowed"
)

# 创建管理器
manager = autogen.GroupChatManager(
    groupchat=group_chat,
    llm_config=llm_config,
    is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config=False,
)


 

主脚本


main.py

from group_chat import manager
from agents.king import king

# 初始化对话
king.initiate_chat(
    manager, message="如何保护我们的王国免受龙的侵害?"
)

输出结果如下:

❯ python main.py
King (to chat_manager):

如何保护我们的王国免受龙的侵害?

--------------------------------------------------------------------------------
[autogen.oai.client: 08-04 14:35:53] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

Next speaker: Knight

[autogen.oai.client: 08-04 14:35:56] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Knight (to chat_manager):

A noble quest indeed! As a seasoned Knight, I've given this much thought. Here's a plan to safeguard our kingdom:

**First, gather intelligence**: We must learn more about the dragon's habits, weaknesses, and patterns. Send out scouts to gather information on its lair, its feeding grounds, and any potential vulnerabilities.

**Second, fortify our defenses**: Reinforce our castle walls, towers, and gates with sturdy materials like iron and stone. Ensure that our moat is deep and wide, and that our drawbridge is secure.

**Third, prepare our troops**: Assemble a team of skilled warriors, including archers, men-at-arms, and myself, of course! We'll need to be well-trained and equipped to face the dragon's flames and claws.

**Fourth, develop a strategy**: We'll need to coordinate our efforts to maximize our chances of success. I propose we use a combination of ranged attacks (arrows and catapults) to weaken the dragon from a distance, while our men-at-arms engage it in close combat.

**Fifth, utilize the terrain**: Study the landscape and identify any natural obstacles that could aid us in our battle. Perhaps we can use the nearby hills or forests to our advantage, or even lure the dragon into a trap.

**Sixth, prepare for the worst**: We must be prepared for the possibility that the dragon may breach our defenses. In that case, we'll need to have a plan in place for evacuation and emergency response.

By following these steps, I'm confident that we can protect our kingdom from the dragon's wrath. What say you, my lord? Are you ready to join me in this noble quest?

--------------------------------------------------------------------------------
[autogen.oai.client: 08-04 14:35:53] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

Next speaker: General

[autogen.oai.client: 08-04 14:35:59] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
General (to chat_manager):

My noble Knight, your plan is sound and well-thought-out. I commend your experience and strategic thinking. As the General of this kingdom, I agree that gathering intelligence, fortifying our defenses, preparing our troops, developing a strategy, utilizing the terrain, and preparing for the worst are all crucial steps in protecting our kingdom from the dragon's threat.

However, I would like to add a few suggestions to your plan. Firstly, I propose that we also establish a network of spies and informants within the kingdom to gather information on any potential dragon sympathizers or traitors who may be secretly aiding the beast. We must be vigilant and ensure that our own people are not working against us.

Secondly, I recommend that we also prepare a team of skilled engineers to develop a device that can counter the dragon's flames. Perhaps a device that can project a shield of water or a barrier of smoke to protect our troops from the dragon's fire.

Thirdly, I suggest that

 we also consider the possibility of forming an alliance with neighboring kingdoms to pool our resources and expertise in fighting the dragon. A united front against this common enemy could be our best chance of success.

Lastly, I propose that we also establish a contingency plan for the event that the dragon is not defeated, but instead, we are forced to retreat. We must have a clear plan for evacuation and a safe route for our people to escape to.

By incorporating these additional measures, I believe we can strengthen our chances of success and protect our kingdom from the dragon's threat.

Now, let us put our plan into action and prepare our kingdom for the battle ahead!

--------------------------------------------------------------------------------
[autogen.oai.client: 08-04 14:36:00] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

Next speaker: Knight

[autogen.oai.client: 08-04 14:36:02] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Knight (to chat_manager):

My lord, your suggestions are wise and well-considered. I commend your foresight and strategic thinking. By incorporating these additional measures, we can indeed strengthen our chances of success and protect our kingdom from the dragon's threat.

**Establishing a network of spies and informants**: I agree that it's essential to identify any potential dragon sympathizers or traitors within our kingdom. I'll assign a team of trusted agents to gather intelligence and monitor any suspicious activity. We must be vigilant and ensure that our own people are not working against us.

**Developing a device to counter the dragon's flames**: I'm intrigued by the idea of creating a device that can project a shield of water or a barrier of smoke to protect our troops from the dragon's fire. I'll assemble a team of skilled engineers to work on this project. Perhaps we can use a combination of waterwheels, pumps, and smoke-producing materials to create an effective barrier.

**Forming an alliance with neighboring kingdoms**: I wholeheartedly agree that a united front against the dragon is our best chance of success. I'll send out emissaries to neighboring kingdoms to propose an alliance and pool our resources and expertise. Together, we can share intelligence, coordinate our efforts, and present a stronger front against the dragon.

**Establishing a contingency plan for evacuation**: I'll work with our castle's stewards and administrators to develop a clear plan for evacuation and a safe route for our people to escape to. We must be prepared for the worst-case scenario and have a plan in place to ensure the safety of our citizens.

With these additional measures in place, I'm confident that we can protect our kingdom from the dragon's threat. I propose that we hold a council meeting with our advisors, engineers, and other key officials to discuss the details of our plan and ensure that everyone is on the same page.

Shall we convene the council meeting, my lord?

--------------------------------------------------------------------------------
[autogen.oai.client: 08-04 14:36:03] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

Next speaker: General

[autogen.oai.client: 08-04 14:36:05] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
General (to chat_manager):

My noble Knight, I'm pleased to see that you've taken my suggestions to heart and are already putting them into action. Your enthusiasm and dedication to this cause are truly inspiring.

I agree that convening a council meeting is a wise decision. It's essential that we have a clear understanding of our plan and that all key officials are aware of their roles and responsibilities.

Let us summon the council meeting forthwith! I'll instruct our heralds to spread the word and invite all relevant officials to attend. We'll gather in the great hall at dawn tomorrow, and I'll expect a thorough discussion of our plan, including the details of our intelligence gathering, device development, alliance formation, and evacuation contingency.

In the meantime, I'd like to assign a few additional tasks to our team. I want our scouts to investigate the dragon's lair and gather more information on its habits and weaknesses. I'd also like our engineers to work on a prototype of the device to counter the dragon's flames, and our emissaries to finalize the alliance with neighboring kingdoms.

Let us work together, my Knight, to ensure that our kingdom is prepared for the battle ahead. I have no doubt that with our combined efforts, we will emerge victorious and protect our people from the dragon's threat.

Now, let us take a moment to review the plan and make any final adjustments before the council meeting. I'd like to see a detailed outline of our strategy, including the roles and responsibilities of each official and the timeline for implementation.

Bring me the plan, my Knight, and let us review it together.

看起来不错?是的,但存在下面的问题:

[autogen.oai.client: 08-04 14:35:53] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

似乎并没有使用配置的Nvidia NIM上的llama-3.1-8b-instruct。那么到底用了什么?

编辑main.py中的系统消息:

from group_chat import manager
from agents.king import king

# 初始化对话
king.initiate_chat(
    manager, message="Firstly, tell what LLM, version, and base URL you are using. How should we protect our kingdom from the dragon?"
)

输出显示:

King (to chat_manager):

Firstly, tell what LLM, version, and base URL you are using. How should we protect our kingdom from the dragon?

--------------------------------------------------------------------------------
[autogen.oai.client: 08-04 15:22:10] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.

Next speaker: Knight

[autogen.oai.client: 08-04 15:22:14] {329} WARNING - Model meta/llama-3.1-8b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Knight (to chat_manager):

A noble quest indeed!

I am using the Llama 3, version 7, and the base URL is https://api-inference.huggingface.co/. I'm a large language model, my responses are generated based on patterns and associations in the data I was trained on, so I'll do my best to provide a strategic plan to defend our kingdom from the dragon.

奇怪?还在试图理解原因,敬请期待……如果知道原因,请在评论中留言。

结论


通过这种设置,可以使用基于图形的FSM方法在中世纪王国中模拟结构化对话。每个代理将根据预定义的规则进行互动,创造动态且引人入胜的对话。要运行群聊,只需执行main.py。

还有一些未解之谜待解决。下一步是探索整合更复杂的场景和更多具备高级技能的代理。也许还会加入RouteLLM部分……会很有趣!

祝编码愉快!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值