auto_chaos.chaos

🦹🏼‍♂️ Chaos module 🦹🏼‍♂️

  1"""
  2🦹🏼‍♂️
  3Chaos module
  4🦹🏼‍♂️
  5"""
  6import os
  7import pprint
  8
  9from openai.types.chat import ChatCompletionMessage
 10
 11from auto_chaos.gpt_utils import generate_text
 12from auto_chaos.base_system import BaseSystem
 13from auto_chaos.utility_system import UtilitySystem
 14
 15PROMPTS_PATH = os.path.join(os.path.dirname(__file__), "prompts")
 16
 17
 18class Chaos:
 19    """
 20    Here the chaos happen !
 21    """
 22
 23    def __init__(self, systems: list[BaseSystem], initial_state: dict) -> None:
 24        # Load chaos engineer prompt
 25        with open(
 26            os.path.join(
 27                PROMPTS_PATH,
 28                "chaos_engineer.txt",
 29            ),
 30            "r",
 31        ) as file:
 32            self.system_prompt = file.read()
 33
 34        # Prepare first system and use messages
 35        self.messages = [
 36            {"role": "system", "content": self.system_prompt},
 37            {"role": "user", "content": f"{initial_state}"},
 38        ]
 39
 40        # Prepare systems
 41        self.systems = systems
 42        self.systems.append(UtilitySystem())
 43
 44    def chaos(self, objective=5) -> None:
 45        """
 46        Do chaos
 47
 48        Args:
 49            objective (int, optional): Number of chaos iteration. Defaults to 5.
 50        """
 51        # This is the end of the chaos
 52        if objective < 0:
 53            return
 54
 55        # Let the chaos engineer do his things and give actions to be done
 56        response = None
 57        while not response:
 58            response = generate_text(
 59                self.messages,
 60                model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"),
 61                temperature=1,
 62            )
 63            response = response.choices[0].message
 64        self.messages.append(response.dict())
 65
 66        # Do actions in all systems
 67        print(f"🦹🏼 Doing {response.content}")
 68        system_result = []
 69        system_error = []
 70        for system in self.systems:
 71            result, error = system.do_action(response.content)
 72            system_result += result
 73            system_error += error
 74
 75        # Add the result to messages for the chaos engineer
 76        if system_result or system_error:
 77            self.messages.append(
 78                {
 79                    "role": "user",
 80                    "content": f"Result : {system_result}"
 81                    + (f", Error: {system_error}" if system_error else ""),
 82                }
 83            )
 84            print(f"        🚀 Results {system_result}")
 85            print(f"        😵 Errors {system_error}")
 86
 87        # Do chaos again
 88        self.chaos(objective - 1)
 89
 90    def report(self):
 91        """
 92        Do a report of the chaos
 93        """
 94        print(f"🦹🏼‍♂️ Doing report, please wait ...")
 95        report = []
 96
 97        # Do a description of all systems
 98        # for system in self.systems:
 99        #    system_description = f"{system.do_action('DESCRIBE')}"
100
101        # Load chaos report prompt
102        with open(
103            os.path.join(
104                PROMPTS_PATH,
105                "chaos_report.txt",
106            ),
107            "r",
108        ) as file:
109            system_prompt = file.read()
110        report.append({"role": "system", "content": system_prompt})
111
112        # Give chaos engineer all previous actions result
113        report_prompt = "Here are the results of chaos actions:"
114        for message in self.messages:
115            if isinstance(message, ChatCompletionMessage):
116                if message.role != "user":
117                    continue
118                report_prompt += f" {message.content}"
119                continue
120            if message["role"] != "user":
121                continue
122            report_prompt += f" {message['content']}"
123        report.append({"role": "user", "content": report_prompt})
124
125        # Ask chaos engineer to do a report of the chaos
126        response = generate_text(
127            report, model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"), temperature=1.0
128        )
129
130        # Add chaos report to messages
131        self.messages.append(
132            {"role": "assistant", "content": response.choices[0].message.content}
133        )
134
135        pp = pprint.PrettyPrinter(indent=4)
136        pp.pprint(response.choices[0].message.content)
PROMPTS_PATH = '/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/auto_chaos/prompts'
class Chaos:
 19class Chaos:
 20    """
 21    Here the chaos happen !
 22    """
 23
 24    def __init__(self, systems: list[BaseSystem], initial_state: dict) -> None:
 25        # Load chaos engineer prompt
 26        with open(
 27            os.path.join(
 28                PROMPTS_PATH,
 29                "chaos_engineer.txt",
 30            ),
 31            "r",
 32        ) as file:
 33            self.system_prompt = file.read()
 34
 35        # Prepare first system and use messages
 36        self.messages = [
 37            {"role": "system", "content": self.system_prompt},
 38            {"role": "user", "content": f"{initial_state}"},
 39        ]
 40
 41        # Prepare systems
 42        self.systems = systems
 43        self.systems.append(UtilitySystem())
 44
 45    def chaos(self, objective=5) -> None:
 46        """
 47        Do chaos
 48
 49        Args:
 50            objective (int, optional): Number of chaos iteration. Defaults to 5.
 51        """
 52        # This is the end of the chaos
 53        if objective < 0:
 54            return
 55
 56        # Let the chaos engineer do his things and give actions to be done
 57        response = None
 58        while not response:
 59            response = generate_text(
 60                self.messages,
 61                model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"),
 62                temperature=1,
 63            )
 64            response = response.choices[0].message
 65        self.messages.append(response.dict())
 66
 67        # Do actions in all systems
 68        print(f"🦹🏼 Doing {response.content}")
 69        system_result = []
 70        system_error = []
 71        for system in self.systems:
 72            result, error = system.do_action(response.content)
 73            system_result += result
 74            system_error += error
 75
 76        # Add the result to messages for the chaos engineer
 77        if system_result or system_error:
 78            self.messages.append(
 79                {
 80                    "role": "user",
 81                    "content": f"Result : {system_result}"
 82                    + (f", Error: {system_error}" if system_error else ""),
 83                }
 84            )
 85            print(f"        🚀 Results {system_result}")
 86            print(f"        😵 Errors {system_error}")
 87
 88        # Do chaos again
 89        self.chaos(objective - 1)
 90
 91    def report(self):
 92        """
 93        Do a report of the chaos
 94        """
 95        print(f"🦹🏼‍♂️ Doing report, please wait ...")
 96        report = []
 97
 98        # Do a description of all systems
 99        # for system in self.systems:
100        #    system_description = f"{system.do_action('DESCRIBE')}"
101
102        # Load chaos report prompt
103        with open(
104            os.path.join(
105                PROMPTS_PATH,
106                "chaos_report.txt",
107            ),
108            "r",
109        ) as file:
110            system_prompt = file.read()
111        report.append({"role": "system", "content": system_prompt})
112
113        # Give chaos engineer all previous actions result
114        report_prompt = "Here are the results of chaos actions:"
115        for message in self.messages:
116            if isinstance(message, ChatCompletionMessage):
117                if message.role != "user":
118                    continue
119                report_prompt += f" {message.content}"
120                continue
121            if message["role"] != "user":
122                continue
123            report_prompt += f" {message['content']}"
124        report.append({"role": "user", "content": report_prompt})
125
126        # Ask chaos engineer to do a report of the chaos
127        response = generate_text(
128            report, model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"), temperature=1.0
129        )
130
131        # Add chaos report to messages
132        self.messages.append(
133            {"role": "assistant", "content": response.choices[0].message.content}
134        )
135
136        pp = pprint.PrettyPrinter(indent=4)
137        pp.pprint(response.choices[0].message.content)

Here the chaos happen !

Chaos( systems: list[auto_chaos.base_system.BaseSystem], initial_state: dict)
24    def __init__(self, systems: list[BaseSystem], initial_state: dict) -> None:
25        # Load chaos engineer prompt
26        with open(
27            os.path.join(
28                PROMPTS_PATH,
29                "chaos_engineer.txt",
30            ),
31            "r",
32        ) as file:
33            self.system_prompt = file.read()
34
35        # Prepare first system and use messages
36        self.messages = [
37            {"role": "system", "content": self.system_prompt},
38            {"role": "user", "content": f"{initial_state}"},
39        ]
40
41        # Prepare systems
42        self.systems = systems
43        self.systems.append(UtilitySystem())
messages
systems
def chaos(self, objective=5) -> None:
45    def chaos(self, objective=5) -> None:
46        """
47        Do chaos
48
49        Args:
50            objective (int, optional): Number of chaos iteration. Defaults to 5.
51        """
52        # This is the end of the chaos
53        if objective < 0:
54            return
55
56        # Let the chaos engineer do his things and give actions to be done
57        response = None
58        while not response:
59            response = generate_text(
60                self.messages,
61                model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"),
62                temperature=1,
63            )
64            response = response.choices[0].message
65        self.messages.append(response.dict())
66
67        # Do actions in all systems
68        print(f"🦹🏼 Doing {response.content}")
69        system_result = []
70        system_error = []
71        for system in self.systems:
72            result, error = system.do_action(response.content)
73            system_result += result
74            system_error += error
75
76        # Add the result to messages for the chaos engineer
77        if system_result or system_error:
78            self.messages.append(
79                {
80                    "role": "user",
81                    "content": f"Result : {system_result}"
82                    + (f", Error: {system_error}" if system_error else ""),
83                }
84            )
85            print(f"        🚀 Results {system_result}")
86            print(f"        😵 Errors {system_error}")
87
88        # Do chaos again
89        self.chaos(objective - 1)

Do chaos

Arguments:
  • objective (int, optional): Number of chaos iteration. Defaults to 5.
def report(self):
 91    def report(self):
 92        """
 93        Do a report of the chaos
 94        """
 95        print(f"🦹🏼‍♂️ Doing report, please wait ...")
 96        report = []
 97
 98        # Do a description of all systems
 99        # for system in self.systems:
100        #    system_description = f"{system.do_action('DESCRIBE')}"
101
102        # Load chaos report prompt
103        with open(
104            os.path.join(
105                PROMPTS_PATH,
106                "chaos_report.txt",
107            ),
108            "r",
109        ) as file:
110            system_prompt = file.read()
111        report.append({"role": "system", "content": system_prompt})
112
113        # Give chaos engineer all previous actions result
114        report_prompt = "Here are the results of chaos actions:"
115        for message in self.messages:
116            if isinstance(message, ChatCompletionMessage):
117                if message.role != "user":
118                    continue
119                report_prompt += f" {message.content}"
120                continue
121            if message["role"] != "user":
122                continue
123            report_prompt += f" {message['content']}"
124        report.append({"role": "user", "content": report_prompt})
125
126        # Ask chaos engineer to do a report of the chaos
127        response = generate_text(
128            report, model=os.getenv("MODEL_NAME", "gpt-3.5-turbo-16k"), temperature=1.0
129        )
130
131        # Add chaos report to messages
132        self.messages.append(
133            {"role": "assistant", "content": response.choices[0].message.content}
134        )
135
136        pp = pprint.PrettyPrinter(indent=4)
137        pp.pprint(response.choices[0].message.content)

Do a report of the chaos