Introspective
IntrospectiveAgentWorker #
Bases: BaseAgentWorker
Introspective Agent Worker.
This agent worker implements the Reflection AI agentic pattern. It does so by merely delegating the work to two other agents in a purely deterministic fashion.
The task this agent performs (again via delegation) is to generate a response
to a query and perform reflection and correction on the response. This
agent delegates the task to (optionally) first a main_agent_worker
that
generates the initial response to the query. This initial response is then
passed to the reflective_agent_worker
to perform the reflection and
correction of the initial response. Optionally, the main_agent_worker
can be skipped if none is provided. In this case, the users input query
will be assumed to contain the original response that needs to go thru
reflection and correction.
Attributes:
Name | Type | Description |
---|---|---|
reflective_agent_worker |
BaseAgentWorker
|
Reflective agent responsible for performing reflection and correction of the initial response. |
main_agent_worker |
Optional[BaseAgentWorker]
|
Main agent responsible for generating an initial response to the user query. Defaults to None. If None, the user input is assumed as the initial response. |
verbose |
bool
|
Whether execution should be verbose. Defaults to False. |
callback_manager |
Optional[CallbackManager]
|
Callback manager. Defaults to None. |
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
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 |
|
from_defaults
classmethod
#
from_defaults(reflective_agent_worker: BaseAgentWorker, main_agent_worker: Optional[BaseAgentWorker] = None, verbose: bool = False, callback_manager: Optional[CallbackManager] = None, **kwargs: Any) -> IntrospectiveAgentWorker
Create an IntrospectiveAgentWorker from args.
Similar to from_defaults
in other classes, this method will
infer defaults for a variety of parameters, including the LLM,
if they are not specified.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
initialize_step #
Initialize step from task.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
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 |
|
run_step #
run_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
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 |
|
arun_step
async
#
arun_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
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 |
|
stream_step #
stream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
214 215 216 217 |
|
astream_step
async
#
astream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
219 220 221 222 223 224 |
|
finalize_task #
finalize_task(task: Task, **kwargs: Any) -> None
Finalize task, after all the steps are completed.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/step.py
226 227 228 229 230 231 232 233 234 235 |
|
SelfReflectionAgentWorker #
Bases: BaseModel
, BaseAgentWorker
Self Reflection Agent Worker.
This agent performs a reflection without any tools on a given response and subsequently performs correction. It should be noted that this reflection implementation has been inspired by two works:
- Reflexion: Language Agents with Verbal Reinforcement Learning, by Shinn et al. (2023) (https://arxiv.org/pdf/2303.11366.pdf)
- CRITIC: Large Language Models Can Self-Correct with Tool-Interactive Critiquing, by Gou et al. (2024) (https://arxiv.org/pdf/2305.11738.pdf)
This agent performs cycles of reflection and correction on an initial response until a satisfactory correction has been generated or a max number of cycles has been reached. To perform reflection, this agent utilizes a user-specified LLM along with a PydanticProgram (thru structured_predict) to generate a structured output that contains an LLM generated reflection of the current response. After reflection, the same user-specified LLM is used again but this time with another PydanticProgram to generate a structured output that contains an LLM generated corrected version of the current response against the priorly generated reflection.
Attr
max_iterations (int, optional): The max number of reflection & correction. Defaults to DEFAULT_MAX_ITERATIONS. callback_manager (Optional[CallbackManager], optional): Callback manager. Defaults to None. llm (Optional[LLM], optional): The LLM used to perform reflection and correction. Must be an OpenAI LLM at this time. Defaults to None. verbose (bool, optional): Whether execution should be verbose. Defaults to False.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
|
from_defaults
classmethod
#
from_defaults(llm: Optional[LLM] = None, max_iterations: int = DEFAULT_MAX_ITERATIONS, callback_manager: Optional[CallbackManager] = None, verbose: bool = False, **kwargs: Any) -> SelfReflectionAgentWorker
Convenience constructor.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
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 |
|
initialize_step #
Initialize step from task.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
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 |
|
run_step #
run_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
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 |
|
arun_step
async
#
arun_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
stream_step #
stream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
447 448 449 450 451 |
|
astream_step
async
#
astream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
453 454 455 456 457 458 459 |
|
finalize_task #
finalize_task(task: Task, **kwargs: Any) -> None
Finalize task, after all the steps are completed.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/self_reflection.py
468 469 470 471 472 473 |
|
ToolInteractiveReflectionAgentWorker #
Bases: BaseModel
, BaseAgentWorker
Tool-Interactive Reflection Agent Worker.
This agent worker implements the CRITIC reflection framework introduced by Gou, Zhibin, et al. (2024) ICLR. (source: https://arxiv.org/pdf/2305.11738)
CRITIC stands for Correcting with tool-interactive critiquing
. It works
by performing a reflection on a response to a task/query using external tools
(e.g., fact checking using a Google search tool) and subsequently using
the critique to generate a corrected response. It cycles thru tool-interactive
reflection and correction until a specific stopping criteria has been met
or a max number of iterations has been reached.
This agent delegates the critique subtask to a user-supplied critique_agent_worker
that is of FunctionCallingAgentWorker
type i.e. it uses tools to perform
tasks. For correction, it uses a user-specified correction_llm
with a
PydanticProgram (determined dynamically with llm.structured_predict)
in order to produce a structured output, namely Correction
that
contains the correction generated by the correction_llm
.
Attributes:
Name | Type | Description |
---|---|---|
critique_agent_worker |
FunctionCallingAgentWorker
|
Critique agent responsible for performing the critique reflection. |
critique_template |
str
|
The template containing instructions for how the Critique agent should perform the reflection. |
max_iterations |
int
|
The max number of reflection & correction cycles permitted. Defaults to DEFAULT_MAX_ITERATIONS = 5. |
stopping_callable |
Optional[StoppingCallable]
|
An optional stopping condition that operates over the critique reflection string and returns a boolean to determine if the latest correction is sufficient. Defaults to None. |
correction_llm |
Optional[LLM]
|
The LLM used for producing corrected responses against a critique or reflection. Defaults to None. |
callback_manager |
Optional[CallbackManager]
|
Callback manager. Defaults to None. |
verbose |
bool
|
Whether execution should be verbose. Defaults to False. |
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
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 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
from_defaults
classmethod
#
from_defaults(critique_agent_worker: FunctionCallingAgentWorker, critique_template: str, correction_llm: Optional[LLM] = None, max_iterations: int = DEFAULT_MAX_ITERATIONS, stopping_callable: Optional[StoppingCallable] = None, callback_manager: Optional[CallbackManager] = None, verbose: bool = False, **kwargs: Any) -> ToolInteractiveReflectionAgentWorker
Convenience constructor method from set of of BaseTools (Optional).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.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 |
|
initialize_step #
Initialize step from task.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
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 |
|
run_step #
run_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
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 |
|
arun_step
async
#
arun_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
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 414 415 416 417 418 419 420 |
|
stream_step #
stream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
423 424 425 426 427 428 429 |
|
astream_step
async
#
astream_step(step: TaskStep, task: Task, **kwargs: Any) -> TaskStepOutput
Run step (async stream).
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
431 432 433 434 435 436 437 438 439 |
|
finalize_task #
finalize_task(task: Task, **kwargs: Any) -> None
Finalize task, after all the steps are completed.
Source code in llama-index-integrations/agent/llama-index-agent-introspective/llama_index/agent/introspective/reflective/tool_interactive_reflection.py
448 449 450 451 452 453 |
|