Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Add tutorial for `CodePrompt` (#149)

Guohao Li007dbfa23644
diff --git a/docs/README.md b/docs/README.md
index 2b781d12d..1cbf43152 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -28,16 +28,15 @@ make html
4. Launch the HTML documentation from the terminal using a local HTTP server
```bash
-cd _build
+cd _build/html
python -m http.server
```
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:
```bash
-http://localhost:8000/html
+http://localhost:8000
```
This will load the HTML documentation in your web browser from the local server.
You can navigate through the documentation using the links and interact with it as you would with any other web page.
-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.
-
+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.
\ No newline at end of file
diff --git a/docs/camel.messages.rst b/docs/camel.messages.rst
new file mode 100644
index 000000000..ed2659a0e
--- /dev/null
+++ b/docs/camel.messages.rst
@@ -0,0 +1,37 @@
+camel.messages package
+======================
+
+Submodules
+----------
+
+camel.messages.base module
+--------------------------
+
+.. automodule:: camel.messages.base
+ :members:
+ :undoc-members:
+ :show-inheritance:
+
+camel.messages.chat\_messages module
+------------------------------------
+
+.. automodule:: camel.messages.chat_messages
+ :members:
+ :undoc-members:
+ :show-inheritance:
+
+camel.messages.system\_messages module
+--------------------------------------
+
+.. automodule:: camel.messages.system_messages
+ :members:
+ :undoc-members:
+ :show-inheritance:
+
+Module contents
+---------------
+
+.. automodule:: camel.messages
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/get_started/code_prompt.md b/docs/get_started/code_prompt.md
new file mode 100644
index 000000000..be3acfc15
--- /dev/null
+++ b/docs/get_started/code_prompt.md
@@ -0,0 +1,83 @@
+# Introduction to `CodePrompt` Class
+
+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.
+
+## 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
+```
+
+## 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.
+
+```python
+code_prompt = CodePrompt("print('Hello, World!')", code_type="python")
+```
+
+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.
+
+## 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.
+
+```python
+print(code_prompt)
+>>> "print('Hello, World!')"
+
+print(code_prompt.code_type)
+>>> "python"
+```
+
+## 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.
+
+```python
+code_prompt = CodePrompt("print('Hello, World!')")
+print(code_prompt.code_type)
+>>> None
+
+code_prompt.set_code_type("python")
+print(code_prompt.code_type)
+>>> "python"
+```
+
+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 tuple containing the output string and local variables.
+
+```python
+CodePrompt("a = 1\nprint('Hello, World!')", code_type="python")
+output, variables = code_prompt.execute()
+print(output)
+>>> "Hello, World!\n"
+
+print(variables)
+>>> {"a": 1}
+```
+
+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.
+
+## Handling Execution Errors
+
+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.
+
+```python
+code_prompt = CodePrompt("print('Hello, World!'")
+traceback_str, _ = code_prompt.execute()
+assert "SyntaxError" in traceback_str
+>>> True
+```
+
+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.
+
+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.
\ No newline at end of file
diff --git a/docs/get_started/prompts.md b/docs/get_started/text_prompt.md
similarity index 99%
rename from docs/get_started/prompts.md
rename to docs/get_started/text_prompt.md
index 0a1fa7978..aadc5509a 100644
--- a/docs/get_started/prompts.md
+++ b/docs/get_started/text_prompt.md
@@ -84,28 +84,34 @@ prompt2 = TextPrompt('Welcome, {name}!')
# Concatenation
prompt3 = prompt1 + ' ' + prompt2
print(prompt3)
-print(isinstance(prompt3, TextPrompt))
-print(prompt3.key_words)
>>> "Hello, {name}! Welcome, {name}!"
+
+print(isinstance(prompt3, TextPrompt))
>>> True
+
+print(prompt3.key_words)
>>> {'name'}
# Joining
prompt4 = TextPrompt(' ').join([prompt1, prompt2])
print(prompt4)
-print(isinstance(prompt4, TextPrompt))
-print(prompt4.key_words)
>>> "Hello, {name}! Welcome, {name}!"
+
+print(isinstance(prompt4, TextPrompt))
>>> True
+
+print(prompt4.key_words)
>>> {'name'}
# Applying string methods
prompt5 = prompt4.upper()
print(prompt5)
-print(isinstance(prompt5, TextPrompt))
-print(prompt5.key_words)
>>> "HELLO, {NAME}! WELCOME, {NAME}!"
+
+print(isinstance(prompt5, TextPrompt))
>>> True
+
+print(prompt5.key_words)
>>> {'NAME'}
```
diff --git a/docs/index.rst b/docs/index.rst
index 036c93c21..331f9ed49 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -16,7 +16,9 @@ Welcome to CAMEL's documentation!
:caption: Get Started:
:name: getting_started
- get_started/prompts.md
+ get_started/text_prompt.md
+ get_started/code_prompt.md
+
.. toctree::
:maxdepth: 1
diff --git a/test/prompts/test_prompt_base.py b/test/prompts/test_prompt_base.py
index 0ce16266c..9c722c94a 100644
--- a/test/prompts/test_prompt_base.py
+++ b/test/prompts/test_prompt_base.py
@@ -136,11 +136,12 @@ def test_code_prompt_set_code_type():
def test_code_prompt_execute():
- code_prompt = CodePrompt("a = 1\nprint('Hello, World!')")
+ code_prompt = CodePrompt("a = 1\nprint('Hello, World!')",
+ code_type="python")
assert code_prompt.execute() == ("Hello, World!\n", {"a": 1})
def test_code_prompt_execute_error():
- code_prompt = CodePrompt("print('Hello, World!'")
+ code_prompt = CodePrompt("print('Hello, World!'", code_type="python")
traceback_str, _ = code_prompt.execute()
assert "SyntaxError" in traceback_str