|
1 | 1 | # Sorcery |
2 | 2 |
|
3 | | -The future of AI roleplay. |
| 3 | +Sorcery is a [SillyTavern](https://github.com/SillyTavern/SillyTavern) extension |
| 4 | +that allows AI characters to reach into the real world. It lets you bind arbitrary |
| 5 | +STscript or JavaScript code to arbitrary events in the chat. It is infinitely more |
| 6 | +powerful than existing "character expression" systems, and dramatically easier to |
| 7 | +use than traditional function calling setups. It does **not** require a specially |
| 8 | +trained function calling model. |
| 9 | + |
| 10 | +Sorcery can enable your virtual characters to do tangible things, from interacting |
| 11 | +with your SillyTavern instance to controlling smart home appliances and toys. |
| 12 | +It is **zero-configuration,** and once installed will immediately work with most |
| 13 | +models and setups. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +Sorcery executes actions **while the response is streaming, at the exact moment |
| 18 | +the relevant event occurs,** as demonstrated in this video: |
| 19 | + |
| 20 | +https://github.com/user-attachments/assets/49ff8f62-2674-4062-b378-bc272d1212e1 |
| 21 | + |
| 22 | +Sorcery works by injecting dynamically generated instructions into the system prompt |
| 23 | +that tell the model to insert special markers into its responses when the configured |
| 24 | +events occur. It then hooks the output stream, and intercepts those markers, removing |
| 25 | +them from the output and running the associated scripts. The whole process is |
| 26 | +completely invisible to the user. |
| 27 | + |
| 28 | +Even relatively small models respond well to Sorcery's instructions. For example, |
| 29 | +I have successfully used Sorcery with the IQ3_M quant of Mistral Small, which fits |
| 30 | +into 12 GB VRAM. |
| 31 | + |
| 32 | + |
| 33 | +## Requirements |
| 34 | + |
| 35 | +For Sorcery to work, you need: |
| 36 | + |
| 37 | +* A text completion backend |
| 38 | +* Instruct mode enabled |
| 39 | +* System prompt enabled |
| 40 | +* Any character-specific system prompt overrides *disabled* |
| 41 | + |
| 42 | +Most users of local models will already have this configuration, and don't |
| 43 | +need to do anything special. |
| 44 | + |
| 45 | +Sorcery may or may not work with API-only hosted models supporting only chat |
| 46 | +completion mode. I don't use such models myself, and haven't tested this case. |
| 47 | +Feedback is appreciated. |
| 48 | + |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +Sorcery can be installed in seconds: |
| 53 | + |
| 54 | +1. Open SillyTavern |
| 55 | +2. Click the "Extensions" button in the top bar |
| 56 | +3. Click "Install extension" |
| 57 | +4. Copy this URL into the input field: `https://github.com/p-e-w/sorcery` |
| 58 | +5. Click "Install just for me" |
| 59 | + |
| 60 | +A new button should appear in the top bar that looks like a wizard's hat. |
| 61 | +Click that button to open the Sorcery configuration UI. |
| 62 | + |
| 63 | + |
| 64 | +## Is this safe? |
| 65 | + |
| 66 | +It's as safe as you want it to be. |
| 67 | + |
| 68 | +Sorcery enables LLMs to execute the scripts written by the user, nothing more |
| 69 | +and nothing less. Models cannot provide their own code to execute, they can only |
| 70 | +choose among the already configured scripts. Thus even with a malicious model, |
| 71 | +the worst thing that can happen is that it runs one of the scripts you wrote, |
| 72 | +at a time that is inconvenient to you. But it is always you who decides what kind |
| 73 | +of code can be run. |
| 74 | + |
| 75 | + |
| 76 | +## Usage example: Controlling a smart bulb |
| 77 | + |
| 78 | +Sorcery's ability to run arbitrary JavaScript code is extremely powerful, because |
| 79 | +it allows us to make requests to any HTTP server. By whipping up a purpose-built |
| 80 | +HTTP server with Python, we can let Sorcery do almost anything. |
| 81 | + |
| 82 | +This example demonstrates how to control a Philips WiZ WiFi smart light bulb from |
| 83 | +Sorcery. WiZ bulbs are relatively cheap, available in most countries, and can be |
| 84 | +controlled entirely using open-source software once configured. If you have another |
| 85 | +brand of smart light, adapt the instructions as needed. |
| 86 | + |
| 87 | +Configure your WiZ bulb and connect it to the same LAN as your PC. Then figure out |
| 88 | +the local IP address of the bulb, for example by logging in to your router. Now |
| 89 | +follow these instructions: |
| 90 | + |
| 91 | +Create a Python virtual environment and install dependencies: |
| 92 | + |
| 93 | +``` |
| 94 | +python3 -m venv .venv |
| 95 | +source .venv/bin/activate |
| 96 | +pip install flask[async] pywizlight |
| 97 | +``` |
| 98 | + |
| 99 | +Copy the following code into a file called `main.py`: |
| 100 | + |
| 101 | +```python |
| 102 | +import pywizlight |
| 103 | +# https://github.com/sbidy/pywizlight/issues/140#issuecomment-1321426436 |
| 104 | +del pywizlight.wizlight.__del__ |
| 105 | +from pywizlight import wizlight, PilotBuilder |
| 106 | +from flask import Flask |
| 107 | + |
| 108 | +bulb_ip = "192.168.1.10" # <-- Your bulb's IP address |
| 109 | + |
| 110 | +app = Flask(__name__) |
| 111 | + |
| 112 | +@app.route("/on") |
| 113 | +async def light_on(): |
| 114 | + light = wizlight(bulb_ip) |
| 115 | + await light.turn_on(PilotBuilder(brightness = 255)) |
| 116 | + return "" |
| 117 | + |
| 118 | +@app.route("/off") |
| 119 | +async def light_off(): |
| 120 | + light = wizlight(bulb_ip) |
| 121 | + await light.turn_off() |
| 122 | + return "" |
| 123 | +``` |
| 124 | + |
| 125 | +Run the server: |
| 126 | + |
| 127 | +``` |
| 128 | +flask --app main run --port 3000 |
| 129 | +``` |
| 130 | + |
| 131 | +Open Sorcery and copy the following code into the JavaScript field of the |
| 132 | +**"{{char}} turns off the lights"** default script: |
| 133 | + |
| 134 | +```javascript |
| 135 | +fetch("http://127.0.0.1:3000/off"); |
| 136 | +``` |
| 137 | + |
| 138 | +Now start a chat and create a situation where the AI character turns off the |
| 139 | +lights in the roleplay. **You will see your light bulb turning off in the real |
| 140 | +world.** This is as close to magic as it gets. |
| 141 | + |
| 142 | + |
| 143 | +## Acknowledgments |
| 144 | + |
| 145 | +Sorcery includes the [code-input](https://github.com/WebCoder49/code-input) |
| 146 | +library to provide syntax-highlighted text inputs. code-input is licensed |
| 147 | +under the MIT License. |
| 148 | + |
| 149 | +Parts of Sorcery's UI HTML were copied from SillyTavern's "World Info" UI. |
| 150 | +SillyTavern is licensed under the GNU Affero General Public License. |
4 | 151 |
|
5 | 152 |
|
6 | 153 | ## License |
|
0 commit comments