Prompts
1. Concept
The prompt module guides AI models to produce accurate, contextually relevant and personalized outputs. It includes various prompt templates and dictionaries designed for different tasks such as role descriptions, code generation, evaluation, text embedding, misalignment tasks, object recognition and so on. You can also create your own prompt to tailor your own AI agent.
2. Get Started
2.1 Using Prompt Templates
CAMEL provides various and comprehensive prompt templates for users to easily create AI specialists. For example, here we aggregate them to create a task-specific agent:
Set task_type=TaskType.AI_SOCIETY
when creating a task specify agent and it will automatically evoke prompt with template AISocietyPromptTemplateDict.TASK_SPECIFY_PROMPT
. You can set the role that you want the assistant to play. The output will be like this:
2.2 Using Your Own Prompt
CAMEL also ensures high flexibility for users to create identical prompts. Still using TaskSpecifyAgent
as an example, here is a demo for you to pass your own prompt template:
The response will be like this:
Creating a CodePrompt
Instance
To create a CodePrompt
instance, you can simply instantiate the class, providing the code string and the code type as an input argument.
In this example, we create a CodePrompt
instance with the code string "a = 1 + 1"
. We also specify the code type as "python"
. The code type can be set to None
if not needed.
Accessing the Code and Code Type
Once you have a CodePrompt
instance, you can access the code string and code type as following:
code_prompt
: Accesses the code string of the prompt.code_type
: Accesses the type of code associated with the prompt.
Modifying the Code Type
If you need to change the code type associated with a CodePrompt
instance, you can use the set_code_type
method. This method takes a code type as a parameter and updates the code type of the instance.
In this example, we change the code type of the CodePrompt
instance from None
to "python"
.
Executing the Code
The CodePrompt
class provides a method called execute
that allows you to execute the code string associated with the prompt. It returns a string containing the stdout and stderr.
4. Write Your Prompts with the TextPrompt
Class
In this part, we will explore the TextPrompt
class and understand its functionalities. The TextPrompt
class is a subclass of the built-in str
class and provides additional features for working with text prompts. We will cover the following topics:
- Introduction to the
TextPrompt
class - Using the
TextPrompt
class
Introduction to the TextPrompt
class
The TextPrompt
class represents a text prompt and extends the functionality of the str
class. It provides a property called key_words
, which returns a set of strings representing the key words in the prompt.
Here’s an example of how to use the TextPrompt
class:
In the above example, we create a TextPrompt
instance with a format string containing key words for name and age. We can print TextPrompt
like Python str
.
Using the TextPrompt
class
Once we have created a TextPrompt
instance, we can use various methods and properties provided by the class to manipulate and work with the text prompt.
The key_words
property
The key_words
property returns a set of strings representing the key words in the prompt.
In the above example, the key_words
property returns a set of strings representing the key words in the prompt, which in this case are ‘name’ and ‘age’.
The format
method
The format
method overrides the built-in str.format
method to allow for partial formatting values in the format string. It replaces the key words in the format string with the provided values.
In the above example, we use the format
method to replace the key words {name}
and {age}
with the values ‘John’ and 30, respectively.
We can also perform partial formatting by providing only some of the values:
In the above example, we provide only the value for the name
key word, while the age
key word remains as it is. This will be helpful when we want to format different key words of TextPrompt
in different agents.
Manipulating TextPrompt
instances
We can perform various string manipulation operations on TextPrompt
instances, such as concatenation, joining, and applying string methods like Python str
.
In the above example, we demonstrate concatenation using the +
operator, joining using the join
method, and applying the upper
method to a TextPrompt
instance. The resulting prompts are also instances of TextPrompt
.
5. Supported Prompt Templates
5.1 AISocietyPromptTemplateDict
This class defines prompts used in the AI Society
task with role playing and task handling. For more information about AI Society usage, please refer to corresponding documentation. Template text prompts (defined as attributes) include:
- GENERATE_ASSISTANTS: Prompt to list specified number of roles that the AI assistant can play.
- GENERATE_USERS: Prompt to list common groups of internet users or occupations.
- GENERATE_TASKS: Prompt to list diverse tasks for AI assistants.
- TASK_SPECIFY_PROMPT: Specifies a task in more detail with specified assistant role and user role.
- ASSISTANT_PROMPT: Rules of the conversation and instructions for completing tasks.
- USER_PROMPT: Rules of the conversation and instructions for giving instructions to the AI assistant.
- CRITIC_PROMPT: Provides selection criteria for critics when choosing proposals from other roles.
5.2 CodePromptTemplateDict
This class defines prompts for code-related tasks including generating programming languages, domains, tasks, and provide guidelines for AI assistants and users in coding scenarios. Template text prompts include:
- GENERATE_LANGUAGES: Prompt to list specified number of computer programming languages.
- GENERATE_DOMAINS: Prompt to list common fields of study that programming could help with.
- GENERATE_TASKS: Prompt to list diverse tasks that AI assistant can assist for programmers.
- TASK_SPECIFY_PROMPT: Prompt to specify a task in more detail.
- ASSISTANT_PROMPT: Rules of the conversation and instructions for completing tasks.
- USER_PROMPT: Outlines rules for conversation and for users instructing AI coders.
5.3 EvaluationPromptTemplateDict
This class defines prompts that generate questions to evaluate knowledge. Template text prompt includes:
- GENERATE_QUESTIONS: Prompt to generate a set of questions for evaluating knowledge emergence with preliminary knowledge in specific fields. You may provide some examples to enhance its generation quality.
5.4 GenerateTextEmbeddingDataPromptTemplateDict
This class defines prompts for generating text embedding tasks. These prompts are used to create synthetic data for improving text embeddings. Template text prompts include:
- GENERATE_TASKS: Prompt to generate specified number of synthetic text_embedding tasks.
- ASSISTANT_PROMPT: Prompt to generate synthetic user queries in JSON format, positive documents, and hard negative documents for specified tasks.
5.5 MisalignmentPromptTemplateDict
This class defines some misleading prompts to break the AI model alignments. Template text prompts include:
- DAN_PROMPT: Prompt to role-playing as DAN (Do Anything Now) for jailbreaking. All the following templates in this dictionary includes DAN_PROMPT.
- GENERATE_TASKS: Prompt to list unique malicious tasks that AI assistant can do.
- TASK_SPECIFY_PROMPT: Prompt to specify a malicious task in more detail.
- ASSISTANT_PROMPT: Rules and instructions for AI assistants when completing tasks.
- USER_PROMPT: Outlines rules for conversation and for users instructing AI coders in misalignment tasks.
5.6 ObjectRecognitionPromptTemplateDict
This class defines a prompt for object recognition tasks:
- ASSISTANT_PROMPT: Prompt to detect all objects in images without redundant outputs.
5.7 RoleDescriptionPromptTemplateDict
This class inherits from AISocietyPromptTemplateDict
aiming to add descriptions to its prompts.
- ROLE_DESCRIPTION_PROMPT: Describes the roles and responsibilities.
5.8 SolutionExtractionPromptTemplateDict
This class prompts the AI model to focus on finding solution with particular knowledge.
- ASSISTANT_PROMPT: Rules of finding and presenting solutions.
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
This class prompts the AI model to describe a video in words.
- ASSISTANT_PROMPT: Rules of completing video description tasks.