Skip to content

Commit 836682b

Browse files
authored
Merge pull request #173 from DefangLabs/172-convert-all-samples-to-use-the-defang-gh-action-from-marketplace
Switch all samples to use the defang gh action
0 parents  commit 836682b

File tree

7 files changed

+880
-0
lines changed

7 files changed

+880
-0
lines changed

.github/workflows/deploy.yaml

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

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Node.js & OpenAI
2+
3+
## Setup
4+
5+
This sample requires an API key to access the OpenAI API. The name of the config value is referenced in the compose.yaml file.
6+
To provide a value for it, you can use the Defang CLI like this:
7+
8+
```
9+
10+
defang config set --name OPENAI_KEY
11+
```
12+
13+
and then enter the value when prompted.
14+
15+
## Testing
16+
17+
```
18+
echo "Hello" | curl -H "Content-Type: text/plain" -d @- https://xxxxxxxx/prompt
19+
```
20+
21+
or
22+
23+
```
24+
cat prompt.txt | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
25+
```
26+
27+
---
28+
29+
Title: Node.js & OpenAI
30+
31+
Short Description: A simple Node.js application that interacts with the OpenAI API.
32+
33+
Tags: Node.js, OpenAI, API, JavaScript
34+
35+
Languages: nodejs

app/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Node runtime based on Alpine as a parent image
2+
FROM node:20-alpine
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json into the container at /app
8+
COPY package*.json ./
9+
10+
# Install any needed packages specified in package.json
11+
RUN npm ci
12+
13+
# Copy the current directory contents into the container at /app
14+
COPY . .
15+
16+
# Make port 3000 available to the world outside this container
17+
EXPOSE 3000
18+
19+
# Run the app when the container launches
20+
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)