Skip to content

Context Injection

David-Andrew Samson edited this page Jun 22, 2023 · 9 revisions

While interacting with the LLM react agent, it is possible to inject context messages into the chat history. Context messages are very flexible and can be used in a number of different ways:

  • providing the agent with information about the current state of a tool
  • temporarily including chat history that can be deleted later to save context length
  • giving the LLM instructions that are only relevant for its next response
  • etc

Managing Context

Context can be created by calling one of the add_context methods on the agent

agent = ReActAgent(...)

# add permanent context
id = agent.add_context("some context message")

# add timed context
n = 5
id = agent.add_context("some message that will be deleted after n steps", lifetime=n)

Timed context messages are automatically deleted from the chat after the specified number of steps, or you can manually delete them before the time expires

Context can be removed via the clear_context function given the message's id

agent.clear_context(id)

Additionally, all context can be cleared with clear_all_context

agent.clear_all_context()

Example Use Cases

  • Archycoder uses context to share the current state of the user's program with the LLM. The context is updated every message so that the program and the LLM never get out of sync
  • Jupyter LLM uses context to indicate the current data being operated on so the LLM can correctly handle data manipulations and analysis