LLM Development with JavaScript: Is that a thing?
Tags: javascript, node, beginners, openai
Published: 2024-03-07 21:20:06

This tutorial is a fast track to developing JavaScript apps that talk to LLM models. You'll have a REST service up and talking to an LLM in under 10 minutes.  Let the coding magic begin!

---

This is part 1 from my free e-book:

[The Busy Developers Guide to Generative AI](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Fjorshali%2Fai-for-developers%2Fblob%2Fmain%2Fbusy_developers_guide_to_genai.pdf)


![Fig 1: The Busy Developers Guide to Gen AI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ex5kha4h8qpa4t67inrc.png)


All source code is available on [GitHub](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Fjorshali%2Fai-for-developers).

---

ChatGPT vaulted generative AI into mainstream culture. But, it's really just a user interface, powered by the true marvel that lies beneath - the large language model (LLM). 

More precisely, LLMs are very large deep learning models that are pre-trained on vast amounts of data. The keyword there is pre-trained. 

All we have to do to make use of these same models is send them a prompt telling it what we want. We can do that by calling the OpenAI APIs.

![Fig 2: Your REST service can call the same APIs used by ChatGPT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdanmfblu2djwndjrrq4.png)


## 1. Install Node

Download and install: [https://nodejs.org](https://nodejs.org). Verify the install in your terminal:

```
~ % node -v
```

If the installation succeeded, the version will print.

## 2. Initialize your project

Create a new directory for your project. Navigate to it in your terminal and run the following command:

```
~/ai-for-devs % npm init -y
```

This creates a new package.json file, initializing the project.

3. Install Node modules

The node modules we'll be using:

- **express**: which makes server creation quick and easy
- **langchain**: which provides a framework for building Apps with LLMs
- **@langchain/openai**: which provides OpenAI integrations through their SDK
- **cors**: Express middleware to enable CORS

In the same terminal, run the following command:

```
~/ai-for-devs % npm install express langchain @langchain/openai cors
```

## 4. Create the server file

Create a file called `server.mjs` in the project directory. Open it in a text editor and add the following lines of code:

```
import express from "express";
import { ChatOpenAI } from "@langchain/openai";
import cors from 'cors';

const app = express();

app.use(cors());

const chatModel = new ChatOpenAI({});

app.get('/', async (req, res) => {
  const response = 
    await chatModel.invoke(
      "Can you simply say 'test'?");

  res.send(response.content);
});

app.listen(3000, () => {
  console.log(`Server is running on port 3000`);
});
```

## 5. Create an OpenAI account

Register here: [https://platform.openai.com](https://platform.openai.com).  Obtain an API key:

- Simply select 'API keys' in the upper left navigation
- Select '+ Create new secret key'
- Copy the key somewhere safe for now

![Fig 3: The API keys in Open AI's interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks4erkz00g526kjtclqx.png)


## 6. Set an environment variable

In the same terminal, run the following command with your key value:

```
~/ai-for-devs % export OPENAI_API_KEY=<YOUR_KEY_VALUE>
```

_Optionally_ add this command to your bash profile: `~/.zshrc`

## 7. Launch your server

Back in the terminal, run the following command:

```
~/node-openai % node server.mjs
```

Open your web browser and visit: [http://localhost:3000](http://localhost:3000)

You'll see the response from the OpenAI model: "test"

## Congratulations!

You've successfully built a functional REST service. Beyond its ability to prompt an AI and generate responses, it forms the foundation for the remainder of my free to download e-book:

[The Busy Developer's Guide to Gen AI](https://github.com/jorshali/ai-for-developers/blob/main/busy_developers_guide_to_genai.pdf)

In Part 2, we'll explore the process of streaming longer responses so our users don't have to wait. Part 3 and part 4 will guide you through creating a complete RAG (Retrieval Augmented Generation) implementation.

Download the book to learn more!