为什么要自定义提示?

虽然 CrewAI 的默认提示适用于许多场景,但底层自定义为实现更灵活、更强大的代理行为打开了大门。以下是您可能需要利用这种更深层控制的原因:

  1. 针对特定 LLM 进行优化 – 不同的模型(例如 GPT-4、Claude 或 Llama)在与其独特架构量身定制的提示格式下表现最佳。
  2. 更改语言 – 构建专门以非英语语言运行的代理,精确处理细微差别。
  3. 针对复杂领域进行专业化 – 为医疗、金融或法律等高度专业化行业调整提示。
  4. 调整语气和风格 – 使代理更正式、更随意、更具创意或更具分析性。
  5. 支持超高度自定义用例 – 利用高级提示结构和格式来满足复杂、项目特定的需求。

本指南探讨了如何在底层利用 CrewAI 的提示,让您对代理的思考和交互方式进行细粒度控制。

理解 CrewAI 的提示系统

在底层,CrewAI 采用模块化提示系统,您可以对其进行广泛自定义

  • 代理模板 – 管理每个代理处理其指定角色的方法。
  • 提示片段 – 控制诸如任务、工具使用和输出结构等专业行为。
  • 错误处理 – 指导代理如何响应失败、异常或超时。
  • 工具特定提示 – 定义关于如何调用或使用工具的详细说明。

查看 CrewAI 仓库中的原始提示模板,了解这些元素是如何组织的。您可以根据需要覆盖或调整它们,以解锁高级行为。

管理提示文件的最佳实践

进行底层提示自定义时,请遵循以下指南,以保持条理性和可维护性

  1. 保持文件分离 – 将您的自定义提示存储在主代码库之外的专用 JSON 文件中。
  2. 版本控制 – 在您的仓库中跟踪更改,确保对提示调整进行清晰的文档记录。
  3. 按模型或语言组织 – 使用 prompts_llama.jsonprompts_es.json 等命名方案快速识别专用配置。
  4. 记录更改 – 提供注释或维护 README 文件,详细说明您的自定义目的和范围。
  5. 尽量减少修改 – 只覆盖您确实需要调整的特定片段,保留其他所有内容的默认功能。

自定义提示的最简单方法

一种简单的方法是为要覆盖的提示创建 JSON 文件,然后将您的团队指向该文件

  1. 创建一个包含您更新的提示片段的 JSON 文件。
  2. 通过 Crew 中的 prompt_file 参数引用该文件。

然后 CrewAI 会将您的自定义与默认设置合并,这样您就不必重新定义每个提示了。方法如下:

示例:基本提示自定义

创建一个名为 custom_prompts.json 的文件,包含您想要修改的提示。确保其中列出了所有应包含的顶级提示,而不仅仅是您更改的部分

{
  "slices": {
    "format": "When responding, follow this structure:\n\nTHOUGHTS: Your step-by-step thinking\nACTION: Any tool you're using\nRESULT: Your final answer or conclusion"
  }
}

然后像这样集成它

from crewai import Agent, Crew, Task, Process

# Create agents and tasks as normal
researcher = Agent(
    role="Research Specialist",
    goal="Find information on quantum computing",
    backstory="You are a quantum physics expert",
    verbose=True
)

research_task = Task(
    description="Research quantum computing applications",
    expected_output="A summary of practical applications",
    agent=researcher
)

# Create a crew with your custom prompt file
crew = Crew(
    agents=[researcher],
    tasks=[research_task],
    prompt_file="path/to/custom_prompts.json",
    verbose=True
)

# Run the crew
result = crew.kickoff()

通过这些简单的修改,您可以获得对代理如何沟通和解决任务的底层控制权。

针对特定模型进行优化

不同的模型在结构不同的提示下表现最佳。进行更深层次的调整可以通过使您的提示与模型的细微差别对齐来显著提升性能。

示例:Llama 3.3 提示模板

例如,在使用 Meta 的 Llama 3.3 时,更深层次的自定义可能需要反映在以下链接中描述的推荐结构:https://www.llama.com/docs/model-cards-and-prompt-formats/llama3_1/#prompt-template

以下是一个示例,重点说明您如何在代码中微调代理以利用 Llama 3.3

from crewai import Agent, Crew, Task, Process
from crewai_tools import DirectoryReadTool, FileReadTool

# Define templates for system, user (prompt), and assistant (response) messages
system_template = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>{{ .System }}<|eot_id|>"""
prompt_template = """<|start_header_id|>user<|end_header_id|>{{ .Prompt }}<|eot_id|>"""
response_template = """<|start_header_id|>assistant<|end_header_id|>{{ .Response }}<|eot_id|>"""

# Create an Agent using Llama-specific layouts
principal_engineer = Agent(
    role="Principal Engineer",
    goal="Oversee AI architecture and make high-level decisions",
    backstory="You are the lead engineer responsible for critical AI systems",
    verbose=True,
    llm="groq/llama-3.3-70b-versatile",  # Using the Llama 3 model
    system_template=system_template,
    prompt_template=prompt_template,
    response_template=response_template,
    tools=[DirectoryReadTool(), FileReadTool()]
)

# Define a sample task
engineering_task = Task(
    description="Review AI implementation files for potential improvements",
    expected_output="A summary of key findings and recommendations",
    agent=principal_engineer
)

# Create a Crew for the task
llama_crew = Crew(
    agents=[principal_engineer],
    tasks=[engineering_task],
    process=Process.sequential,
    verbose=True
)

# Execute the crew
result = llama_crew.kickoff()
print(result.raw)

通过这种更深层次的配置,您可以对基于 Llama 的工作流程进行全面的底层控制,而无需单独的 JSON 文件。

结论

CrewAI 中的底层提示自定义为实现超高度自定义和复杂的用例打开了大门。通过建立组织良好的提示文件(或直接内联模板),您可以适应各种模型、语言和专业领域。这种灵活性确保您可以精确地塑造所需的 AI 行为,同时了解当您不覆盖默认设置时,CrewAI 仍然提供可靠的默认功能。

您现在已经掌握了在 CrewAI 中进行高级提示自定义的基础。无论是为了适应特定模型的结构还是特定领域的约束,这种底层方法都可以让您以高度专业化的方式塑造代理交互。