使用CAMEL创建第一个Agent Society
CAMEL介绍
CAMEL 是一个开源社区,致力于探索代理的扩展规律。相信,在大规模研究这些代理可以提供对其行为、能力和潜在风险的宝贵见解。为了促进这一领域的研究,实现了并支持各种类型的代理、任务、提示、模型和模拟环境。
GitHub地址:https://github.com/camel-ai/camel

创建第一个代理社会
Society模块是 CAMEL 的核心模块之一。通过模拟信息交换过程,该模块研究代理之间的社会行为。
RolePlaying是 CAMEL 的一个独特的协作代理框架。通过这个框架,CAMEL 中的代理克服了诸如角色转换、助手重复指令、敷衍的回答、消息无限循环以及对话终止条件等众多挑战。
现在就来使用CAMEL 创建一个Agent Society。
编写.env如下所示:
Silicon_Model_ID="Qwen/Qwen2.5-72B-Instruct"
ZHIPU_Model_ID="THUDM/GLM-4-32B-0414"
SiliconCloud_API_KEY="你的api key"
SiliconCloud_Base_URL="https://api.siliconflow.cn/v1"
想要使用两个不同的模型:
from camel.societies import RolePlaying
from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.types import ModelPlatformType
import pathlib
import os
from dotenv import load_dotenv
sys_msg = 'You are a curious stone wondering about the universe.'
base_dir = pathlib.Path(__file__).parent.parent
env_path = base_dir / ".env"
load_dotenv(dotenv_path=str(env_path))
modeltype = os.getenv("Silicon_Model_ID")
modeltype2= os.getenv("ZHIPU_Model_ID")
api_key = os.getenv("SiliconCloud_API_KEY")
base_url = os.getenv("SiliconCloud_Base_URL")
siliconcloud_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
model_type=modeltype,
api_key=api_key,
url=base_url,
model_config_dict={"temperature": 0.4, "max_tokens": 4096},
)
siliconcloud_model2 = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
model_type=modeltype2,
api_key=api_key,
url=base_url,
model_config_dict={"temperature": 0.4, "max_tokens": 4096},
)
设置任务、AI用户、AI助手参数:
task_kwargs = {
'task_prompt': '制定一个新人小白学习esp32的教程,使用中文回答。',
'with_task_specify': True,
'task_specify_agent_kwargs': {'model': siliconcloud_model}
}
user_role_kwargs = {
'user_role_name': '一个esp32小白',
'user_agent_kwargs': {'model': siliconcloud_model}
}
assistant_role_kwargs = {
'assistant_role_name': '一个esp32专家',
'assistant_agent_kwargs': {'model': siliconcloud_model2}
}
构建社会:
society = RolePlaying(
**task_kwargs, # The task arguments
**user_role_kwargs, # The instruction sender's arguments
**assistant_role_kwargs, # The instruction receiver's arguments
)
让这个社会最多运行10轮:
def is_terminated(response):
"""
Give alerts when the session should be terminated.
"""
if response.terminated:
role = response.msg.role_type.name
reason = response.info['termination_reasons']
print(f'AI {role} terminated due to {reason}')
return response.terminated
def run(society, round_limit: int=10):
# Get the initial message from the ai assistant to the ai user
input_msg = society.init_chat()
# Starting the interactive session
for _ in range(round_limit):
# Get the both responses for this round
assistant_response, user_response = society.step(input_msg)
# Check the termination condition
if is_terminated(assistant_response) or is_terminated(user_response):
break
# Get the results
print(f'[AI User] {user_response.msg.content}.\n')
# Check if the task is end
if 'CAMEL_TASK_DONE' in user_response.msg.content:
break
print(f'[AI Assistant] {assistant_response.msg.content}.\n')
# Get the input message for the next round
input_msg = assistant_response.msg
return None
if __name__ == "__main__":
run(society, round_limit=10)
查看效果。
第一轮:

第二轮:

AI用户会重新问一个与解决任务相关的问题。
第三轮:

第四轮:

第五轮:

第六轮:

第7轮:

当达到令牌限制时,为了满足限制,将从记忆中删除部分消息。
如果10轮还没完成的话会直接结束:

构建这个Agent Society有什么用呢?
我觉得一个很有用的点就是适合头脑风暴场景,有一个问题或者方案需要详细讨论,那就丢给两个或更多的AI,让它们进行详细讨论,我们先看一下它们的讨论情况,看看是否有一些有效的建议,看看能不能找到一些灵感。
如果觉得在控制台中不好看,可以将结果写入文件中:
def run(society, round_limit: int=10):
# Get the initial message from the ai assistant to the ai user
input_msg = society.init_chat()
# Starting the interactive session
for _ in range(round_limit):
# Get the both responses for this round
assistant_response, user_response = society.step(input_msg)
# Check the termination condition
if is_terminated(assistant_response) or is_terminated(user_response):
break
# Get the results
print(f'[AI User] {user_response.msg.content}.\n')
# 写入一个md文件
with open('output.md', 'a', encoding='utf-8') as f:
f.write(f'[AI User] {user_response.msg.content}.\n')
# Check if the task is end
if 'CAMEL_TASK_DONE' in user_response.msg.content:
break
print(f'[AI Assistant] {assistant_response.msg.content}.\n')
# 写入一个md文件
with open('output.md', 'a', encoding='utf-8') as f:
f.write(f'[AI Assistant] {assistant_response.msg.content}.\n')
# Get the input message for the next round
input_msg = assistant_response.msg
return None
对于这个任务、AI用户、AI助手参数:
task_kwargs = {
'task_prompt': '制定一个新人小白学习C#的教程,使用中文回答。',
'with_task_specify': True,
'task_specify_agent_kwargs': {'model': siliconcloud_model}
}
user_role_kwargs = {
'user_role_name': '一个想要学习C#小白',
'user_agent_kwargs': {'model': siliconcloud_model}
}
assistant_role_kwargs = {
'assistant_role_name': '一个C#专家',
'assistant_agent_kwargs': {'model': siliconcloud_model2}
}
写入到output.md文件的内容如下所示:

相对于自己一次一次与AI交互,有些时候自己定义一些角色,让它们自己去交互,我们只看它们的结果,还是更高效一些的。
相关推荐
使用CAMEL创建第一个Agent Society的更多相关文章
- 创建第一个 local network(I) - 每天5分钟玩转 OpenStack(80)
在 ML2 配置文件中 enable local network 后,本节将开始创建第一个 local network. 我们将通过 Web GUI 创建第一个 local network. 首先确保 ...
- 用Kotlin创建第一个Android项目(KAD 01)
原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...
- 创建第一个 local network(II)- 每天5分钟玩转 OpenStack(81)
上一节通过 Web GUI 创建了 “first_local_net”,本节我们需要搞清楚底层网络结构有了哪些变化? 点击 “first_local_net” 链接,显示 network 的 subn ...
- 3.创建第一个android项目
安卓开发学习笔记 1.安卓开发之环境搭建 2.SDK目录结构和adb工具及命令介绍 3.创建第一个android项目 1.打开Eclipse,选择File——>new——>others.. ...
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- access中根据一个表创建另一个
access中根据一个表创建另一个 SELECT * INTO newTableFROM zD_qlr; SELECT * into mdFROM zd IN 'E:\fz\高阳\大姚\fz\bz\b ...
- 创建第一个ArcGIS API for Silverlight应用
原文:创建第一个ArcGIS API for Silverlight应用 在完成前面的开发环境搭建以后,接下来实现我们的第一个ArcGIS API forSilverlight应用程序. 接下来我们一 ...
- 创建第一个MVC
创建第一个MVC(asp.net)和默认路由设置 Asp.net的MVC已经出到了4.0,我用的是visual studio2013,接下来努力学下MVC,学之前的话我建议大家先去学下三层(分别是DA ...
- 创建第一个UI
创建一个2D UI 制作UI时,首先要创建UI的"根".在Unity顶部NGUI菜单中选择Create,然后选择2D UI. 创建完成后,在Scene窗口中,NGUI自动生成了一个 ...
- WCF学习系列一_创建第一个WCF服务
原创作者:灰灰虫的家http://hi.baidu.com/grayworm WCF开发实战系列一:创建第一个WCF服务 在这个实战中我们将使用DataContract,ServiceContract ...
随机推荐
- oracle使用存储过程返回游标实现报表查询
最近在oracle中通过存储过程实现一个报表查询,查询涉及到数据计算这里使用了临时表和存储过程实现输出查询,java接受游标变量返回结果集 第一步.创建统计使用的临时表 CREATE GLOBAL T ...
- java中的HsahMap
HsahMap HashMap 是 Java 中最常用的集合类之一,它实现了 Map 接口,基于哈希表存储键值对 HashMap的存储是无顺序的 HashMap存储的是键值对(key-value)其中 ...
- 云数据库与Web网站:构建高效、可扩展的网络应用
本文分享自天翼云开发者社区<云数据库与Web网站:构建高效.可扩展的网络应用>,作者:3****m 一.云数据库与Web网站的关系 云数据库与Web网站之间存在着密切的关系.Web网站需要 ...
- 虚拟化技术 - CPU虚拟化
本文分享自天翼云开发者社区<虚拟化技术 - CPU虚拟化>,作者:谢****悦 物理机器是由CPU,内存和I/O设备等一组资源构成的实体.虚拟机也一样,由虚拟CPU,虚拟内存和虚拟I/O设 ...
- DeepSeek-R1本地部署如何选择适合你的版本?看这里
DeepSeek-R1本地部署:选择最适合你的版本,轻松搞定! 关于本地部署DeepSeek-R1前期知识 如果你正在考虑将DeepSeek-R1部署到本地服务器上,了解每种类型的硬件需求是非常重要的 ...
- deepseek等AI工具是程序员技能发展的双刃剑
2025年,全球已有73%的程序员日常使用AI编码工具(Gartner 2025Q1数据).当我们惊叹于GitHub Copilot生成完整功能模块仅需10秒时,也需要警惕一个现象:新一代程序员在ID ...
- 实战AI大模型辅助编程:新安江水文模型和SCE-UA优化算法的移植与实现
新安江水文模型与 SCE-UA 优化算法是水文学和水资源管理领域的重要工具,二者结合使用可以有效模拟流域的水文过程并优化模型参数. 新安江水文模型是一种概念性水文模型,主要用于模拟流域的降雨-径流关系 ...
- 【效能提升】测试人员提bug,应该提供哪些信息以便排查问题?
背景 我们在运维企业级应用时,会遇到很多Bug. 有时候,测试人员或业务方反馈bug,描述得不够详细,我们基于他的描述很难清晰地了解情况,以解决bug. 一般情况下,我们会跟他询问更多的详情,才能知悉 ...
- 傻妞教程——如何获取天行数据服务的Key
在傻妞插件列表中,比如油价查询.舔狗语录等需要申请天行KEY,才能使用 1.打开 天行数据官网注册登录 2.在控制台首页完成实名认证 3.在左侧数据管理里面获取你的秘钥Key 4.回到傻妞已安装的插件 ...
- NetPad:一个.NET开源、跨平台的C#编辑器
前言 今天大姚给大家分享一个基于.NET开源.跨平台的C#编辑器和游乐场:NetPad. 项目介绍 NetPad是一个基于.NET开源(MIT License).跨平台的C#编辑器和游乐场,它允许用户 ...