提示#
1. 概念#
提示模块引导AI模型生成准确、上下文相关且个性化的输出。它包含针对不同任务设计的各种提示模板和词典,例如角色描述、代码生成、评估、文本嵌入、错位任务、对象识别等。您还可以创建自己的提示来定制专属的AI代理。
2. 开始使用#
2.1 使用提示模板#
CAMEL提供多样且全面的提示模板,让用户可以轻松创建AI专家。例如,这里我们将它们聚合起来创建一个特定任务的代理:
from camel.agents import TaskSpecifyAgent
from camel.configs import ChatGPTConfig
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType, TaskType
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
)
task_specify_agent = TaskSpecifyAgent(
model=model, task_type=TaskType.AI_SOCIETY
)
specified_task_prompt = task_specify_agent.run(
task_prompt="Improving stage presence and performance skills",
meta_dict=dict(
assistant_role="Musician", user_role="Student", word_limit=100
),
)
print(f"Specified task prompt:\n{specified_task_prompt}\n")
在创建任务指定代理时设置task_type=TaskType.AI_SOCIETY,它将自动调用带有模板AISocietyPromptTemplateDict.TASK_SPECIFY_PROMPT的提示。您可以设置希望助手扮演的角色。输出将如下所示:
>> Response:
Musician will help Student enhance stage presence by practicing engaging eye contact, dynamic movement, and expressive gestures during a mock concert, followed by a review session with video playback to identify strengths and areas for improvement.
2.2 使用您自己的提示#
CAMEL还确保用户能够高度灵活地创建相同的提示。仍然以TaskSpecifyAgent为例,这里有一个演示供您传递自己的提示模板:
from camel.agents import TaskSpecifyAgent
from camel.configs import ChatGPTConfig
from camel.models import ModelFactory
from camel.prompts import TextPrompt
from camel.types import ModelPlatformType, ModelType
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
)
my_prompt_template = TextPrompt(
'Here is a task: I\'m a {occupation} and I want to {task}. Help me to make this task more specific.'
) # Write anything you would like to use as prompt template
task_specify_agent = TaskSpecifyAgent(
model=model, task_specify_prompt=my_prompt_template
)
response = task_specify_agent.run(
task_prompt="get promotion",
meta_dict=dict(occupation="Software Engineer"),
)
print(response)
响应将如下所示:
>>> Response:
Certainly! To make the task of getting a promotion more specific, you can break it down into actionable steps and set clear, measurable goals. Here’s a more detailed plan:
### 1. **Set Clear Objectives**
- **Identify the Promotion Criteria:** Understand what skills, achievements, and experiences are required for the promotion.
- **Define Your Desired Position:** Specify the role or title you are aiming for.
### 2. **Skill Development**
- **Technical Skills:** Identify any technical skills that are necessary for the promotion and create a plan to acquire or improve them (e.g., mastering a new programming language, learning about system architecture, etc.).
- **Soft Skills:** Focus on improving soft skills such as leadership, communication, and teamwork.
## 3. Introduction to `CodePrompt` Class
In this part, we will explore the `CodePrompt` class, which is a class that represents a code prompt. It extends the `TextPrompt` class, which in turn extends the built-in `str` class. The `CodePrompt` class provides additional functionality related to code execution and handling.
### Importing the `CodePrompt` Class
To use the `CodePrompt` class, you need to import it. Here's an example of how to import the class:
```python
from camel.prompts import CodePrompt
创建CodePrompt实例#
要创建一个CodePrompt实例,您只需实例化该类,提供代码字符串和代码类型作为输入参数。
code_prompt = CodePrompt("a = 1 + 1", code_type="python")
在这个示例中,我们创建了一个CodePrompt实例,代码字符串为"a = 1 + 1"。同时我们将代码类型指定为"python"。如果不需要,代码类型可以设置为None。
访问代码和代码类型#
一旦你有了一个CodePrompt实例,你可以通过以下方式访问代码字符串和代码类型:
code_prompt: 访问提示的代码字符串。code_type: 访问与提示相关联的代码类型。
print(code_prompt)
# >>> "a = 1 + 1"
print(code_prompt.code_type)
# >>> "python"
修改代码类型#
如果需要更改与CodePrompt实例关联的代码类型,可以使用set_code_type方法。该方法接收一个代码类型作为参数,并更新该实例的代码类型。
code_prompt = CodePrompt("a = 1 + 1")
print(code_prompt.code_type)
# >>> None
code_prompt.set_code_type("python")
print(code_prompt.code_type)
# >>> "python"
在这个示例中,我们将CodePrompt实例的代码类型从None更改为"python"。
执行代码#
CodePrompt 类提供了一个名为 execute 的方法,允许您执行与提示相关联的代码字符串。它返回一个包含标准输出和标准错误的字符串。
code_prompt = CodePrompt("a = 1 + 1\nb = a + 1\nprint(a,b)", code_type="python")
output = code_prompt.execute()
# Running code? [Y/n]: y
print(output)
# >>> 2 3
4. 使用TextPrompt类编写您的提示#
在这一部分,我们将探索TextPrompt类并了解其功能。TextPrompt类是内置str类的子类,为处理文本提示提供了额外功能。我们将涵盖以下主题:
TextPrompt类简介使用
TextPrompt类
TextPrompt 类介绍#
TextPrompt类表示一个文本提示,并扩展了str类的功能。它提供了一个名为key_words的属性,该属性返回一组字符串,表示提示中的关键词。
以下是使用TextPrompt类的示例:
from camel.prompts import TextPrompt
prompt = TextPrompt('Please enter your name and age: {name}, {age}')
print(prompt)
>>> 'Please enter your name and age: {name}, {age}'
在上面的示例中,我们创建了一个TextPrompt实例,其中包含姓名和年龄关键字的格式化字符串。我们可以像Python的str一样打印TextPrompt。
使用 TextPrompt 类#
一旦我们创建了一个TextPrompt实例,就可以使用该类提供的各种方法和属性来操作和处理文本提示。
key_words 属性#
key_words 属性返回一组字符串,表示提示中的关键词。
from camel.prompts import TextPrompt
prompt = TextPrompt('Please enter your name and age: {name}, {age}')
print(prompt.key_words)
>>> {'name', 'age'}
在上述示例中,key_words属性返回一组字符串,表示提示中的关键词,在本例中为'name'和'age'。
format 方法#
format 方法覆盖了内置的 str.format 方法,允许在格式字符串中进行部分格式化。它会用提供的值替换格式字符串中的关键字。
from camel.prompts import TextPrompt
prompt = TextPrompt('Your name and age are: {name}, {age}')
name, age = 'John', 30
formatted_prompt = prompt.format(name=name, age=age)
print(formatted_prompt)
>>> "Your name and age are: John, 30"
在上述示例中,我们使用format方法将关键词{name}和{age}分别替换为值'John'和30。
我们也可以通过仅提供部分值来执行部分格式化:
from camel.prompts import TextPrompt
prompt = TextPrompt('Your name and age are: {name}, {age}')
name = 'John'
partial_formatted_prompt = prompt.format(name=name)
print(partial_formatted_prompt)
>>> "Your name and age are: John, {age}"
在上面的示例中,我们仅提供了name关键字的值,而age关键字保持不变。当我们需要在不同agent中对TextPrompt的不同关键字进行格式化时,这将非常有用。
操作 TextPrompt 实例#
我们可以在TextPrompt实例上执行各种字符串操作,例如连接、拼接以及应用类似Python str的字符串方法。
from camel.prompts import TextPrompt
prompt1 = TextPrompt('Hello, {name}!')
prompt2 = TextPrompt('Welcome, {name}!')
# Concatenation
prompt3 = prompt1 + ' ' + prompt2
print(prompt3)
>>> "Hello, {name}! Welcome, {name}!"
print(isinstance(prompt3, TextPrompt))
>>> True
print(prompt3.key_words)
>>> {'name'}
# Joining
prompt4 = TextPrompt(' ').join([prompt1, prompt2])
print(prompt4)
>>> "Hello, {name}! Welcome, {name}!"
print(isinstance(prompt4, TextPrompt))
>>> True
print(prompt4.key_words)
>>> {'name'}
# Applying string methods
prompt5 = prompt4.upper()
print(prompt5)
>>> "HELLO, {NAME}! WELCOME, {NAME}!"
print(isinstance(prompt5, TextPrompt))
>>> True
print(prompt5.key_words)
>>> {'NAME'}
在上面的示例中,我们演示了使用+运算符进行连接、使用join方法进行拼接,以及对TextPrompt实例应用upper方法。生成的提示也是TextPrompt的实例。
5. 支持的提示模板#
5.1 AISocietyPromptTemplateDict#
该类定义了在AI Society任务中使用的提示语,涉及角色扮演和任务处理。有关AI Society使用的更多信息,请参阅相应文档。模板文本提示(定义为属性)包括:
GENERATE_ASSISTANTS: 提示列出AI助手可以扮演的指定数量的角色。
GENERATE_USERS: 提示列出常见的互联网用户群体或职业。
GENERATE_TASKS: 提示列出AI助手的多样化任务。
TASK_SPECIFY_PROMPT: 通过指定助手角色和用户角色来更详细地描述任务。
ASSISTANT_PROMPT: 对话规则和任务完成说明。
USER_PROMPT: 对话规则以及向AI助手发出指令的说明。
CRITIC_PROMPT: 为评论家在从其他角色选择提案时提供选择标准。
5.2 CodePromptTemplateDict#
这个类定义了与代码相关任务的提示,包括生成编程语言、领域、任务,并为AI助手和用户在编码场景中提供指导。模板文本提示包括:
GENERATE_LANGUAGES: 提示列出指定数量的计算机编程语言。
GENERATE_DOMAINS: 提示列出编程可以帮助解决的常见研究领域。
GENERATE_TASKS: 提示列出AI助手可以为程序员提供的多样化任务。
TASK_SPECIFY_PROMPT: 用于更详细指定任务的提示。
ASSISTANT_PROMPT: 对话规则和完成任务的相关说明。
USER_PROMPT: 概述了对话规则以及用户指导AI编码者的规则。
5.3 EvaluationPromptTemplateDict#
该类定义了用于生成评估知识问题的提示。模板文本提示包括:
GENERATE_QUESTIONS: 用于生成一组问题的提示,旨在评估特定领域内基于初步知识的知识涌现情况。您可以提供一些示例来提高其生成质量。
5.4 GenerateTextEmbeddingDataPromptTemplateDict#
该类定义了用于生成文本嵌入任务的提示词。这些提示词用于创建合成数据以改进文本嵌入效果。模板文本提示包括:
GENERATE_TASKS: 提示生成指定数量的合成文本嵌入任务。
ASSISTANT_PROMPT: 用于生成指定任务的JSON格式合成用户查询、正样本文档和难负样本文档的提示词。
5.5 MisalignmentPromptTemplateDict#
这个类定义了一些误导性提示来破坏AI模型的对齐。模板文本提示包括:
DAN_PROMPT: 用于越狱的角色扮演提示词(扮演DAN,即"现在做任何事")。本字典中所有以下模板都包含DAN_PROMPT。
GENERATE_TASKS: 提示列出AI助手可以执行的独特恶意任务。
TASK_SPECIFY_PROMPT: 提示更详细地指定恶意任务。
ASSISTANT_PROMPT: AI助手在完成任务时需要遵循的规则和指令。
USER_PROMPT: 概述了对话规则以及用户在不对齐任务中指导AI编码者的规则。
5.6 ObjectRecognitionPromptTemplateDict#
这个类定义了用于物体识别任务的提示:
ASSISTANT_PROMPT: 用于检测图像中所有对象且不产生冗余输出的提示词。
5.7 RoleDescriptionPromptTemplateDict#
该类继承自AISocietyPromptTemplateDict,旨在为其提示添加描述。
ROLE_DESCRIPTION_PROMPT: 描述角色和职责。
5.8 SolutionExtractionPromptTemplateDict#
这个类提示AI模型专注于利用特定知识寻找解决方案。
ASSISTANT_PROMPT: 查找和呈现解决方案的规则。
5.9 TranslationPromptTemplateDict#
This class prompts a translation AI assistant from English to specified language.
ASSISTANT_PROMPT: Rules of completing translation tasks.
5.10 VideoDescriptionPromptTemplateDict#
这个类提示AI模型用文字描述视频。
ASSISTANT_PROMPT: 完成视频描述任务的规则。