The New Computer: Use Serverless to Build Your First AI-OS App
Tags: ai, serverless, chatgpt, programming
Published: 2024-02-01 12:46:55

There is no denying some really interesting and groundbreaking things are cooking over at OpenAI.

Why do I say that? The reason is that in recent months they have started to release some things many people didn't fully expect. I believe this is a sign that internally, OpenAI is currently executing on an overarching plan that over the coming years will change the digital landscape completely. 

What are some of these things they have released? Examples include `GPTs`, `GPT Actions`, and most recently: `GPT @-mentions`.

![GPTs @ mentions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9x9awb9mdpct8kgzco4.jpg)

This is simply a way to reference your GPTs—AI chatbots that you can customize on your own—in your current ChatGPT conversation.

Well, you might say, that doesn't sound like such a big deal? And why is so much time being spent on these GPTs? Are they even any good?

Let me show you why it is a big deal.

## The Dawn Of A New Computer

Back in the day, there was a little company called Microsoft that revolutionized personal computing. Founded in 1975 by Bill Gates and Paul Allen, Microsoft achieved its big break with MS-DOS, an operating system developed for the IBM PC in 1981. This success paved the way for Windows, which became the dominant operating system worldwide.

Seizing the opportunity in the flourishing era of personal computing, Microsoft's strategic innovations and market adaptability turned it into a tech juggernaut.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f3bqfxh5ji1q3fmfu7j4.png" alt="OpenAI hypothetical LLM OS">
  <figcaption>Image of an hypothetical LLM OS by [Andrej Karpathy](https://twitter.com/karpathy?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor), working at OpenAI.</figcaption>
</figure>

I believe that what Microsoft did with the release of Windows 1.0 back in 1985, is what OpenAI is gearing up to do in 2024 and beyond: creating a new kind of AI-OS for the next generation of personal computers. This could be as pivotal for our digital interactions as when Bill and Paul revolutionized computing with Windows.

## GPTs as AI-OS Apps

So essentially, these GPTs are to the AI-OS, what traditional applications were to Windows; instead of launching an app on your computer, you will be orchestrating AI agents to perform actions on your behalf. And it will be so much more fun and engaging than pressing down 👇🏻 keys on a board or other pieces of plastic. 

Instead of being on your own as in the days of PC's past, as I described in a [previous article](https://dev.to/dawiddahl/meet-your-future-co-workers-the-rise-of-ai-agents-in-the-office-441m), you will instead be collaborating directly with a host of artificially intelligent beings, not at all unlike how Luke Skywalker is dealing with C-3PO in Star Wars.

![C-3PO from Star Wars](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obvi46b6gfueg0l1mi95.jpeg)

But how do you actually create one of these new AI-OS apps? In the next section, I'll guide you through the process using AI to help you build a ([AI Application Value Level 2](https://dev.to/dawiddahl/climbing-the-ai-application-value-ladder-4cf0#value-level-2-function-calling
)) `GPT Action`, using serverless functions technology. 

The most common way of building a `GPT Action` today, if you look on ChatGPT-related YouTube content, is Zapier: a no-code platform allowing you to perform actions like sending email or updating your calendar. By using serverless functions instead, you actually won't need to pay Zapier a subscription fee every month!

>ℹ️ 1. Even though being a developer helps when building serverless functions, with the help of AI (and a little grit), it's not strictly necessary, as you can learn as you go.
>
>ℹ️ 2. Even though it is called “serverless”, that doesn’t mean there is no server. It just means that we don’t use our own local server; we use some other company’s server in the ☁️.

## Using Serverless Functions to Create Your First Proto AI-OS-App

So what shall we build? As a proof-of-concept, let's go for an AI-OS app that should, on the server, generate some ASCII art of a cow that says something. Like this:

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89ats08pjltcpt7jxph3.png" alt="ASCII art cow">
  <figcaption>To create the ASCII art, we'll use `cowsay` on the server, which is an [external library](https://www.npmjs.com/package/cowsay) designed for this cowsome purpose.</figcaption>
</figure>

Then that art should be sent from the server back to the AI-OS app (our GPT), which will then create a beautiful painting drawing inspiration from this ASCII art.

>You will need 1) a [ChatGPT](https://chat.openai.com/) Plus or Teams account, 2) a free [Vercel](https://vercel.com/) account and 3) a free [GitHub](https://github.com) account to build along with me.

### Step 1: Set Up The ☁️ Environment

Open up ChatGPT and ask it to generate a serverless function on Vercel.

To get started, use this prompt:

"_Could you carefully guide me through creating a serverless function with Vercel using Node, starting by setting up a Next.js project using create-next-app, then writing a basic serverless function in TypeScript, and finally deploying it via the Vercel CLI? Please also explain step-by-step how we link the Vercel project to GitHub_."

If you prefer a written guide, you can use [this](https://vercel.com/docs/functions/serverless-functions/quickstart). To see or clone my finished serverless function repository on GitHub, click [here](https://github.com/dawid-dahl-umain/gpt-functions-cowsay/tree/main).

>Vercel's Hobby Plan offers _free_ serverless functions for small projects, with up to 10-second runtime and ample monthly capacity of 100 GB-hours. That means a simple function can be run around 700,000 times a month, for free! No need to pay Zapier every month.
>
>More info on pricing [here](https://vercel.com/docs/accounts/plans/hobby).

### Step 2: Create The Function 🛠️

Now you should have a Next project. Inside the `app` folder, there is an `api` folder. Inside that folder, create a new folder and call it something that should be thought of as a spell 🪄✨ we use to activate our function. Let's go for `gpt-functions-cowsay`, or whatever you'd like. Remember this spell name, we will need it later.

Next, in this spell folder, create a file called `route.ts`. The folder structure will thus be: `app/api/gpt-functions-cowsay/route.ts`.

>If at any point you feel lost, no worries! Just ask ChatGPT for clarification or help.

Now, request ChatGPT to write the server-side code if it didn't already, to generate a cowsay and return the result. Use this prompt to get started:

"_I need help creating a simple serverless function in Next.js that uses the 'cowsay' package. The function should take text from a URL search parameter, make a cow say it, and return this along with the request. Can you guide me through the steps, including necessary TypeScript code, to set up this function?_"

If the AI does its job, the code for the function will end up something like this.

```typescript
import { NextResponse } from "next/server";
import { say } from "cowsay";
import type { NextRequest } from "next/server";

export const GET = (request: NextRequest) => {
  const cowsayText = request.nextUrl.searchParams.get("cowsay") || "";

  return NextResponse.json(
    {
      cowsay: say({ text: cowsayText }),
    },
    {
      status: 200,
    }
  );
};
```

Paste this code into the `route.ts` file in the spell folder (`gpt-functions-cowsay`).

Please note that although this function performs a simple task, in reality, within this server environment, you now wield the **full power of software engineering**.

That's right. Unlike with Zapier where you are restricted to follow their rules, in here, you can build _any_ tool you want. And through the `Actions` input the GPTs creation editor, you can hand this tool over to the AI for it to use on your behalf. 

Take a moment and just reflect on the vast possibilities. **The sk-AI is the limit!**

### Step 3: Create An OpenAPI Spec 📄

Now, the way we make our GPT aware of our new function so it can use it, is to hand it something called an OpenAPI specification.

>Yes, that was not a typo. While OpenAI is the company, [OpenAPI](https://swagger.io/specification/) is a rulebook for how computer programs talk to each other (APIs).

If you are not a developer, you will have no idea how to write such a specification. But fear not, you can use another GPT called [ActionsGPT](https://chat.openai.com/g/g-TYEliDU6A-actionsgpt) to do it for you.

![Add actions button in gpt editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf3q6kk1q6cq0kvnn38l.png)

- In the configuration tab of the GTP creator, click the "Create new action" button.
- In a separate ChatGPT thread, @-mention `ActionsGPT`.

![@ mentioning a gpt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjzbzvow0crtaltx4rlv.png)

- Ask it: "_I have set up a serverless function in Vercel. What should I do now to get an OpenAPI specification from you?_" You could hand it some of the code too.
- `ActionsGPT` will tell you to hand it some information.
- You will give it something like this. (The Base URL you get from your Vercel project.) The prompt doesn't have to be exact, just get the urls and the `GET` or `POST` right and describe what your function does. Use this prompt to get started:

>"_Endpoint URL(s): gpt-functions-cowsay
HTTP Methods: GET
Base URL: gpt-functions-cowsay.vercel.app_
>
>_When given an input called cowsay, it will take it and make a cowsay out of it. Then it will return the cowsay._"

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9lnghot7bkt1xl8d0kn.png" alt="abra cadabra spell function invocation">
  <figcaption>In Aramaic, "_avra kehdabra_" means "_I will create as I speak_". If `gpt-functions-cowsay` is the _kadabra_, `GET` is the _abra_. Using them both together will cast the function's magic! ✨</figcaption>
</figure>

- `ActionsGPT` will then generate the OpenAPI spec for you.

### Step 4: Launch Your GPT! 🚀

Finally, paste the OpenAI specification into the Schema input of the GPT Actions editor. Like this:

![GPT actions configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eolvcloqyh29moqzpxu4.png)

If you encounter errors, consult `ActionsGPT` with your serverless function code at hand. Iteration is key in when building with AI.

> Use this free [privacy policy generator](https://app.freeprivacypolicy.com/wizard/privacy-policy) to create a policy for the GPT action, in case you want your GPT to be public.

### Step 5: You're done! ✅

That's it, if OpenAI allows you to save this GPT, that means you did it - you just built your first simple AI-OS app! 👏🏻

This might've seemed daunting, especially for non-developers. And don't worry if you couldn't get it to work on your first try. Because remember, adding an action to a GPT is a [Level 2](https://dev.to/dawiddahl/climbing-the-ai-application-value-ladder-4cf0#value-level-2-function-calling
) task in AI software development — it's supposed to be a bit on the tougher side! But also more rewarding and fun to build, if you ask me.

![Cow congratulating you](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqre84np7y5blzp85zw5.png)

## Conclusion

In this guide, you've learned how to create an AI-OS app using serverless technology with our `cowsay` example. This introductory project showcases the potential for building some truly innovative AI applications.

If you didn't follow along and build it with me, here is the cowtastic [Cowsay Creator](https://chat.openai.com/g/g-z1SLp6C5w-cowsay-creator) in action!

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsx2su80wztzhsrtiubd.png" alt="gpt allow button for actions">
  <figcaption>It is all right to press "_Allow_" here. You can check the [Github repo](https://github.com/dawid-dahl-umain/gpt-functions-cowsay) to verify that apart from bad cow art, nothing else bad happens in our serverless action.</figcaption>
</figure>

OpenAI's latest developments hint at a major shift, similar to when Windows first changed computing. We're seeing the start of a new AI-OS that could change everything, indicating that a future with C-3PO-like companions might be closer than we anticipate.

And while our `Cowsay Creator` GPT was just for fun and practice, by exploring this, you're already a part of the emerging AI-OS future. Who knows what actually valuable AI-OS apps you'll create next!

---

Dawid Dahl is a full-stack developer at [UMAIN](https://www.umain.com/) | [ARC](https://arc.inc/). In his free time, he enjoys metaphysical ontology, analog synthesizers, consciousness, Huayan and Madhyamika Prasangika philosophy, and being with friends and family.

>For those keen to dive deeper into `function calling` with LLMs, in [this article](https://dev.to/dawiddahl/function-calling-the-most-significant-ai-feature-since-chatgpt-itself-81m) I offer another thorough exploration of the topic.