File size: 6,749 Bytes
4962437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
Swarms Documentation

====================

Worker Node

-----------

The `WorkerNode` class is a powerful component of the Swarms framework. It is designed to spawn an autonomous agent instance as a worker to accomplish complex tasks. It can search the internet, spawn child multi-modality models to process and generate images, text, audio, and so on.

### WorkerNodeInitializer

The `WorkerNodeInitializer` class is used to initialize a worker node.

#### Initialization

```

WorkerNodeInitializer(openai_api_key: str,

llm: Optional[Union[InMemoryDocstore, ChatOpenAI]] = None,

tools: Optional[List[Tool]] = None,

worker_name: Optional[str] = "Swarm Worker AI Assistant",

worker_role: Optional[str] = "Assistant",

human_in_the_loop: Optional[bool] = False,

search_kwargs: dict = {},

verbose: Optional[bool] = False,

chat_history_file: str = "chat_history.txt")

```

Copy code

##### Parameters

- `openai_api_key` (str): The OpenAI API key.

- `llm` (Union[InMemoryDocstore, ChatOpenAI], optional): The language model to use. Default is `ChatOpenAI`.

- `tools` (List[Tool], optional): The tools to use.

- `worker_name` (str, optional): The name of the worker. Default is "Swarm Worker AI Assistant".

- `worker_role` (str, optional): The role of the worker. Default is "Assistant".

- `human_in_the_loop` (bool, optional): Whether to include a human in the loop. Default is False.

- `search_kwargs` (dict, optional): The keyword arguments for the search.

- `verbose` (bool, optional): Whether to print verbose output. Default is False.

- `chat_history_file` (str, optional): The file to store the chat history. Default is "chat_history.txt".

##### Example

```

from swarms.tools.autogpt import DuckDuckGoSearchRun

worker_node_initializer = WorkerNodeInitializer(openai_api_key="your_openai_api_key",

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="Assistant",

human_in_the_loop=True)

```

Copy code

### WorkerNode

The `WorkerNode` class is used to create a worker node.

#### Initialization

```

WorkerNode(openai_api_key: str,

temperature: int,

llm: Optional[Union[InMemoryDocstore, ChatOpenAI]] = None,

tools: Optional[List[Tool]] = None,

worker_name: Optional[str] = "Swarm Worker AI Assistant",

worker_role: Optional[str] = "Assistant",

human_in_the_loop: Optional[bool] = False,

search_kwargs: dict = {},

verbose: Optional[bool] = False,

chat_history_file: str = "chat_history.txt")

```

Copy code

##### Parameters

- `openai_api_key` (str): The OpenAI API key.

- `temperature` (int): The temperature for the language model.

- `llm` (Union[InMemoryDocstore, ChatOpenAI], optional): The language model to use. Default is `ChatOpenAI`.

- `tools` (List[Tool], optional): The tools to use.

- `worker_name` (str, optional): The name of the worker. Default is "Swarm Worker AI Assistant".

- `worker_role` (str, optional): The role of the worker. Default is "Assistant".

- `human_in_the_loop` (bool, optional): Whether to include a human in the loop. Default is False.

- `search_kwargs` (dict, optional): The keyword arguments for the search.

- `verbose` (bool, optional): Whether to print verbose output. Default is False.

- `chat_history_file` (str, optional): The file to store the chat history. Default is "chat_history.txt".

##### Example

```

worker_node = WorkerNode(openai_api_key="your_openai_api_key",

temperature=0.8,

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="As```

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="Assistant",

human_in_the_loop=True)

# Create a worker node

worker_node = WorkerNode(openai_api_key="your_openai_api_key",

temperature=0.8,

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="Assistant",

human_in_the_loop=True)

# Add a tool to the worker node

worker_node_initializer.add_tool(DuckDuckGoSearchRun())

# Initialize the language model and tools for the worker node

worker_node.initialize_llm(ChatOpenAI, temperature=0.8)

worker_node.initialize_tools(ChatOpenAI)

# Create the worker node

worker_node.create_worker_node(worker_name="My Worker Node",

worker_role="Assistant",

human_in_the_loop=True,

llm_class=ChatOpenAI,

search_kwargs={})

# Run the worker node

`worker_node.run("Hello, world!")`

In this example, we first initialize a `WorkerNodeInitializer` and a `WorkerNode`. We then add a tool to the `WorkerNodeInitializer` and initialize the language model and tools for the `WorkerNode`. Finally, we create the worker node and run it with a given prompt.

This example shows how you can use the `WorkerNode` and `WorkerNodeInitializer` classes to create a worker node, add tools to it, initialize its language model and tools, and run it with a given prompt. The parameters of these classes can be customized to suit your specific needs.

Thanks for becoming an alpha build user, email [email protected] with all complaintssistant",

human_in_the_loop=True)

```

Copy code

### Full Example

Here is a full example of how to use the `WorkerNode` and `WorkerNodeInitializer` classes:

```python

from swarms.tools.autogpt import DuckDuckGoSearchRun

from swarms.worker_node import WorkerNode, WorkerNodeInitializer

# Initialize a worker node

worker_node_initializer = WorkerNodeInitializer(openai_api_key="your_openai_api_key",

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="Assistant",

human_in_the_loop=True)

# Create a worker node

worker_node = WorkerNode(openai_api_key="your_openai_api_key",

temperature=0.8,

tools=[DuckDuckGoSearchRun()],

worker_name="My Worker",

worker_role="Assistant",

human_in_the_loop=True)

# Add a tool to the worker node

worker_node_initializer.add_tool(DuckDuckGoSearchRun())

# Initialize the language model and tools for the worker node

worker_node.initialize_llm(ChatOpenAI, temperature=0.8)

worker_node.initialize_tools(ChatOpenAI)

# Create the worker node

worker_node.create_worker_node(worker_name="My Worker Node",

worker_role="Assistant",

human_in_the_loop=True,

llm_class=ChatOpenAI,

search_kwargs={})

# Run the worker node

worker_node.run("Hello, world!")

```

In this example, we first initialize a `WorkerNodeInitializer` and a `WorkerNode`. We then add a tool to the `WorkerNodeInitializer` and initialize the language model and tools for the `WorkerNode`. Finally, we create the worker node and run it with a given prompt.

This example shows how you can use the `WorkerNode` and `WorkerNodeInitializer` classes to create a worker node, add tools to it, initialize its language model and tools, and run it with a given prompt. The parameters of these classes can be customized to suit your specific needs.