|
| 1 | +# Resources |
| 2 | + |
| 3 | +Resources are a component of workflows that allow us to equip our steps with external dependencies such as memory, LLMs, query engines or chat history. |
| 4 | + |
| 5 | +Resources are a powerful way of binding components to our steps that we otherwise would need to specify by hand every time and, most importantly, resources are **stateful**, meaning that they maintain their state across different steps, unless otherwise specified. |
| 6 | + |
| 7 | +## Using Stateful Resources |
| 8 | + |
| 9 | +In order to use them within our code, we need to import them from the `resource` submodule: |
| 10 | + |
| 11 | +```python |
| 12 | +from llama_index.core.workflow.resource import Resource |
| 13 | +from llama_index.core.workflow import ( |
| 14 | + Event, |
| 15 | + step, |
| 16 | + StartEvent, |
| 17 | + StopEvent, |
| 18 | + Workflow, |
| 19 | +) |
| 20 | +``` |
| 21 | + |
| 22 | +The `Resource` function works as a wrapper for another function that, when executed, returns an object of a specified type. This is the usage pattern: |
| 23 | + |
| 24 | +```python |
| 25 | +from typing import Annotated |
| 26 | +from llama_index.core.memory import Memory |
| 27 | + |
| 28 | + |
| 29 | +def get_memory(*args, **kwargs) -> Memory: |
| 30 | + return Memory.from_defaults("user_id_123", token_limit=60000) |
| 31 | + |
| 32 | + |
| 33 | +resource = Annotated[Memory, Resource(get_memory)] |
| 34 | +``` |
| 35 | + |
| 36 | +When a step of our workflow will be equipped with this resource, the variable in the step to which the resource is assigned would behave as a memory component: |
| 37 | + |
| 38 | +```python |
| 39 | +import random |
| 40 | + |
| 41 | +from typing import Union |
| 42 | +from llama_index.core.llms import ChatMessage |
| 43 | + |
| 44 | +RANDOM_MESSAGES = [ |
| 45 | + "Hello World!", |
| 46 | + "Python is awesome!", |
| 47 | + "Resources are great!", |
| 48 | +] |
| 49 | + |
| 50 | + |
| 51 | +class CustomStartEvent(StartEvent): |
| 52 | + message: str |
| 53 | + |
| 54 | + |
| 55 | +class SecondEvent(Event): |
| 56 | + message: str |
| 57 | + |
| 58 | + |
| 59 | +class ThirdEvent(Event): |
| 60 | + message: str |
| 61 | + |
| 62 | + |
| 63 | +class WorkflowWithMemory(Workflow): |
| 64 | + @step |
| 65 | + async def first_step( |
| 66 | + self, |
| 67 | + ev: CustomStartEvent, |
| 68 | + memory: Annotated[Memory, Resource(get_memory)], |
| 69 | + ) -> SecondEvent: |
| 70 | + await memory.aput( |
| 71 | + ChatMessage.from_str( |
| 72 | + role="user", content="First step: " + ev.message |
| 73 | + ) |
| 74 | + ) |
| 75 | + return SecondEvent(message=RANDOM_MESSAGES[random.randint(0, 2)]) |
| 76 | + |
| 77 | + @step |
| 78 | + async def second_step( |
| 79 | + self, ev: SecondEvent, memory: Annotated[Memory, Resource(get_memory)] |
| 80 | + ) -> Union[ThirdEvent, StopEvent]: |
| 81 | + await memory.aput( |
| 82 | + ChatMessage(role="assistant", content="Second step: " + ev.message) |
| 83 | + ) |
| 84 | + if random.randint(0, 1) == 0: |
| 85 | + return ThirdEvent(message=RANDOM_MESSAGES[random.randint(0, 2)]) |
| 86 | + else: |
| 87 | + messages = await memory.aget_all() |
| 88 | + return StopEvent(result=messages) |
| 89 | + |
| 90 | + @step |
| 91 | + async def third_step( |
| 92 | + self, ev: ThirdEvent, memory: Annotated[Memory, Resource(get_memory)] |
| 93 | + ) -> StopEvent: |
| 94 | + await memory.aput( |
| 95 | + ChatMessage(role="user", content="Third step: " + ev.message) |
| 96 | + ) |
| 97 | + messages = await memory.aget_all() |
| 98 | + return StopEvent(result=messages) |
| 99 | +``` |
| 100 | + |
| 101 | +As you can see, each step has access to memory and writes to it - the memory is shared among them and we can see it by running the workflow: |
| 102 | + |
| 103 | +```python |
| 104 | +wf = WorkflowWithMemory(disable_validation=True) |
| 105 | + |
| 106 | + |
| 107 | +async def main(): |
| 108 | + messages = await wf.run( |
| 109 | + start_event=CustomStartEvent(message="Happy birthday!") |
| 110 | + ) |
| 111 | + for m in messages: |
| 112 | + print(m.blocks[0].text) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + import asyncio |
| 117 | + |
| 118 | + asyncio.run(main()) |
| 119 | +``` |
| 120 | + |
| 121 | +A potential result for this might be: |
| 122 | + |
| 123 | +```text |
| 124 | +First step: Happy birthday! |
| 125 | +Second step: Python is awesome! |
| 126 | +Third step: Hello World! |
| 127 | +``` |
| 128 | + |
| 129 | +This shows that each step added its message to a global memory, which is exactly what we were expecting! |
| 130 | + |
| 131 | +It is important to note, though, the resources are preserved across steps of the same workflow instance, but not across different workflows. If we were to run two `WorkflowWithMemory` instances, their memories would be separate and independent: |
| 132 | + |
| 133 | +```python |
| 134 | +wf1 = WorkflowWithMemory(disable_validation=True) |
| 135 | +wf2 = WorkflowWithMemory(disable_validation=True) |
| 136 | + |
| 137 | + |
| 138 | +async def main(): |
| 139 | + messages1 = await wf1.run( |
| 140 | + start_event=CustomStartEvent(message="Happy birthday!") |
| 141 | + ) |
| 142 | + messages2 = await wf1.run( |
| 143 | + start_event=CustomStartEvent(message="Happy New Year!") |
| 144 | + ) |
| 145 | + for m in messages1: |
| 146 | + print(m.blocks[0].text) |
| 147 | + print("===================") |
| 148 | + for m in messages2: |
| 149 | + print(m.blocks[0].text) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + import asyncio |
| 154 | + |
| 155 | + asyncio.run(main()) |
| 156 | +``` |
| 157 | + |
| 158 | +This is a possible output: |
| 159 | + |
| 160 | +```text |
| 161 | +First step: Happy birthday! |
| 162 | +Second step: Resources are great! |
| 163 | +=================== |
| 164 | +First step: Happy New Year! |
| 165 | +Second step: Python is awesome! |
| 166 | +``` |
| 167 | + |
| 168 | +## Using Steteless Resources |
| 169 | + |
| 170 | +Resources can also be stateless, meaning that we can configure them *not* to be preserved across steps in the same run. |
| 171 | + |
| 172 | +In order to do so, we just need to specify `cache=False` when instantiating `Resource` - let's see this in a simple example, using a custom `Counter` class: |
| 173 | + |
| 174 | +```python |
| 175 | +from pydantic import BaseModel, Field |
| 176 | + |
| 177 | + |
| 178 | +class Counter(BaseModel): |
| 179 | + counter: int = Field(description="A simple counter", default=0) |
| 180 | + |
| 181 | + async def increment(self) -> None: |
| 182 | + self.counter += 1 |
| 183 | + |
| 184 | + |
| 185 | +def get_counter() -> Counter: |
| 186 | + return Counter() |
| 187 | + |
| 188 | + |
| 189 | +class SecondEvent(Event): |
| 190 | + count: int |
| 191 | + |
| 192 | + |
| 193 | +class WorkflowWithCounter(Workflow): |
| 194 | + @step |
| 195 | + async def first_step( |
| 196 | + self, |
| 197 | + ev: StartEvent, |
| 198 | + counter: Annotated[Counter, Resource(get_counter, cache=False)], |
| 199 | + ) -> SecondEvent: |
| 200 | + await counter.increment() |
| 201 | + return SecondEvent(count=counter.counter) |
| 202 | + |
| 203 | + @step |
| 204 | + async def second_step( |
| 205 | + self, |
| 206 | + ev: SecondEvent, |
| 207 | + counter: Annotated[Counter, Resource(get_counter, cache=False)], |
| 208 | + ) -> StopEvent: |
| 209 | + print("Counter at first step: ", ev.count) |
| 210 | + await counter.increment() |
| 211 | + print("Counter at second step: ", counter.counter) |
| 212 | + return StopEvent(result="End of Workflow") |
| 213 | +``` |
| 214 | + |
| 215 | +If we now run this workflow, we will get out: |
| 216 | + |
| 217 | +```text |
| 218 | +Counter at first step: 1 |
| 219 | +Counter at second step: 1 |
| 220 | +``` |
| 221 | + |
| 222 | +Now that we've mastered resources, let's take a look at [observability and debugging](./observability.md) in workflows. |
0 commit comments