Skip to content

Commit 007dbfa

Browse files
authored
Add tutorial for CodePrompt (camel-ai#149)
1 parent 603ec9b commit 007dbfa

File tree

6 files changed

+141
-13
lines changed

6 files changed

+141
-13
lines changed

docs/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ make html
2828

2929
4. Launch the HTML documentation from the terminal using a local HTTP server
3030
```bash
31-
cd _build
31+
cd _build/html
3232
python -m http.server
3333
```
3434
This command starts a local HTTP server on port 8000 by default. Once the server is running, open your web browser and enter the following URL:
3535
```bash
36-
http://localhost:8000/html
36+
http://localhost:8000
3737
```
3838
This will load the HTML documentation in your web browser from the local server.
3939

4040
You can navigate through the documentation using the links and interact with it as you would with any other web page.
4141

42-
To stop the local server, go back to the terminal or command prompt where it is running and press `Ctrl+C` to terminate the server.
43-
42+
To stop the local server, go back to the terminal or command prompt where it is running and press `Ctrl+C` to terminate the server.

docs/camel.messages.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
camel.messages package
2+
======================
3+
4+
Submodules
5+
----------
6+
7+
camel.messages.base module
8+
--------------------------
9+
10+
.. automodule:: camel.messages.base
11+
:members:
12+
:undoc-members:
13+
:show-inheritance:
14+
15+
camel.messages.chat\_messages module
16+
------------------------------------
17+
18+
.. automodule:: camel.messages.chat_messages
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
23+
camel.messages.system\_messages module
24+
--------------------------------------
25+
26+
.. automodule:: camel.messages.system_messages
27+
:members:
28+
:undoc-members:
29+
:show-inheritance:
30+
31+
Module contents
32+
---------------
33+
34+
.. automodule:: camel.messages
35+
:members:
36+
:undoc-members:
37+
:show-inheritance:

docs/get_started/code_prompt.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Introduction to `CodePrompt` Class
2+
3+
In this tutorial, 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.
4+
5+
## Importing the `CodePrompt` Class
6+
7+
To use the `CodePrompt` class, you need to import it. Here's an example of how to import the class:
8+
9+
```python
10+
from camel.prompts import CodePrompt
11+
```
12+
13+
## Creating a `CodePrompt` Instance
14+
15+
To create a `CodePrompt` instance, you can simply instantiate the class, providing the code string and the code type as an input argument.
16+
17+
```python
18+
code_prompt = CodePrompt("print('Hello, World!')", code_type="python")
19+
```
20+
21+
In this example, we create a `CodePrompt` instance with the code string `"print('Hello, World!')"`. We also specify the code type as `"python"`. The code type can be set to `None` if not needed.
22+
23+
## Accessing the Code and Code Type
24+
25+
Once you have a `CodePrompt` instance, you can access the code string and code type as following:
26+
27+
- `code_prompt`: Accesses the code string of the prompt.
28+
- `code_type`: Accesses the type of code associated with the prompt.
29+
30+
```python
31+
print(code_prompt)
32+
>>> "print('Hello, World!')"
33+
34+
print(code_prompt.code_type)
35+
>>> "python"
36+
```
37+
38+
## Modifying the Code Type
39+
40+
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.
41+
42+
```python
43+
code_prompt = CodePrompt("print('Hello, World!')")
44+
print(code_prompt.code_type)
45+
>>> None
46+
47+
code_prompt.set_code_type("python")
48+
print(code_prompt.code_type)
49+
>>> "python"
50+
```
51+
52+
In this example, we change the code type of the `CodePrompt` instance from `None` to `"python"`.
53+
54+
## Executing the Code
55+
56+
The `CodePrompt` class provides a method called `execute` that allows you to execute the code string associated with the prompt. It returns a tuple containing the output string and local variables.
57+
58+
```python
59+
CodePrompt("a = 1\nprint('Hello, World!')", code_type="python")
60+
output, variables = code_prompt.execute()
61+
print(output)
62+
>>> "Hello, World!\n"
63+
64+
print(variables)
65+
>>> {"a": 1}
66+
```
67+
68+
In this example, we execute the code prompt and store the output string in the `output` variable and the local variables in the `variables` dictionary.
69+
70+
## Handling Execution Errors
71+
72+
If there is an error during the code execution, the `execute` method catches the error and returns the traceback string. If there is no error, the method returns the output string and local variables.
73+
74+
```python
75+
code_prompt = CodePrompt("print('Hello, World!'")
76+
traceback_str, _ = code_prompt.execute()
77+
assert "SyntaxError" in traceback_str
78+
>>> True
79+
```
80+
81+
In this example, the code string has a syntax error where a right bracket `)` is missing, and the `execute` method returns the traceback string indicating the error.
82+
83+
That's it! You have went through the basics of using the `CodePrompt` class. You can now create code prompts, access the code and code type, modify the code type if needed, and execute the code.

docs/get_started/prompts.md renamed to docs/get_started/text_prompt.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,34 @@ prompt2 = TextPrompt('Welcome, {name}!')
8484
# Concatenation
8585
prompt3 = prompt1 + ' ' + prompt2
8686
print(prompt3)
87-
print(isinstance(prompt3, TextPrompt))
88-
print(prompt3.key_words)
8987
>>> "Hello, {name}! Welcome, {name}!"
88+
89+
print(isinstance(prompt3, TextPrompt))
9090
>>> True
91+
92+
print(prompt3.key_words)
9193
>>> {'name'}
9294

9395
# Joining
9496
prompt4 = TextPrompt(' ').join([prompt1, prompt2])
9597
print(prompt4)
96-
print(isinstance(prompt4, TextPrompt))
97-
print(prompt4.key_words)
9898
>>> "Hello, {name}! Welcome, {name}!"
99+
100+
print(isinstance(prompt4, TextPrompt))
99101
>>> True
102+
103+
print(prompt4.key_words)
100104
>>> {'name'}
101105

102106
# Applying string methods
103107
prompt5 = prompt4.upper()
104108
print(prompt5)
105-
print(isinstance(prompt5, TextPrompt))
106-
print(prompt5.key_words)
107109
>>> "HELLO, {NAME}! WELCOME, {NAME}!"
110+
111+
print(isinstance(prompt5, TextPrompt))
108112
>>> True
113+
114+
print(prompt5.key_words)
109115
>>> {'NAME'}
110116
```
111117

docs/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Welcome to CAMEL's documentation!
1616
:caption: Get Started:
1717
:name: getting_started
1818

19-
get_started/prompts.md
19+
get_started/text_prompt.md
20+
get_started/code_prompt.md
21+
2022

2123
.. toctree::
2224
:maxdepth: 1

test/prompts/test_prompt_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ def test_code_prompt_set_code_type():
136136

137137

138138
def test_code_prompt_execute():
139-
code_prompt = CodePrompt("a = 1\nprint('Hello, World!')")
139+
code_prompt = CodePrompt("a = 1\nprint('Hello, World!')",
140+
code_type="python")
140141
assert code_prompt.execute() == ("Hello, World!\n", {"a": 1})
141142

142143

143144
def test_code_prompt_execute_error():
144-
code_prompt = CodePrompt("print('Hello, World!'")
145+
code_prompt = CodePrompt("print('Hello, World!'", code_type="python")
145146
traceback_str, _ = code_prompt.execute()
146147
assert "SyntaxError" in traceback_str

0 commit comments

Comments
 (0)