Language Agent
Run a LanguageAgent with memory, optional remote acting, and optional automatic dataset creation capabilities.
While it is always recommended to explicitly define your observation and action spaces, which can be set with a gym.Space object or any python object using the Sample class (see examples/using_sample.py for a tutorial), you can have the recorder infer the spaces by setting recorder="default" for automatic dataset recording.
Examples:
>>> agent = LanguageAgent(context=SYSTEM_PROMPT, model_src=backend, recorder="default")
>>> agent.act_and_record("pick up the fork", image)
Alternatively, you can define the recorder separately to record the space you want. For example, to record the dataset with the image and instruction observation and AnswerAndActionsList as action.
Examples:
>>> observation_space = spaces.Dict({"image": Image(size=(224, 224)).space(), "instruction": spaces.Text(1000)})
>>> action_space = AnswerAndActionsList(actions=[HandControl()] * 6).space()
>>> recorder = Recorder(
... 'example_recorder',
... out_dir='saved_datasets',
... observation_space=observation_space,
... action_space=action_space
To record the dataset, you can use the record method of the recorder object.
Examples:
>>> recorder.record(
... observation={
... "image": image,
... "instruction": instruction,
... },
... action=answer_actions,
... )
LanguageAgent
Bases: Agent
An agent that can interact with users using natural language.
This class extends the functionality of a base Agent to handle natural language interactions. It manages memory, dataset-recording, and asynchronous remote inference, supporting multiple platforms including OpenAI, Anthropic, and Gradio.
Attributes:
Name | Type | Description |
---|---|---|
reminders |
List[Reminder]
|
A list of reminders that prompt the agent every n messages. |
context |
List[Message]
|
The current context of the conversation. |
Examples:
Basic usage with OpenAI: >>> cognitive_agent = LanguageAgent(api_key="...", model_src="openai", recorder="default") >>> cognitive_agent.act("your instruction", image)
Automatically act and record to dataset: >>> cognitive_agent.act_and_record("your instruction", image)
Stream the response: >>> for chunk in cognitive_agent.act_and_stream("your instruction", image): ... print(chunk)
To use vLLM:
agent = LanguageAgent(
context=context,
model_src="openai",
model_kwargs={"api_key": "EMPTY", "base_url": "http://1.2.3.4:1234/v1"},
)
response = agent.act("Hello, how are you?", model="mistralai/Mistral-7B-Instruct-v0.3")
To use ollama:
agent = LanguageAgent(
context="You are a robot agent.",
model_src="ollama",
model_kwargs={"endpoint": "http://localhost:11434/api/chat"},
)
response = agent.act("Hello, how are you?", model="llama3.1")
Source code in mbodied/agents/language/language_agent.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
__init__(model_src='openai', context=None, api_key=os.getenv('OPENAI_API_KEY'), model_kwargs=None, recorder='omit', recorder_kwargs=None)
Agent with memory, asynchronous remote acting, and automatic dataset recording.
Additionally supports asynchronous remote inference, supporting multiple platforms including OpenAI, Anthropic, vLLM, Gradio, and Ollama.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_src
|
Literal['openai', 'anthropic', 'gradio', 'ollama', 'http'] | AnyUrl | FilePath | DirectoryPath | NewPath
|
The source of the model to use for inference. It can be one of the following: - "openai": Use the OpenAI backend (or vLLM). - "anthropic": Use the Anthropic backend. - "gradio": Use the Gradio backend. - "ollama": Use the Ollama backend. - "http": Use a custom HTTP API backend. - AnyUrl: A URL pointing to the model source. - FilePath: A local path to the model's weights. - DirectoryPath: A local directory containing the model's weights. - NewPath: A new path object representing the model source. |
'openai'
|
context
|
Union[list, Image, str, Message]
|
The starting context to use for the conversation. It can be a list of messages, an image, a string, or a message. If a string is provided, it will be interpreted as a user message. Defaults to None. |
None
|
api_key
|
str
|
The API key to use for the remote actor (if applicable). Defaults to the value of the OPENAI_API_KEY environment variable. |
getenv('OPENAI_API_KEY')
|
model_kwargs
|
dict
|
Additional keyword arguments to pass to the model source. Can be overidden at act time. See the documentation of the specific backend for more details. Defaults to None. |
None
|
recorder
|
Union[str, Literal['default', 'omit']]
|
The recorder configuration or name or action. Defaults to "omit". |
'omit'
|
recorder_kwargs
|
dict
|
Additional keyword arguments to pass to the recorder. Defaults to None. |
None
|
Source code in mbodied/agents/language/language_agent.py
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 |
|
act(instruction, image=None, context=None, model=None, **kwargs)
Responds to the given instruction, image, and context.
Uses the given instruction and image to perform an action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instruction
|
str
|
The instruction to be processed. |
required |
image
|
Image
|
The image to be processed. |
None
|
context
|
list | str | Image | Message
|
Additonal context to include in the response. If context is a list of messages, it will be interpreted as new memory. |
None
|
model
|
The model to use for the response. |
None
|
|
**kwargs
|
Additional keyword arguments. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The response to the instruction. |
Examples:
>>> agent.act("Hello, world!", Image("scene.jpeg"))
"Hello! What can I do for you today?"
>>> agent.act("Return a plan to pickup the object as a python list.", Image("scene.jpeg"))
"['Move left arm to the object', 'Move right arm to the object']"
Source code in mbodied/agents/language/language_agent.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
act_and_parse(instruction, image=None, parse_target=Sample, context=None, model=None, max_retries=1, record=False, **kwargs)
Responds to the given instruction, image, and context and parses the response into a Sample object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instruction
|
str
|
The instruction to be processed. |
required |
image
|
Image
|
The image to be processed. |
None
|
parse_target
|
type[Sample]
|
The target type to parse the response into. |
Sample
|
context
|
list | str | Image | Message
|
Additonal context to include in the response. If context is a list of messages, it will be interpreted as new memory. |
None
|
model
|
The model to use for the response. |
None
|
|
max_retries
|
int
|
The maximum number of retries to parse the response. |
1
|
record
|
bool
|
Whether to record the interaction for training. |
False
|
**kwargs
|
Additional keyword arguments. |
{}
|
Source code in mbodied/agents/language/language_agent.py
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 286 287 288 289 290 291 292 293 294 295 296 297 |
|
act_and_stream(instruction, image=None, context=None, model=None, **kwargs)
Responds to the given instruction, image, and context and streams the response.
Source code in mbodied/agents/language/language_agent.py
389 390 391 392 393 394 395 396 397 398 399 400 |
|
async_act_and_parse(instruction, image=None, parse_target=Sample, context=None, model=None, max_retries=1, **kwargs)
async
Responds to the given instruction, image, and context asynchronously and parses the response into a Sample object.
Source code in mbodied/agents/language/language_agent.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
forget(everything=False, last_n=-1)
Forget the last n messages in the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
everything
|
Whether to forget everything. |
False
|
|
last_n
|
int
|
The number of messages to forget. |
-1
|
Source code in mbodied/agents/language/language_agent.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
forget_after(first_n)
Forget after the first n messages in the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
first_n
|
int
|
The number of messages to keep. |
required |
Source code in mbodied/agents/language/language_agent.py
209 210 211 212 213 214 215 |
|
forget_last()
Forget the last message in the context.
Source code in mbodied/agents/language/language_agent.py
202 203 204 205 206 207 |
|
history()
Return the conversation history.
Source code in mbodied/agents/language/language_agent.py
235 236 237 |
|
postprocess_response(response, message, memory, **kwargs)
Postprocess the response.
Source code in mbodied/agents/language/language_agent.py
348 349 350 351 352 |
|
prepare_inputs(instruction, image=None, context=None)
Helper method to prepare the inputs for the agent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instruction
|
str
|
The instruction to be processed. |
required |
image
|
Image
|
The image to be processed. |
None
|
context
|
list | str | Image | Message
|
Additonal context to include in the response. If context is a list of messages, it will be interpreted as new memory. |
None
|
Source code in mbodied/agents/language/language_agent.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
remind_every(prompt, n)
Remind the agent of the prompt every n messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str | Image | Message
|
The prompt to remind the agent of. |
required |
n
|
int
|
The frequency of the reminder. |
required |
Source code in mbodied/agents/language/language_agent.py
239 240 241 242 243 244 245 246 247 |
|
Reminder
dataclass
A reminder to show the agent a prompt every n messages.
Source code in mbodied/agents/language/language_agent.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
make_context_list(context)
Convert the context to a list of messages.
Source code in mbodied/agents/language/language_agent.py
88 89 90 91 92 93 94 95 96 |
|