What is the Prompt Module?

The prompt module in CAMEL guides AI models to produce accurate, relevant, and personalized outputs. It provides a library of templates and dictionaries for diverse tasks — like role description, code generation, evaluation, embeddings, and even object recognition.

You can also craft your own prompts to precisely shape your agent’s behavior.

Using Prompt Templates

CAMEL provides many ready-to-use prompt templates for quickly spinning up task-specific agents.

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")

Set task_type=TaskType.AI_SOCIETY to use the default society prompt template, or define your own.

Using Your Own Prompt

Create and pass your own prompt template with full flexibility:

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.'
)
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)

Introduction to the CodePrompt Class

The CodePrompt class represents a code prompt and extends TextPrompt. It’s perfect for code generation and execution tasks.

from camel.prompts import CodePrompt

Creating a CodePrompt

code_prompt = CodePrompt("a = 1 + 1", code_type="python")

Accessing and Modifying the Code and Type

print(code_prompt)          # >>> "a = 1 + 1"
print(code_prompt.code_type)  # >>> "python"

code_prompt.set_code_type("python")
print(code_prompt.code_type)  # >>> "python"

Executing the Code

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

Write Your Prompts with the TextPrompt Class

The TextPrompt class is a subclass of Python’s str, with extra features for managing key words and advanced formatting.

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}'

The key_words Property

from camel.prompts import TextPrompt

prompt = TextPrompt('Please enter your name and age: {name}, {age}')
print(prompt.key_words)
# >>> {'name', 'age'}

The format Method (Partial Formatting Supported)

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"

# Partial formatting
partial_formatted_prompt = prompt.format(name=name)
print(partial_formatted_prompt)
# >>> "Your name and age are: John, {age}"

Manipulating TextPrompt Instances

You can concatenate, join, and use string methods with TextPrompt just like Python strings:

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'}

# String methods
prompt5 = prompt4.upper()
print(prompt5)
# >>> "HELLO, {NAME}! WELCOME, {NAME}!"
print(isinstance(prompt5, TextPrompt))  # >>> True
print(prompt5.key_words)                # >>> {'NAME'}

Supported Prompt Templates