使用crewai创建属于你自己的AI团队
1.与LLMs进行在IDE中直接、无需提示的交互是工具构建者探索的一个有希望的未来方向2.将爬虫与大语言模型结合3.AvaloniaChat:一个基于大语言模型用于翻译的简单应用4.最佳实践:在AvaloniaChat中接入SiliconCloud5.AvaloniaChat—从源码构建指南6.SimpleRAG:基于WPF与Semantic Kernel实现的一个简单的RAG应用7.Semantic Kernel/C#:接入智谱AI的两种方式8.AvaloniaChat-v0.0.2:兼容智谱AI 快速使用指南9.使用SiliconCloud快速体验SimpleRAG(手把手教程)10.使用Ollama本地离线体验SimpleRAG(手把手教程)11.在NextChat中接入SiliconCloud API 体验不同的开源先进大语言模型12.AI Agents有哪些风险?哪些措施可以减少风险?13.来自OpenAI官网的Function calling介绍与最佳实践14.在LobeChat中使用SiliconCloud,一键拥有你自己的 ChatGPT/Gemini/Claude/Ollama 应用15.Semantic Kernel/C#:一种通用的Function Calling方法,文末附经测试可用的大模型16.在SimpleRAG中使用SiliconCloud快速测试Function Calling17.SimpleTranslationAIAgent:基于C#与LLM的翻译AI Agent18.SimpleTranslationAIAgent借助SiliconCloud API 构建自己的专属翻译助手19.SimpleAISearch:C# + DuckDuckGo 实现简单的AI搜索20.SimpleAIAgent:使用免费的glm-4-flash即可开始构建简单的AI Agent应用21.SimpleRAG-v1.0.3:增加文件对话功能22.如何自己动手实现一个图片解答小助手23.Microsoft.Extensions.AI 初探24.AI工具推荐——Cherry Studio25.AI提效工具推荐——Sider26.VLM-OCR-Demo:一个使用VLM用于OCR任务的示例27.使用C#构建一个论文总结AI Agent28.PaperAssistant:使用Microsoft.Extensions.AI实现29.C# AIModelRouter:使用不同的AI模型完成不同的任务30.AutoGen入门-让两个AI自行聊天完成任务31.DeepSeekV3+Roo Code,智能编码好助手32.使用Chainlit快速构建一个对话式人工智能应用体验DeepSeek-R133.AI工具推荐——open-interpreter34.浏览器自动化与AI Agent结合项目browser-use初探35.AI工具推荐:领先的开源 AI 代码助手——Continue36.Word中接入大模型教程
37.使用crewai创建属于你自己的AI团队
38.如何让大模型安全地自动生成代码并执行?CrewAI 是一个用于协调自主 AI 代理的前沿框架。
CrewAI 允许你创建 AI 团队,其中每个代理都有特定的角色、工具和目标,协同工作以完成复杂任务。
把它想象成组建你的梦之队——每个成员(代理)都带来独特的技能和专业知识,无缝协作以实现你的目标。
最近使用了crewai这个框架,我觉得是一个比较好用的AI Agent框架,因此推荐给大家。
在crewai中涵盖了Agents、Tasks、Crews、Flows、Knowledge、LLMs与Tools等这些核心概念。
接下来我将以一个具体的例子,介绍一下crewai的使用。
crewai的GitHub地址为:https://github.com/crewAIInc/crewAI
使用crewai构建一个翻译代理
创建一个python虚拟环境,安装crewai与crewai-tools。
运行命令:
crewai create crew translation_agent
会出现一个模板项目。
在config目录下,使用yaml配置agent与task:
先来设置一下代理:
file_reader
在这里设置了三个代理,分别是读取文件代理、翻译代理与文件保存代理。
再来配置一下task:
file_read_task
设置了三个任务,分别是file_read_task、translation_task与file_save_task。
完成这些任务,需要代理能够使用读取文件与保存文件的工具。
在tools目录下可以写工具代码:
file_read_tool工具代码:
from typing import Any, Optional, Type
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
class FileReadToolSchema(BaseModel):
"""Input for FileReadTool."""
# Mandatory file full path to read the file
# 必须的文件全路径,以读取文件
file_path: str = Field(..., description="Mandatory file full path to read the file")
class FileReadTool(BaseTool):
"""A tool for reading file contents.
This tool inherits its schema handling from BaseTool to avoid recursive schema
definition issues. The args_schema is set to FileReadToolSchema which defines
the required file_path parameter. The schema should not be overridden in the
constructor as it would break the inheritance chain and cause infinite loops.
The tool supports two ways of specifying the file path:
1. At construction time via the file_path parameter
2. At runtime via the file_path parameter in the tool's input
Args:
file_path (Optional[str]): Path to the file to be read. If provided,
this becomes the default file path for the tool.
**kwargs: Additional keyword arguments passed to BaseTool.
Example:
>>> tool = FileReadTool(file_path="/path/to/file.txt")
>>> content = tool.run() # Reads /path/to/file.txt
>>> content = tool.run(file_path="/path/to/other.txt") # Reads other.txt
用于读取文件内容的工具。
该工具继承自 BaseTool 的 schema 处理,以避免递归 schema 定义问题。args_schema 设置为 FileReadToolSchema,定义了必需的 file_path 参数。构造函数中不应该覆盖 schema,否则会破坏继承链并导致无限循环。
该工具支持两种指定文件路径的方法:
在构造时通过 file_path 参数
在运行时通过工具的输入参数 file_path
参数:
file_path (可选[str]): 要读取的文件路径。如果提供,则成为工具的默认文件路径。
**kwargs: 传递给 BaseTool 的其他关键字参数。
示例:
>>> tool = FileReadTool(file_path="/path/to/file.txt")
>>> content = tool.run() # 读取 /path/to/file.txt
>>> content = tool.run(file_path="/path/to/other.txt") # 读取 other.txt
"""
name: str = "Read a file's content"
description: str = "A tool that reads the content of a file. To use this tool, provide a 'file_path' parameter with the path to the file you want to read."
args_schema: Type[BaseModel] = FileReadToolSchema
file_path: Optional[str] = None
def __init__(self, file_path: Optional[str] = None, **kwargs: Any) -> None:
"""
Initialize the FileReadTool.
Args:
file_path (Optional[str]): Path to the file to be read. If provided,
this becomes the default file path for the tool.
**kwargs: Additional keyword arguments passed to BaseTool.
初始化 FileReadTool。
参数:
file_path(可选[str]):要读取的文件路径。如果提供,则此路径成为工具的默认文件路径。
**kwargs:传递给 BaseTool 的其他关键字参数。
"""
if file_path is not None:
kwargs['description'] = f"A tool that reads file content. The default file is {file_path}, but you can provide a different 'file_path' parameter to read another file."
super().__init__(**kwargs)
self.file_path = file_path
def _run(
self,
**kwargs: Any,
) -> str:
file_path = kwargs.