Skip to content

Commit 9f589bb

Browse files
authored
chore: update workforce doc and openai o1 example (#1133)
1 parent 207db81 commit 9f589bb

File tree

2 files changed

+147
-18
lines changed

2 files changed

+147
-18
lines changed

docs/key_modules/workforce.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ For more detailed usage information, please refer to our cookbook: [Create A Hac
1212

1313
### Architecture
1414

15-
Workforce follows a hierarchical architecture. A workforce can consist of
16-
multiple worker nodes, and each of the worker nodes will contain an agent
17-
as the worker. The worker nodes are managed by a coordinator agent inside the
18-
workforce, and the coordinator agent will assign tasks to the worker nodes
19-
according to the description of the worker nodes, along with their tool sets.
15+
Workforce follows a hierarchical architecture. A workforce can consist of
16+
multiple worker nodes, and each of the worker nodes will contain
17+
one agent or multiple agents as the worker. The worker nodes are managed by
18+
a coordinator agent inside the workforce, and the coordinator agent will assign
19+
tasks to the worker nodes according to the description of the worker nodes,
20+
along with their tool sets.
2021

2122
Alongside the coordinator agent, there is also a task planner agent inside the
2223
workforce. The task planner agent will take the responsibility of decomposing

examples/models/openai_o1_example.py

Lines changed: 141 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from camel.agents import ChatAgent
1616
from camel.configs import ChatGPTConfig
17-
from camel.messages import BaseMessage
1817
from camel.models import ModelFactory
1918
from camel.types import ModelPlatformType, ModelType
2019

@@ -24,22 +23,151 @@
2423
model_config_dict=ChatGPTConfig(temperature=0.0).as_dict(),
2524
)
2625

27-
# Define system message
28-
sys_msg = BaseMessage.make_assistant_message(
29-
role_name="Assistant",
30-
content="You are a helpful assistant.",
31-
)
32-
3326
# Set agent
34-
camel_agent = ChatAgent(system_message=sys_msg, model=o1_model)
27+
camel_agent = ChatAgent(model=o1_model)
3528

36-
user_msg = BaseMessage.make_user_message(
37-
role_name="User",
38-
content="""Write a bash script that takes a matrix represented as a string
29+
# Set user message
30+
user_msg = """Write a bash script that takes a matrix represented as a string
3931
with format '[1,2],[3,4],[5,6]' and prints the transpose in the same
40-
format.""",
41-
)
32+
format."""
4233

4334
# Get response information
4435
response = camel_agent.step(user_msg)
4536
print(response.msgs[0].content)
37+
'''
38+
===============================================================================
39+
Here's a bash script that transposes a matrix represented as the string format
40+
specified. It handles matrices of various sizes, including those with varying
41+
numbers of columns.
42+
43+
```bash
44+
#!/bin/bash
45+
46+
# Read input string from argument or stdin
47+
if [ -n "$1" ]; then
48+
input="$1"
49+
else
50+
read input
51+
fi
52+
53+
# Preprocess the input string to facilitate parsing
54+
# Replace '],[' with '];[' to use ';' as row separator
55+
input="${input//],[/];[}"
56+
57+
# Remove leading and trailing square brackets if any
58+
input="${input#[}"
59+
input="${input%]}"
60+
61+
# Split the input into rows
62+
IFS=';' read -ra rows <<< "$input"
63+
64+
declare -A matrix
65+
66+
nrows=${#rows[@]}
67+
ncols=0
68+
69+
# Parse each row
70+
for ((i=0; i<nrows; i++)); do
71+
row="${rows[i]}"
72+
# Remove leading '[' and trailing ']'
73+
row="${row#[}"
74+
row="${row%]}"
75+
# Split row into elements
76+
IFS=',' read -ra elems <<< "$row"
77+
num_elems=${#elems[@]}
78+
if (( num_elems > ncols )); then
79+
ncols=$num_elems
80+
fi
81+
# Store elements in matrix associative array
82+
for ((j=0; j<num_elems; j++)); do
83+
matrix[$i,$j]="${elems[j]}"
84+
done
85+
done
86+
87+
# Function to join array elements with a delimiter
88+
join_by() {
89+
local d=$1; shift
90+
if [ "$#" -gt 0 ]; then
91+
printf %s "$1" "${@/#/$d}"
92+
fi
93+
}
94+
95+
# Now, build the transposed matrix
96+
transposed_rows=()
97+
for ((j=0; j<ncols; j++)); do
98+
tr_row_elements=()
99+
for ((i=0; i<nrows; i++)); do
100+
e="${matrix[$i,$j]:-}" # Use empty string if element is missing
101+
tr_row_elements+=("$e")
102+
done
103+
tr_row_elements_str=$(join_by ',' "${tr_row_elements[@]}")
104+
tr_row="[${tr_row_elements_str}]"
105+
transposed_rows+=("$tr_row")
106+
done
107+
108+
# Build output string
109+
output=$(join_by ',' "${transposed_rows[@]}")
110+
111+
# Print the output
112+
echo "$output"
113+
```
114+
115+
**Usage:**
116+
117+
Save the script to a file, for example, `transpose_matrix.sh`, and make it
118+
executable:
119+
120+
```bash
121+
chmod +x transpose_matrix.sh
122+
```
123+
124+
You can run the script by passing the matrix string as an argument:
125+
126+
```bash
127+
./transpose_matrix.sh '[1,2],[3,4],[5,6]'
128+
```
129+
130+
Output:
131+
132+
```
133+
[1,3,5],[2,4,6]
134+
```
135+
136+
**Explanation:**
137+
138+
The script performs the following steps:
139+
140+
1. **Input Preprocessing:**
141+
- Replaces `],[` with `];[` to use `;` as a separator between rows.
142+
- Removes any leading or trailing square brackets.
143+
144+
2. **Parsing the Input into a Matrix:**
145+
- Splits the input string into rows using `IFS`.
146+
- For each row:
147+
- Removes the leading `[` and trailing `]`.
148+
- Splits the row into its elements.
149+
- Stores the elements into an associative array `matrix` with keys as
150+
`row,column`.
151+
152+
3. **Determining the Matrix Dimensions:**
153+
- Counts the number of rows (`nrows`).
154+
- Determines the maximum number of columns (`ncols`) across all rows.
155+
156+
4. **Transposing the Matrix:**
157+
- Iterates over each column index.
158+
- For each column, collects the elements from each row at that column index.
159+
- Builds the transposed row string and adds it to `transposed_rows`.
160+
161+
5. **Generating the Output:**
162+
- Joins the transposed rows using commas to form the final output string.
163+
- Prints the output.
164+
165+
**Notes:**
166+
167+
- The script supports matrices where rows have different numbers of columns.
168+
- Missing elements in the matrix (due to irregular column sizes) are handled
169+
by inserting empty strings in the transposed matrix.
170+
- The `join_by` function is used to handle joining array elements with a
171+
specified delimiter, ensuring proper formatting.
172+
===============================================================================
173+
'''

0 commit comments

Comments
 (0)