Skip to content

Commit 65a0fc1

Browse files
authored
Merge pull request #336 from DefangLabs/linda-nounly-go
Nounly Sample
0 parents  commit 65a0fc1

File tree

9 files changed

+934
-0
lines changed

9 files changed

+934
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
FROM mcr.microsoft.com/devcontainers/typescript-node:22-bookworm

.devcontainer/devcontainer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}

.github/workflows/deploy.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Node.js & OpenAI
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-nodejs-openai-template%26template_owner%3DDefangSamples)
4+
5+
A simple Node.js and OpenAI app deployed using Defang.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
23+
24+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
25+
26+
### `OPENAI_KEY`
27+
An API key to access the OpenAI API.
28+
```bash
29+
defang config set OPENAI_KEY
30+
```
31+
32+
## Testing
33+
Below are some useful commands for testing.
34+
35+
```bash
36+
echo "Hello" | curl -H "Content-Type: text/plain" -d @- https://xxxxxxxx/prompt
37+
```
38+
39+
or alternatively,
40+
41+
```bash
42+
cat prompt.txt | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
43+
```
44+
45+
## Deployment
46+
47+
> [!NOTE]
48+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
49+
50+
### Defang Playground
51+
52+
Deploy your application to the Defang Playground by opening up your terminal and typing:
53+
```bash
54+
defang compose up
55+
```
56+
57+
### BYOC (AWS)
58+
59+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud):
60+
61+
---
62+
63+
Title: Node.js & OpenAI
64+
65+
Short Description: A simple Node.js application that interacts with the OpenAI API.
66+
67+
Tags: Node.js, OpenAI, API, JavaScript
68+
69+
Languages: nodejs

app/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use the slim version of Node.js on Debian Bookworm as the base image
2+
FROM node:20-bookworm-slim
3+
4+
RUN apt-get update -qq \
5+
&& apt-get install -y curl \
6+
&& apt-get clean \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
# Set the working directory to /app
10+
WORKDIR /app
11+
12+
# Copy package.json and package-lock.json into the container at /app
13+
COPY package*.json ./
14+
15+
# Install any needed packages specified in package.json using npm ci for a clean install
16+
RUN npm ci
17+
18+
# Copy the current directory contents into the container at /app
19+
COPY . .
20+
21+
# Make port 3000 available to the world outside this container
22+
EXPOSE 3000
23+
24+
# Run the app when the container launches
25+
ENTRYPOINT ["node", "main.js"]

app/main.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require('express');
2+
const axios = require('axios');
3+
const bodyParser = require('body-parser');
4+
5+
const app = express();
6+
app.use(bodyParser.text());
7+
8+
app.get('/', (req, res) => {
9+
res.json({status: 'ok'});
10+
});
11+
12+
app.post('/prompt', async (req, res) => {
13+
const promptText = req.body;
14+
15+
const messages = [
16+
// {role: 'system', content: 'You are an experienced software engineer.'},
17+
{role: 'user', content: promptText},
18+
];
19+
20+
const apiKey = process.env.OPENAI_KEY;
21+
const url = 'https://api.openai.com/v1/chat/completions';
22+
23+
const postBody = {
24+
model: 'gpt-3.5-turbo',
25+
messages: messages,
26+
};
27+
28+
const headers = {
29+
'Content-Type': 'application/json',
30+
'Authorization': `Bearer ${apiKey}`
31+
};
32+
33+
try {
34+
const response = await axios.post(url, postBody, {headers: headers});
35+
res.json({status: 'ok', response: response.data});
36+
} catch (error) {
37+
console.error(error);
38+
res.status(500).json({status: 'error', message: error.message});
39+
}
40+
});
41+
42+
app.listen(3000, () => {
43+
console.log('Server running on port 3000');
44+
});

0 commit comments

Comments
 (0)