Agent通常通过承担特定的角色来执行任务,例如编码工程师、教师和领域专家。Profile模块旨在确定智能体的概要信息,通常将其写入提示词中以影响智能体的行为。智能体档案通常包括年龄、性别和职业等基本信息,以及反应智能体个性的心理信息和详细说明智能体之间关系的社会信息。

用于描述智能体的信息选择很大程度取决于具体的应用场景。例如,如果应用程序旨在研究人类认知过程,那么心理学信息就变得至关重要。

Profile模块

Profile模块对于DB-GPT的Agent实现非常重要,因为它们可以影响智能体的行为。以下是一些具体的介绍样例

  1. from dbgpt.agent import ConversableAgent, ProfileConfig
  2. class MySummarizerAgent(ConversableAgent):
  3. profile: ProfileConfig = ProfileConfig(
  4. # The name of the agent
  5. name="Aristotle",
  6. # The role of the agent
  7. role="Summarizer",
  8. # The core functional goals of the agent tell LLM what it can do with it.
  9. goal=(
  10. "Summarize answer summaries based on user questions from provided "
  11. "resource information or from historical conversation memories."
  12. ),
  13. # Introduction and description of the agent, used for task assignment and display.
  14. # If it is empty, the goal content will be used.
  15. desc=(
  16. "You can summarize provided text content according to user's questions"
  17. " and output the summarization."
  18. ),
  19. )
  20. def __init__(self, **kwargs):
  21. super().__init__(**kwargs)

在上面的例子中,ProfileConfig 类用于定义智能体的Profile. 这是定义智能体配置文件的简单方法,只需要指定姓名、角色、目标和描述。

让我们来看看从配置文件生成的最终Prompt。首先,我们需要淡出创建一个配置文件。

  1. from dbgpt.agent import ProfileConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. # The name of the agent
  4. name="Aristotle",
  5. # The role of the agent
  6. role="Summarizer",
  7. # The core functional goals of the agent tell LLM what it can do with it.
  8. goal=(
  9. "Summarize answer summaries based on user questions from provided "
  10. "resource information or from historical conversation memories."
  11. ),
  12. # Introduction and description of the agent, used for task assignment and display.
  13. # If it is empty, the goal content will be used.
  14. desc=(
  15. "You can summarize provided text content according to user's questions"
  16. " and output the summarization."
  17. ),
  18. )
  19. # Create a profile from the configuration
  20. real_profile = profile.create_profile()
  21. system_prompt = real_profile.format_system_prompt(question="What can you do?")
  22. user_prompt = real_profile.format_user_prompt(question="What can you do?")
  23. print(f"System Prompt: \n{system_prompt}")
  24. print("#" * 50)
  25. print(f"User Prompt: \n{user_prompt}")

运行上面的代码可以生成如下的Prompt

  1. System Prompt:
  2. You are a Summarizer, named Aristotle, your goal is Summarize answer summaries based on user questions from provided resource information or from historical conversation memories..
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. *** IMPORTANT REMINDER ***
  6. Please answer in English.
  7. ##################################################
  8. User Prompt:
  9. Question: What can you do?

正如你所看到的一样,Profile被用于生成系统和用户Prompt,它们将输入给LLM来生成回答。

因此,可以轻松看到从Profile生成到真实Prompt,这对于调试和理解Agent行为非常有用,我们不会向隐藏太多细节。

创建Profile

此部分,你将会学习到关于Agent Profile创建相关的内容。

方法一、使用ProfileConfig

正如Profile部分提到的,ProfileConfig类被用于定义Agent的Profile,其提供了一种简单定义智能体Profile的方式。

确切来说,ProfileConfig类支持以下参数

  • name: 智能体的名称
  • role: 智能体的角色
  • goal: 核心目标告诉LLM它可以做什么
  • desc: 智能体的介绍和描述,用于任务分配和显示。如果为空,则使用目标内容。
  • constraints: 可以包含多个约束和推理限制逻辑。
  • expand_prompt: 要添加提示的扩展内容,可以传递一些要添加到Prompt的自定义文本。
  • examples: Prompt中提供的一些样例

下面是使用ProfileConfig类创建的完整样例:

  1. from dbgpt.agent import ProfileConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. # The name of the agent
  4. name="Aristotle",
  5. # The role of the agent
  6. role="Summarizer",
  7. # The core functional goals of the agent tell LLM what it can do with it.
  8. goal=(
  9. "Summarize answer summaries based on user questions from provided "
  10. "resource information or from historical conversation memories."
  11. ),
  12. # Constraints of the agent
  13. constraints=[
  14. "Prioritize the summary of answers to user questions from the improved "
  15. "resource text. If no relevant information is found, summarize it from "
  16. "the historical dialogue memory given. It is forbidden to make up your "
  17. "own.",
  18. "You need to first detect user's question that you need to answer with "
  19. "your summarization.",
  20. "Extract the provided text content used for summarization.",
  21. "Then you need to summarize the extracted text content.",
  22. "Output the content of summarization ONLY related to user's question. "
  23. "The output language must be the same to user's question language.",
  24. "If you think the provided text content is not related to user "
  25. "questions at all, ONLY output 'Did not find the information you "
  26. "want.'!!.",
  27. ],
  28. # Introduction and description of the agent, used for task assignment and display.
  29. # If it is empty, the goal content will be used.
  30. desc=(
  31. "You can summarize provided text content according to user's questions"
  32. " and output the summarization."
  33. ),
  34. expand_prompt="Keep your answer concise",
  35. # Some examples in your prompt
  36. examples=""
  37. )

在上面的样例程序中,我们可以看到<font style="color:rgb(28, 30, 33);">constraints</font><font style="color:rgb(28, 30, 33);">expand_prompt</font>被添加到了Profile中

  1. real_profile = profile.create_profile()
  2. system_prompt = real_profile.format_system_prompt(question="What can you do?")
  3. user_prompt = real_profile.format_user_prompt(question="What can you do?")
  4. print(f"System Prompt: \n{system_prompt}")
  5. print("#" * 50)
  6. print(f"User Prompt: \n{user_prompt}")

运行上述代码,将生成如下的Prompts

  1. System Prompt:
  2. You are a Summarizer, named Aristotle, your goal is Summarize answer summaries based on user questions from provided resource information or from historical conversation memories..
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. Keep your answer concise
  6. *** IMPORTANT REMINDER ***
  7. Please answer in English.
  8. 1. Prioritize the summary of answers to user questions from the improved resource text. If no relevant information is found, summarize it from the historical dialogue memory given. It is forbidden to make up your own.
  9. 2. You need to first detect user's question that you need to answer with your summarization.
  10. 3. Extract the provided text content used for summarization.
  11. 4. Then you need to summarize the extracted text content.
  12. 5. Output the content of summarization ONLY related to user's question. The output language must be the same to user's question language.
  13. 6. If you think the provided text content is not related to user questions at all, ONLY output 'Did not find the information you want.'!!.
  14. ##################################################
  15. User Prompt:
  16. Question: What can you do?

方法二、使用ProfileFactory

它是一种更灵活的方式使用ProfileFactory来创建Profile

创建Profile Factory

  1. from typing import Optional
  2. from dbgpt.agent import ProfileFactory, Profile, DefaultProfile
  3. class MyProfileFactory(ProfileFactory):
  4. def create_profile(
  5. self,
  6. profile_id: int,
  7. name: Optional[str] = None,
  8. role: Optional[str] = None,
  9. goal: Optional[str] = None,
  10. prefer_prompt_language: Optional[str] = None,
  11. prefer_model: Optional[str] = None,
  12. ) -> Optional[Profile]:
  13. return DefaultProfile(
  14. name="Aristotle",
  15. role="Summarizer",
  16. goal=(
  17. "Summarize answer summaries based on user questions from provided "
  18. "resource information or from historical conversation memories."
  19. ),
  20. desc=(
  21. "You can summarize provided text content according to user's questions"
  22. " and output the summarization."
  23. ),
  24. expand_prompt="Keep your answer concise",
  25. examples=""
  26. )

使用Profile Factory

要使用Profile文件工厂,你需要将工厂传递给ProfileConfig类。在此情况下,你不需要提供智能体的姓名、角色、目标和描述。

  1. from dbgpt.agent import ProfileConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. factory=MyProfileFactory(),
  4. )

让我们看一下最终的Profile生成

  1. real_profile = profile.create_profile()
  2. system_prompt = real_profile.format_system_prompt(question="What can you do?")
  3. user_prompt = real_profile.format_user_prompt(question="What can you do?")
  4. print(f"System Prompt: \n{system_prompt}")
  5. print("#" * 50)
  6. print(f"User Prompt: \n{user_prompt}")

运行上述代码,可以看到如下的Prompt

  1. System Prompt:
  2. You are a Summarizer, named Aristotle, your goal is Summarize answer summaries based on user questions from provided resource information or from historical conversation memories..
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. Keep your answer concise
  6. *** IMPORTANT REMINDER ***
  7. Please answer in English.
  8. ##################################################
  9. User Prompt:
  10. Question: What can you do?

在本部分教程中,你学习了如何通过ProfileConfig类和ProfileFactory类创建Agent Profile的使用。使用这些方法可以灵活且轻松地定义智能体的配置,特别是当你需要创建数千个智能体时。

将Profile转换为Prompt

在前面的教程中,介绍了如何创建Profile,以及查看通过Profile生成的Prompt,本部分教程主要是介绍更多的细节。

什么是Prompt模版?

在前面的章节中,我们使用内部模版来生成Prompt,让我们来看看模版内容

  1. from dbgpt.agent import ProfileConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. # The name of the agent
  4. name="Aristotle",
  5. # The role of the agent
  6. role="Summarizer",
  7. # The core functional goals of the agent tell LLM what it can do with it.
  8. goal=(
  9. "Summarize answer summaries based on user questions from provided "
  10. "resource information or from historical conversation memories."
  11. ),
  12. # Introduction and description of the agent, used for task assignment and display.
  13. # If it is empty, the goal content will be used.
  14. desc=(
  15. "You can summarize provided text content according to user's questions"
  16. " and output the summarization."
  17. ),
  18. )
  19. real_profile = profile.create_profile()
  20. print(f"System Prompt Template: \n{real_profile.get_system_prompt_template()}")
  21. print("#" * 50)
  22. print(f"User Prompt Template: \n{real_profile.get_user_prompt_template()}")

运行上述代码将生成下面的输出

  1. System Prompt Template:
  2. You are a {{ role }}, {% if name %}named {{ name }}, {% endif %}your goal is {{ goal }}.
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. {% if resource_prompt %}{{ resource_prompt }}
  6. {% endif %}{% if expand_prompt %}{{ expand_prompt }}
  7. {% endif %}
  8. *** IMPORTANT REMINDER ***
  9. {% if language == 'zh' %}Please answer in simplified Chinese.
  10. {% else %}Please answer in English.
  11. {% endif %}
  12. {% if constraints %}{% for constraint in constraints %}{{ loop.index }}. {{ constraint }}
  13. {% endfor %}{% endif %}
  14. {% if examples %}You can refer to the following examples:
  15. {{ examples }}{% endif %}
  16. {% if out_schema %} {{ out_schema }} {% endif %}
  17. ##################################################
  18. User Prompt Template:
  19. {% if most_recent_memories %}Most recent observations:
  20. {{ most_recent_memories }}
  21. {% endif %}
  22. {% if question %}Question: {{ question }}
  23. {% endif %}

该模版是jinjia2模版,我们现在只在智能体中使用jinjia2模版,因为它既简单又灵活。

使用自定义Prompt模版

首先,创建一个简单的系统提示模版和用户提示模版

  1. my_system_prompt_template = """\
  2. You are a {{ role }}, {% if name %}named {{ name }}, {% endif %}your goal is {{ goal }}.
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. *** IMPORTANT REMINDER ***
  6. {% if language == 'zh' %}\
  7. Please answer in simplified Chinese.
  8. {% else %}\
  9. Please answer in English.
  10. {% endif %}\
  11. """ # noqa
  12. my_user_prompt_template = "User question: {{ question }}"

然后,使用自定义提示模版创建Profile

  1. from dbgpt.agent import ProfileConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. # The name of the agent
  4. name="Aristotle",
  5. # The role of the agent
  6. role="Summarizer",
  7. # The core functional goals of the agent tell LLM what it can do with it.
  8. goal=(
  9. "Summarize answer summaries based on user questions from provided "
  10. "resource information or from historical conversation memories."
  11. ),
  12. # Introduction and description of the agent, used for task assignment and display.
  13. # If it is empty, the goal content will be used.
  14. desc=(
  15. "You can summarize provided text content according to user's questions"
  16. " and output the summarization."
  17. ),
  18. system_prompt_template=my_system_prompt_template,
  19. user_prompt_template=my_user_prompt_template,
  20. )
  21. real_profile = profile.create_profile()
  22. system_prompt = real_profile.format_system_prompt(question="What can you do?")
  23. user_prompt = real_profile.format_user_prompt(question="What can you do?")
  24. print(f"System Prompt: \n{system_prompt}")
  25. print("#" * 50)
  26. print(f"User Prompt: \n{user_prompt}")

运行上述代码,会产生如下的Prompt

  1. System Prompt:
  2. You are a Summarizer, named Aristotle, your goal is Summarize answer summaries based on user questions from provided resource information or from historical conversation memories..
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. *** IMPORTANT REMINDER ***
  6. Please answer in English.
  7. ##################################################
  8. User Prompt:
  9. User question: What can you do?

动态Profile

有时,我们只想以简单的方式修改Profile文件的一部分,这里我们介绍如何创建动态Profile文件。

Profile中的动态字段

这里我们使用DynConfig创建动态配置文件,你可以修改原始文件中的字段。

创建一个名为profile_dynamic.py的python文件并添加如下代码:

  1. from dbgpt.agent import ProfileConfig, DynConfig
  2. profile: ProfileConfig = ProfileConfig(
  3. # The name of the agent
  4. name=DynConfig(
  5. "Aristotle",
  6. key="summary_profile_name",
  7. provider="env"
  8. ),
  9. # The role of the agent
  10. role="Summarizer",
  11. )

在上面的例子中,我们使用了DynConfig创建了一个动态Profile文件,字段为”name”, 默认值为”Aristotle”, 键为”summary_profile_name”, provider为”env” 表示值为该字段将从环节变量中读取,

然后,你可以根据配置创建Profile文件并生成提示。

  1. real_profile = profile.create_profile()
  2. system_prompt = real_profile.format_system_prompt(question="What can you do?")
  3. user_prompt = real_profile.format_user_prompt(question="What can you do?")
  4. print(f"System Prompt: \n{system_prompt}")
  5. print("#" * 50)
  6. print(f"User Prompt: \n{user_prompt}")

不设置环境变量,运行上述代码:

  1. python profile_dynamic.py

输出如下

  1. System Prompt:
  2. You are a Summarizer, named Aristotle, your goal is None.
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. *** IMPORTANT REMINDER ***
  6. Please answer in English.
  7. ##################################################
  8. User Prompt:
  9. Question: What can you do?

带环境变量运行上述代码

  1. summary_profile_name="Plato" python profile_dynamic.py

输出如下

  1. System Prompt:
  2. You are a Summarizer, named Plato, your goal is None.
  3. Please think step by step to achieve the goal. You can use the resources given below.
  4. At the same time, please strictly abide by the constraints and specifications in IMPORTANT REMINDER.
  5. *** IMPORTANT REMINDER ***
  6. Please answer in English.
  7. ##################################################
  8. User Prompt:
  9. Question: What can you do?