-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtest_agent.py
285 lines (218 loc) · 9.83 KB
/
test_agent.py
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
276
277
278
279
280
281
282
283
284
285
"""
Example implementation of a GAME SDK agent with multiple workers.
This example demonstrates how to:
1. Create and configure workers with different capabilities
2. Manage state across workers
3. Define and register custom functions
4. Handle function results and state updates
The example implements a simple environment with objects that workers can interact with
through various actions like taking, throwing, and sitting.
"""
from game_sdk.game.agent import Agent, WorkerConfig
from game_sdk.game.custom_types import Function, Argument, FunctionResult, FunctionResultStatus
from typing import Tuple
import os
game_api_key = os.environ.get("GAME_API_KEY")
def get_worker_state_fn(function_result: FunctionResult, current_state: dict) -> dict:
"""
State management function for workers in the example environment.
This function demonstrates how to maintain and update worker state based on
function execution results. It shows both static state management and
dynamic state updates.
Args:
function_result (FunctionResult): Result from the previous function execution.
current_state (dict): Current state of the worker.
Returns:
dict: Updated state containing available objects and their properties.
Note:
This example uses a fixed state for simplicity, but you can implement
dynamic state updates based on function_result.info.
"""
# Dict containing info about the function result as implemented in the executable
info = function_result.info
# Example of fixed state - the first state placed here is the initial state
init_state = {
"objects": [
{"name": "apple", "description": "A red apple", "type": ["item", "food"]},
{"name": "banana", "description": "A yellow banana", "type": ["item", "food"]},
{"name": "orange", "description": "A juicy orange", "type": ["item", "food"]},
{"name": "chair", "description": "A chair", "type": ["sittable"]},
{"name": "table", "description": "A table", "type": ["sittable"]},
]
}
if current_state is None:
# at the first step, initialise the state with just the init state
new_state = init_state
else:
# do something wiht the current state input and the function result info
new_state = init_state # this is just an example where the state is static
return new_state
def get_agent_state_fn(function_result: FunctionResult, current_state: dict) -> dict:
"""
State management function for the main agent.
Maintains the high-level state of the agent, which can be different from
or aggregate the states of individual workers.
Args:
function_result (FunctionResult): Result from the previous function execution.
current_state (dict): Current state of the agent.
Returns:
dict: Updated agent state.
"""
# example of fixed state (function result info is not used to change state) - the first state placed here is the initial state
init_state = {
"objects": [
{"name": "apple", "description": "A red apple", "type": ["item", "food"]},
{"name": "banana", "description": "A yellow banana", "type": ["item", "food"]},
{"name": "orange", "description": "A juicy orange", "type": ["item", "food"]},
{"name": "chair", "description": "A chair", "type": ["sittable"]},
{"name": "table", "description": "A table", "type": ["sittable"]},
]
}
if current_state is None:
# at the first step, initialise the state with just the init state
new_state = init_state
else:
# do something wiht the current state input and the function result info
new_state = init_state # this is just an example where the state is static
return new_state
def take_object(object: str, **kwargs) -> Tuple[FunctionResultStatus, str, dict]:
"""
Function to take an object from the environment.
Args:
object (str): Name of the object to take.
**kwargs: Additional arguments that might be passed.
Returns:
Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info.
Example:
status, msg, info = take_object("apple")
"""
if object:
return FunctionResultStatus.DONE, f"Successfully took the {object}", {}
return FunctionResultStatus.FAILED, "No object specified", {}
def throw_object(object: str, **kwargs) -> Tuple[FunctionResultStatus, str, dict]:
"""
Function to throw an object.
Args:
object (str): Name of the object to throw.
**kwargs: Additional arguments that might be passed.
Returns:
Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info.
Example:
status, msg, info = throw_object("ball")
"""
if object:
return FunctionResultStatus.DONE, f"Successfully threw the {object}", {}
return FunctionResultStatus.FAILED, "No object specified", {}
def sit_on_object(object: str, **kwargs) -> Tuple[FunctionResultStatus, str, dict]:
"""
Function to sit on an object.
Args:
object (str): Name of the object to sit on.
**kwargs: Additional arguments that might be passed.
Returns:
Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info.
Raises:
ValueError: If the object is not sittable.
Example:
status, msg, info = sit_on_object("chair")
"""
sittable_objects = {"chair", "bench", "stool", "couch", "sofa", "bed"}
if not object:
return FunctionResultStatus.FAILED, "No object specified", {}
if object.lower() in sittable_objects:
return FunctionResultStatus.DONE, f"Successfully sat on the {object}", {}
return FunctionResultStatus.FAILED, f"Cannot sit on {object} - not a sittable object", {}
def throw_fruit(object: str, **kwargs) -> Tuple[FunctionResultStatus, str, dict]:
"""
Specialized function to throw fruit objects.
This function demonstrates type-specific actions in the environment.
Args:
object (str): Name of the fruit to throw.
**kwargs: Additional arguments that might be passed.
Returns:
Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info.
Example:
status, msg, info = throw_fruit("apple")
"""
fruits = {"apple", "banana", "orange", "pear", "mango", "grape"}
if not object:
return FunctionResultStatus.ERROR, "No fruit specified", {}
if object.lower() in fruits:
return FunctionResultStatus.DONE, f"Successfully threw the {object} across the room!", {}
return FunctionResultStatus.ERROR, f"Cannot throw {object} - not a fruit", {}
def throw_furniture(object: str, **kwargs) -> Tuple[FunctionResultStatus, str, dict]:
"""
Specialized function to throw furniture objects.
This function demonstrates type-specific actions in the environment.
Args:
object (str): Name of the furniture to throw.
**kwargs: Additional arguments that might be passed.
Returns:
Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info.
Example:
status, msg, info = throw_furniture("chair")
"""
furniture = {"chair", "table", "stool", "lamp", "vase", "cushion"}
if not object:
return FunctionResultStatus.ERROR, "No furniture specified", {}
if object.lower() in furniture:
return FunctionResultStatus.DONE, f"Powerfully threw the {object} across the room!", {}
return FunctionResultStatus.ERROR, f"Cannot throw {object} - not a furniture item", {}
# Create functions for each executable with detailed argument specifications
take_object_fn = Function(
fn_name="take",
fn_description="Take object",
args=[Argument(name="object", type="item", description="Object to take")],
executable=take_object
)
sit_on_object_fn = Function(
fn_name="sit",
fn_description="Sit on object",
args=[Argument(name="object", type="sittable", description="Object to sit on")],
executable=sit_on_object
)
throw_object_fn = Function(
fn_name="throw",
fn_description="Throw any object",
args=[Argument(name="object", type="item", description="Object to throw")],
executable=throw_object
)
throw_fruit_fn = Function(
fn_name="throw_fruit",
fn_description="Throw fruit only",
args=[Argument(name="object", type="item", description="Fruit to throw")],
executable=throw_fruit
)
throw_furniture_fn = Function(
fn_name="throw_furniture",
fn_description="Throw furniture only",
args=[Argument(name="object", type="item", description="Furniture to throw")],
executable=throw_furniture
)
# Create the specialized workers
fruit_thrower = WorkerConfig(
id="fruit_thrower",
worker_description="A worker specialized in throwing fruits ONLY with precision",
get_state_fn=get_worker_state_fn,
action_space=[take_object_fn, sit_on_object_fn, throw_fruit_fn]
)
furniture_thrower = WorkerConfig(
id="furniture_thrower",
worker_description="A strong worker specialized in throwing furniture",
get_state_fn=get_worker_state_fn,
action_space=[take_object_fn, sit_on_object_fn, throw_furniture_fn]
)
# Create agent with both workers
chaos_agent = Agent(
api_key=game_api_key,
name="Chaos",
agent_goal="Conquer the world by causing chaos.",
agent_description="You are a mischievous master of chaos is very strong but with a very short attention span, and not so much brains",
get_agent_state_fn=get_agent_state_fn,
workers=[fruit_thrower, furniture_thrower]
)
# # interact and instruct the worker to do something
# chaos_agent.get_worker("fruit_thrower").run("make a mess and rest!")
# # compile and run the agent - if you don't compile the agent, the things you added to the agent will not be saved
chaos_agent.compile()
chaos_agent.run()