From bd5c5911af0ed1b0b1834dbd747132f143d47e1b Mon Sep 17 00:00:00 2001 From: Niels Geens Date: Wed, 30 Oct 2024 16:48:20 +0100 Subject: [PATCH] Updated working principles --- diagrams/SIA_Class_Diagram.puml | 75 +++++++++ old_architecture.md | 44 +++++ readme.md | 279 ++++++++++++++------------------ 3 files changed, 239 insertions(+), 159 deletions(-) create mode 100644 diagrams/SIA_Class_Diagram.puml create mode 100644 old_architecture.md diff --git a/diagrams/SIA_Class_Diagram.puml b/diagrams/SIA_Class_Diagram.puml new file mode 100644 index 0000000..43c6fc5 --- /dev/null +++ b/diagrams/SIA_Class_Diagram.puml @@ -0,0 +1,75 @@ +@startuml +skinparam classAttributeIconSize 0 + +class AgentCore { + - LLMEngine llmEngine + - List actions + + run() + + templateActions(): List[Element] + + systemInfo(): Element + + buildContext(systemInfo: Element, actions: List[Element]): str + + parseResponse(response: str): List[CoreAction], List[int] + + deleteActions(List[int]) + + deleteAction(id: int) + + stop() +} + +class ServerCore { + - WebSystem webSystem + + run() +} + +class WebSystem { + + updateContext(context: str) + + getUpdatedContext(): str + + updateActions(actions: List[CoreAction]) + + getUpdatedActions(): List[CoreAction] +} + +class LLMEngine { + + infer(context: str): Iterator[str] +} + +class CoreAction { + + id: str + + template(id: str): Element + + execute() +} + +class SingleShotAction extends CoreAction { +} + +class RepeatAction extends CoreAction { +} + +class BackgroundAction extends CoreAction { +} + +class DeleteAction { + + execute(context: Context) +} + +class InferenceResult { + + reasoning: str + + actions: List[CoreAction] + + {static} parse(llmOutput: str): InferenceResult +} + +Agent o-- LLMEngine +Agent o-- AgentCore +Agent o-- ServerCore +Agent o-- CoreAction + +AgentCore o-- Context +AgentCore o-- InferenceResult +AgentCore o-- CoreAction + +ServerCore o-- WebSystem +ServerCore o-- InferenceResult +ServerCore o-- CoreAction + +CoreAction <|-- SingleShotAction +CoreAction <|-- RepeatAction +CoreAction <|-- BackgroundAction +CoreAction o-- DeleteAction +@enduml \ No newline at end of file diff --git a/old_architecture.md b/old_architecture.md new file mode 100644 index 0000000..506eacc --- /dev/null +++ b/old_architecture.md @@ -0,0 +1,44 @@ +## Architecture + +An overview of the key components and their interactions. + +![SIA Component Model](./diagrams/SIA_Component_Model.svg) + +### Core Actions + +Core actions are implemented each in a separate class. +Each action in the context is an instance of the corresponding class. +The `delete` and `stop` actions are exceptions to this. +They are implemented as functions in the `AgentCore`. + +Actions have a `template(id)` method that returns an XML Element. +If they require updating in each iteration, they do so at the start of the `template` method. + +### Agent Core + +The `AgentCore` manages the state of the agent. + +It can also run a basic main loop consisting of: +- Running template on all actions +- Collecting system information +- Building the context +- Running the LLM +- Splitting the LLM output in reasoning and actions +- Instantiating the new actions and listing id's to delete +- Deleting the old actions + +### Web System + +The `WebSystem` uses an `AgentCore` but doesn't run the main loop. +It runs a modified main loop and interacts with the WebSystem. +It also instantiates alternative actions for stdio to interact with the WebSystem. + +### LLM Engine + +The `LLMEngine` does the LLM inference. +It takes a context as string and returns an iterator of tokens. + +### Inference Result + +An `InferenceResult` object contains the resoning and parsed actions. +Parsing is part of the Inference Result constructor. \ No newline at end of file diff --git a/readme.md b/readme.md index dee17dc..a86f7bd 100644 --- a/readme.md +++ b/readme.md @@ -1,16 +1,114 @@ # SIA - The Self Improving Agent SIA is an agentic artificial intelligence system that autonomously completes complex tasks by writing and executing scripts. -It uses a Large Language Model (LLM) which operates in a loop, generating reasoning and actions based on an updating context. +It uses a Large Language Model (LLM) which operates in a loop. +Each iteration a context is updated with system info and a list of previous reasoning and actions. +The agent responds with a new reasoning or an action. Context, reasoning and actions are stored in a file for each iteration. SIA can read past iterations to improve its reasoning and actions. It can improve in two ways: -- By providing better reasoning or actions for a given context and update the LLM. -- By modifying its own source code. +- By finetuning the LLM with a better reasoning or action for a given context +- By modifying its own source code + +## Example + +This example shows a typical context with some monitored items and previous actions. +Between each of the responses, the context would be updated. + +### Context + +```xml + + + + + + + + + + + + + + + + + + + +``` + +### Responses + +Start by reasoning about the task. +```xml + + John did not specify an exact time. + I'll suggest 9am. + He also did not specify how to be reminded. + I'll ask but if he doesn't respond I'll assume a text message on standard output is fine. + I'll write down this task in a file so I remember it even on a power failure. + +``` + +Store important information on disk. +```xml + /tasks/reminder_to_feed_cat.txt]]> +``` + +Respond to the user. +```xml +I'll remind you to feed the cat tomorrow morning at 9am. Is a message on the standard output ok? +``` + +Clear initial reasoning. +```xml + +``` + +The conversation is kept in context to understand the user's expected response. +If the context was near full, it would be summarized and cleaned up. + +The single shot is also kept in context. +If the file was updated often, it could be replaced by a repeated `cat`, like the general info. ## Working principles -High-level overview of the main components of SIA and how they work together. +The main context is regenerated for each iteration. +It contains info about the system and previous actions that have not been deleted. +Together with the system prompt and available core actions it forms the prompt for the LLM. +The LLM responds with one core action. + +### Core Actions + +There are only a few core actions: +- Starting a script +- Deleting data from context +- Stopping SIA +- Reading standard input +- Writing to standard output +- Reasoning ### Scripts @@ -34,38 +132,10 @@ Similar to single-shot scripts, the next iteration starts after all repeat scrip #### Background The script is started and keeps running. -This is useful for long-running processes e.g. a web server or a communication channel. -Because output of a background script can grow long, it is often redirected to a file. +This is useful for waiting for events, a communication channels or a process that requires attention. -### LLM prompt - -The main context is regenerated for each iteration. -It contains info about the system, the scripts and what happended in the previous iteration. -Together with the system prompt and available core actions it forms the prompt for the LLM. - -The LLM generates reasoning and an XML structure with core actions. -If the structure cannot be parsed, the error is described and the LLM is asked to try again. -This can continue until the context overflows. -Only the first reasoning, last reasoning and last actions are shown in the new context. -All is stored on the file system. - -### Core Actions - -There are only a few core actions: -- Starting a script -- Stopping a script -- Stopping SIA -- Reading standard input -- Writing to standard output - -Standard error is used by the core for debugging. - -SIA typically runs in a Docker container. -When stopped, the latest container version is pulled. -This is how SIA can be updated. - -SIA can also run SIA processes as script. -This can be used for testing updates to the LLM or core functionality. +Long-running processes e.g. a web server can be run as a service or detached process to keep the context small. +The output can be redirected to a file and monitored with a repeat script. ### Use of XML @@ -73,11 +143,21 @@ The context and actions are formatted as XML. For the context this adds clear rules for escaping. This is usefull in case a previous context is embedded. -The response starts with freeform reasoning followed by XML formatted actions. -In case the LLM makes a mistake it can start over. -Only the last XML block is evaluated. +The LLM is free to escape data any way it wants, +as long as it results in valid XML. +The Context is escaped using CDATA blocks. +Except when the data contains CDATA closing sequences. +Then the whole block is escaped using standard XML escaping. -Action results are added in the context in the previous_iteration section. +### The SIA process + +SIA is typically runs with the `restart.sh` script. +This is a simple shell script that runs SIA in a loop. +When stopped, SIA restarts and reloads the Python files. +This is how SIA can self-update. + +SIA can also run SIA processes as script. +This can be used for testing updates to the LLM or core functionality. ### Server for debuggin and human input @@ -88,123 +168,4 @@ The web interface takes over standard input and output. It will display the context for editing before handing it to the LLM. After each run of the LLM, before parsing, it will display the reasoning and actions. It interactively displays if the actions can be parsed. -At any time, the user can write to the standard input of SIA. - -## Architecture - -An overview of the key components and their interactions. - -![SIA Component Model](./diagrams/SIA_Component_Model.svg) - -### Modules - -Modules execute core commands and provide data for the context template. - -- System Module - - System information - - SIA stdio operations - - Stopping SIA (possibly triggering an update) -- Process Module - - Starting scripts - - Stopping scripts - - Managing process stdio and status - -### Agent Core - -The Agent Core runs the SIA main loop. -This loop consists of: -- Templating the context -- Running the LLM -- Parsing the LLM output -- Rerunning the LLM if the output cannot be parsed -- Executing the appropriate actions - -### Server Core - -The Server Core is an alternative for the Agent Core. -It runs a modified main loop and ues the WebSystem Module. -This is an extension of the System Module redirecting stdio to the web interface. - -### LLM Engine - -The LLM Engine does the LLM inference. -It takes a context and returns an iterator of tokens. - -### Inference Result - -An Inference Result object contains the resoning and parsed actions. -Parsing is part of the Inference Result constructor. - -## Example iterations - -### Clarifying a task - -This example shows how to work with standard IO, run simple scripts and monitor files. - -#### Context - -```xml - - - - - - There is data available on the standard input channel. I should read it. I have no other running tasks to tend to. - - - - - - - - - - - - - - - - - - - - - -``` - -#### LLM response - -```xml -John did not specify an exact time. I'll suggest 9am. He also did not specify how to be reminded. I'll ask but if he doesn't respond I'll assume a text message on standard output is fine. I'll write down this task in a file so I can keep it in context. I can write simple files with busybox:latest and echo but I will need to use sh -c to do the redirect. - - - - sh - -c - /tasks/reminder.txt]]> - - /tasks:/tasks - - - - -``` +At any time, the user can write to the standard input of SIA. \ No newline at end of file