|
| 1 | +import { Tab, Tabs } from "rspress/theme"; |
| 2 | + |
| 3 | +# Why AIScript |
| 4 | + |
| 5 | +AIScript is my experimental project to explore a new way to build web application in AI era. It isn't an oh-another-language built by a guy who just want to learn how to write an interpreter. My ambient is bigger than that. |
| 6 | + |
| 7 | +Not matter what language you use to build web API, you need a web framework. These web framework essentially does the same thing. They all parse HTTP requests, execute SQL queries, and response the result with JSON or render HTML templates. Some framework can do more, validating request params, generate OpenAPI spec, etc. However, different framworks from different languages have totally different develope experience to build an web app. When you switch from one to another, you can't escape to learn the syntax and docs again. That why I want to adopt an brand-new intuitive apporach combine language with web framework to bring the modern experience to build web apps. |
| 8 | + |
| 9 | +## Intuitive first |
| 10 | + |
| 11 | +What I want is write a code like this to run a web API with single command, no need to learn a new web framework, no need to struggle choose which framework to use, no need to install the dependencies to run. Just run `aiscript serve`. |
| 12 | + |
| 13 | +<Tabs> |
| 14 | +<Tab label="route.ai"> |
| 15 | +```javascript |
| 16 | +get /guess { |
| 17 | + |
| 18 | + query { |
| 19 | + @number(min=0, max=100) |
| 20 | + value: int |
| 21 | + } |
| 22 | + |
| 23 | + let message = "You got it!" if query.value == 42 else "Try again"; |
| 24 | + return { message }; |
| 25 | +} |
| 26 | +``` |
| 27 | +</Tab> |
| 28 | +<Tab label="run"> |
| 29 | +```bash |
| 30 | +$ aiscript serve route.ai |
| 31 | +Listening on http://localhost:8080 |
| 32 | +``` |
| 33 | +</Tab> |
| 34 | +<Tab label="curl"> |
| 35 | +```bash |
| 36 | +$ curl -X GET http://localhost:8080/guess?n=10 |
| 37 | +{"message": "Try again"} |
| 38 | + |
| 39 | +$ curl -X GET http://localhost:8080/guess?n=42 |
| 40 | +{"message": "You got it!"} |
| 41 | + |
| 42 | +$ curl -X GET http://localhost:8080/guess?n=999 |
| 43 | +{"error": "Field validation failed: value: Number is greater than the maximum value of 100."} |
| 44 | +``` |
| 45 | +</Tab> |
| 46 | +</Tabs> |
| 47 | + |
| 48 | +Here are just a small poart of cool ideas come to my brain. I always seek for it if someone already built it. But I cannot find one, so I decided to build one. |
| 49 | + |
| 50 | +## Out-of-box first |
| 51 | + |
| 52 | +When build a web app, almost the rest besides the core business logic is the same. How can I support JWT authentication? How to integrate Google or GitHub login? How to query database or validate data? For those reraly changed standard, I wish it built into the language or framework, give me an out-of-box experience. |
| 53 | + |
| 54 | +<Tabs> |
| 55 | +<Tab label="jwt_auth.ai"> |
| 56 | +```rust |
| 57 | +@auth // JWT auth |
| 58 | +post /chat { |
| 59 | + query { |
| 60 | + @string(min_len=5, max_len=200) |
| 61 | + message: str |
| 62 | + } |
| 63 | + |
| 64 | + use std.db.pg; |
| 65 | + let message = pg.query("INSERT INTO messages (message) VALUES ($1)", query.message); |
| 66 | + return message; |
| 67 | +} |
| 68 | +``` |
| 69 | +</Tab> |
| 70 | +<Tab label="google_login.ai"> |
| 71 | +```javascript |
| 72 | +@sso(provider="google") |
| 73 | +get /auth/google { |
| 74 | + let url = sso.authority_url(); |
| 75 | + print(url); |
| 76 | + return temporary_redirect(target=url); |
| 77 | +} |
| 78 | + |
| 79 | +@sso(provider="google") |
| 80 | +get /auth/google/callback { |
| 81 | + query { |
| 82 | + code: str, |
| 83 | + state: str, |
| 84 | + } |
| 85 | + |
| 86 | + print("code", query.code); |
| 87 | + print("state", query.state); |
| 88 | + let user_info = sso.verify(code=query.code); |
| 89 | + print(user_info); |
| 90 | + return { user_info }; |
| 91 | +} |
| 92 | +``` |
| 93 | +</Tab> |
| 94 | +<Tab label="validation.ai"> |
| 95 | +```javascript |
| 96 | +class User { |
| 97 | + @string(min_len=3, max=20) |
| 98 | + name: str, |
| 99 | + @number(min=18, strict_int=true, strict_float=true) |
| 100 | + age: int, |
| 101 | + @in(["male", "female"]) |
| 102 | + gender: str = "male", |
| 103 | +} |
| 104 | +let u1 = User("Le", 20, "male") |err| { |
| 105 | + print("Validate error:", err); |
| 106 | +}; |
| 107 | +// Validate error: { |
| 108 | +// loc: [name], input: Le, type: validation_error, |
| 109 | +// msg: String length is less than the minimum length of 3 |
| 110 | +// } |
| 111 | +let u2 = User("Lee", 17, "male") |err| { |
| 112 | + print("Validate error:", err); |
| 113 | +}; |
| 114 | +// Validate error: { |
| 115 | +// loc: [age], input: 17, type: validation_error, |
| 116 | +// msg: Number is less than the minimum value of 18 |
| 117 | +// } |
| 118 | +let u3 = User("Lee", 20, "boy") |err| { |
| 119 | + print("Validate error:", err); |
| 120 | +}; |
| 121 | +// Validate error: { |
| 122 | +// loc: [gender], input: boy, type: validation_error, |
| 123 | +// msg: Value is not in the list of allowed values |
| 124 | +// } |
| 125 | +let u4 = User("Lee") |err| { |
| 126 | + print("Validate error:", err); |
| 127 | +}; |
| 128 | +// Validate error: { |
| 129 | +// loc: [age], input: nil, type: missing, |
| 130 | +// msg: Field required |
| 131 | +// } |
| 132 | +``` |
| 133 | +</Tab> |
| 134 | +<Tab label="project.toml"> |
| 135 | +```toml |
| 136 | +[auth.jwt] |
| 137 | +secret = "$JWT_SECRET" |
| 138 | +expiration = 3600 |
| 139 | + |
| 140 | +[sso.google] |
| 141 | +client_id = "your_google_client_id" |
| 142 | +client_secret = "your_google_client_secret" |
| 143 | +redirect_url = "http://localhost:8080/auth/google/callback" |
| 144 | +scopes = ["email"] |
| 145 | +``` |
| 146 | +</Tab> |
| 147 | +</Tabs> |
| 148 | + |
| 149 | +## AI first |
| 150 | + |
| 151 | +People we'll ask: |
| 152 | + |
| 153 | +:::info |
| 154 | +"Do we really need a new programming language in the age AI can write code for us?". |
| 155 | +::: |
| 156 | + |
| 157 | +In my own opionion, I think yes, at least for web application. |
| 158 | + |
| 159 | +In the one hand, most of mainstream language designed two-decades ago, `prompt`, `agent` isn't a primitive features in the language, it is in AIScript. Your don't need to install a package to call LLM API or orchestrate multiple agents. |
| 160 | + |
| 161 | +<Tabs> |
| 162 | +<Tab label="prompt.ai"> |
| 163 | +```rust |
| 164 | +let answer = prompt "Why the sky is blue?" => { |
| 165 | + model: "claude-3.7", |
| 166 | + temperature: 0.8, |
| 167 | +}; |
| 168 | +print(answer); |
| 169 | +``` |
| 170 | +</Tab> |
| 171 | +<Tab label="agent.ai"> |
| 172 | +```python |
| 173 | +agent WeatherAgent { |
| 174 | + instructions: "Providing a weather forecast at the locations the user provides.", |
| 175 | + model: "gpt-4o", |
| 176 | + |
| 177 | + fn get_forecast(location: str, forecast_date: date) -> str { |
| 178 | + """ |
| 179 | + Call this tools to forecast weather based on `location` and `forecast_date`. |
| 180 | + """ |
| 181 | + // In real code: call weather API, DB queries, etc. |
| 182 | + return f'The forecast in {location} on {forecast_date} is 24°C and sunny.'; |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +let user_prompt = "What will the weather be like in Paris on Tuesday?"; |
| 187 | +WeatherAgent.run(input=user_prompt); |
| 188 | +``` |
| 189 | +</Tab> |
| 190 | +<Tab label="project.toml"> |
| 191 | +```toml |
| 192 | +[ai.embedding] |
| 193 | +model = "gpt-3.5-turbo" |
| 194 | + |
| 195 | +[ai.openai] |
| 196 | +api_key = "YOUR_API_KEY" |
| 197 | + |
| 198 | +[ai.anthropic] |
| 199 | +api_key = "YOUR_API_KEY" |
| 200 | +``` |
| 201 | +</Tab> |
| 202 | +</Tabs> |
| 203 | + |
| 204 | +In the other hands, many features is out-of-box and batter-included in the standard library. It's helpful for AI code generation. |
| 205 | + |
| 206 | +:::info Thought on AI code generation era |
| 207 | + |
| 208 | +#### Building web app is not that easy |
| 209 | + |
| 210 | +- Solid coding experience is required, from programming languages to web frameworks and libraries. You need a lot of hands-on experience to build software with confidence. |
| 211 | + |
| 212 | +- Even though AI code generation is pretty helpful, you still need expertise in coding if you're not going to build a toy project. |
| 213 | + |
| 214 | +- Finishing the code is just the beginning; deploying, observability, security, there are a bunch of things waiting for you. AI is not that helpful for these right now. |
| 215 | + |
| 216 | +#### Code Generation: Just the Tip of the Iceberg |
| 217 | + |
| 218 | +- For any serious piece of software that makes it to production, 80% or more of the cost is in the phase after initial development. That's the phase called Day-2 operations. |
| 219 | + |
| 220 | +- Day-2 operations present the biggest challenges. |
| 221 | + |
| 222 | +- Can Cursor and WindSurf solve the deployment problem, make the whole Day-2 easy and effortless? No, I don't think so, neither can Devin. |
| 223 | + |
| 224 | +::: |
| 225 | + |
| 226 | + |
| 227 | +## Best practice first |
| 228 | + |
| 229 | +I hope I don't need to learn serveral methods to compare and figure out which one is the best practice. What I hope I only need one solution which is the best practice, free myself from such a choose struggeling. |
| 230 | + |
| 231 | +AIScript choose Rust's best practice of web developement, and encapsulate those best practices into the language and framework. |
| 232 | + |
0 commit comments