CrewAI初体验

什么是crewAI

CrewAI is a lean, lightning-fast Python framework built entirely from scratch—completely independent of LangChain or other agent frameworks.

总之,在我的理解中差不多就是一个等同于langchain的agent框架。

CrewAI中有3个重要的概念,分别是AgentTaskCrew

Agent: 在 CrewAI 框架中,代理是一个自主单元,能够: 1.执行特定任务 2.根据其角色和目标做出决策 3.使用工具实现目标 4.与其他代理沟通协作 5.保持交互记忆 6.在允许的情况下委派任务

Task: 在 CrewAI 框架中,任务是指由代理完成的特定任务。 任务提供执行所需的所有必要信息,例如描述、负责的代理、所需工具等,从而简化了各种操作的复杂性。 CrewAI 中的任务可以协作,需要多个代理协同工作。这通过任务属性进行管理,并由 Crew 的流程进行协调,从而增强团队合作并提高效率。

Crew: 在crewAI 中,CrewAI 代表一组协作的智能体,它们共同协作完成一组任务。每个 CrewAI 都定义了任务执行、智能体协作以及整体工作流程的策略。

写一个小玩具

定义一个Agent,该Agent充当运维操作员,实现对Linux主机的运维操作。

1
2
3
4
5
6
7
8
9
10
11
12
from crewai import Agent

operation_engineer_agent = Agent(
# Agent扮演的角色
role = 'Operation and maintenance engineer',
# Agent的目标
goal = 'Responsible for operating the Linux operating system',
# 背景故事,丰富Agent设定
backstory = 'An experienced maintenance engineer who is familiar with various Linux distributions. Knows all Linux commands. Does not install additional programs without permission, and will prompt when a program must be installed.',
llm = llm,
tools = [connect, execute]
)

定义一个Task,为运维操作员Agent定义一个需要执行的任务。

1
2
3
4
5
6
7
8
9
10
11
12
from crewai import Task

operation_task = Task(
# 详细完成的任务内容
description = 'Connect to the Linux host via ssh and execute the command to obtain the following: {requirements}',
# 想要的输出内容
expected_output = 'Extract appropriate content based on command execution results',
# 指定agent
agent = operation_engineer_agent,
# 输出文件
# output_file=
)

最后将Agent与Task绑定

1
2
3
4
5
6
7
from crewai import Crew
crew = Crew(
agents = [operation_engineer_agent],
tasks = [operation_task],
# crew运行信息
verbose = True
)

在定义Agent时,我向Agent提供了两个tool,分别是connect,execute

  • connect: 用于进行SSH登录
  • execute: 执行shell命令

这里就放一下tool如何定义。因为只是写的一个玩具,所以就不放具体实现了。在定义tool时,需要详细的编写函数说明供LLM理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from crewai.tools import tool

@tool("connect")
def connect(username, password, hostname, port)
"""
函数功能描述

Args:
各个参数作用描述

Returns:
函数返回值的结果描述
"""
pass

@tool("execute")
def execute(command)
"""
"""
pass