Skip to content

Commit

Permalink
Merge pull request #48 from Flow-Works/liquid-env
Browse files Browse the repository at this point in the history
✨ feature: FlowGPT
  • Loading branch information
ThinLiquid authored Jul 23, 2023
2 parents eb23d0e + 48c00bd commit 5179faf
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_KEY=fTAHXJ2_JgRsP9nVSnpZPQgoOIfQlmG7G5Br5A9SbKU
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"name": "yarn",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
"start"
],
"cwd": "${workspaceRoot}",
"timeout": 10000
Expand Down
25 changes: 25 additions & 0 deletions FlowOS/public/builtin/apps/flowgpt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />

<link rel="stylesheet" href="/styles/style.css" />
<link rel="stylesheet" href="/styles/window.css" />

<link rel="stylesheet" href="/builtin/apps/styles/flowgpt.css" />
</head>

<body>
<div class="flowgpt">
<div class="fill">
</div>
<form>
<input/>
<button>Send</button>
</form>
</div>

<script src="/scripts/app.js" defer type="module"></script>
<script src="/builtin/apps/scripts/flowgpt.js" defer type="module"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions FlowOS/public/builtin/apps/scripts/flowgpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-env browser */

import { marked } from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';

const logs = [];
document.querySelector('form').onsubmit = async (e) => {
e.preventDefault();

const you = new ChatLog('You');
const gpt = new ChatLog('FlowGPT');

const value = document.querySelector('input').value;
document.querySelector('input').value = '';

document.querySelector('button').disabled = true;
logs.push({ role: 'user', content: value });
you.set(value);

const res = await fetch('http://localhost:3000' + '/ai/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ logs: [ ...logs, { role: 'user', content: value } ] })
});

const data = await res.json();

logs.push(data);
gpt.set(data.content);
document.querySelector('button').disabled = false;
};

class ChatLog {
constructor(name) {
this.name = name;
this.div = document.createElement('div');
this.div.innerHTML += `<b>${name}</b>: <img src="/assets/loading.gif" style="position:relative;top:2.5px;" width="15px"/><br/><br/>`;
document.querySelector('.fill').appendChild(this.div);
}

set = (content) => {
this.div.innerHTML = `<b>${this.name}</b>: ${marked.parse(content)}<br/><br/>`;
};
};
32 changes: 32 additions & 0 deletions FlowOS/public/builtin/apps/styles/flowgpt.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.flowgpt {
height: 100%;
display: flex;
flex-direction: column;
}

.fill {
flex: 1;
}

form {
display: flex;
gap: 10px;
}

form textarea {
flex: 1;
}

form button {
width: unset;
}

p {
display: inline;
}

code {
background: var(--surface-bg);
padding: 2px;
border-radius: 2px;
}
1 change: 1 addition & 0 deletions FlowOS/public/constants/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const apps = () => {
'emulator': new AppData('emulator', 'Emulator', '/emu/', '/assets/icons/gameboy.svg', false),
'app-store': new AppData('app-store', 'App Store', '/builtin/apps/apps.html', '/assets/icons/applications-apps.svg', false),
'app-wizard': new AppData('app-wizard', 'Custom Application Wizard', '/builtin/apps/app-wizard.html', '/assets/icons/applications-programming.svg', false),
'flowgpt': new AppData('flowgpt', 'FlowGPT', '/builtin/apps/flowgpt.html', '/assets/icons/chat.svg', false),
...config.apps.get(),
...config.customApps.get(),
};
Expand Down
2 changes: 1 addition & 1 deletion FlowOS/public/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { config } from './scripts/managers.js';
import apps from './constants/apps.js';

export default class FlowInstance {
version = 'v1.0.5-beta';
version = 'v1.0.6-beta';
init = false;
setup = false;

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flow-os",
"version": "1.0.5-beta",
"version": "1.0.6-beta",
"description": "The customizable webOS.",
"keywords": [
"tomp",
Expand All @@ -24,8 +24,10 @@
"@proudparrot2/uv": "^2.0.0",
"@sysce/stomp": "^2.0.0",
"@tomphttp/bare-server-node": "^2.0.1",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"fastify": "^4.19.2"
"fastify": "^4.19.2",
"openai": "^3.3.0"
},
"overrides": {
"word-wrap": "^1.2.4"
Expand Down
33 changes: 33 additions & 0 deletions src/ai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Configuration, OpenAIApi } from 'openai';
import 'dotenv/config';

const configuration = new Configuration({
apiKey: process.env.API_KEY,
basePath: 'https://chimeragpt.adventblocks.cc/v1'
});

const openai = new OpenAIApi(configuration);

const ai = (app, opts, done) => {
app.addContentTypeParser('application/json', { parseAs: 'string' }, (req, body, done) => {
try {
const json = JSON.parse(body);
done(null, json);
} catch (err) {
err.statusCode = 400;
done(err, undefined);
}
});

app.post('/chat', async (req, res) => {
const chatCompletion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: req.body.logs,
});
return res.send(chatCompletion.data.choices[0].message);
});

done();
};

export default ai;
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import fastifyCompress from '@fastify/compress';
import fastifyCaching from '@fastify/caching';

import { createBareServer } from '@tomphttp/bare-server-node';
import { stompPath } from '@sysce/stomp';
Expand All @@ -13,6 +12,8 @@ import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';

import ai from './ai.js';

const port = process.env.PORT || 3000;

let server;
Expand Down Expand Up @@ -50,12 +51,7 @@ app.register(
{ threshold: 1 }
);

if (process.env.NODE_ENV == 'production') {
app.register(
fastifyCaching,
{ privacy: fastifyCaching.privacy.PUBLIC, expiresIn: 3600 },
);
}
app.register(ai, { prefix: '/ai/' });

app.register(fastifyStatic, {
root: uvPath,
Expand Down
53 changes: 52 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ async-exit-hook@^2.0.1:
resolved "https://registry.npmjs.org/async-exit-hook/-/async-exit-hook-2.0.1.tgz"
integrity sha512-NW2cX8m1Q7KPA7a5M2ULQeZ2wR5qI5PAbw5L0UOMxdioVk9PMZ0h1TmyZEkPYrCvYjDlFICusOu1dlEKAAeXBw==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

at-least-node@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz"
Expand All @@ -546,6 +551,13 @@ avvio@^8.2.1:
debug "^4.0.0"
fastq "^1.6.1"

axios@^0.26.0:
version "0.26.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
dependencies:
follow-redirects "^1.14.8"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
Expand Down Expand Up @@ -777,6 +789,13 @@ color-name@~1.1.4:
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

commander@^10.0.1:
version "10.0.1"
resolved "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz"
Expand Down Expand Up @@ -949,6 +968,11 @@ defaults@^1.0.3:
dependencies:
clone "^1.0.2"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

[email protected]:
version "2.0.0"
resolved "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz"
Expand Down Expand Up @@ -986,6 +1010,11 @@ dotenv@^16.0.3:
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz"
integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==

dotenv@^16.3.1:
version "16.3.1"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e"
integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==

duplexify@^3.5.0:
version "3.7.1"
resolved "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz"
Expand Down Expand Up @@ -1428,6 +1457,20 @@ flatted@^3.1.0:
resolved "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.14.8:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

[email protected]:
version "0.2.0"
resolved "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz"
Expand Down Expand Up @@ -2095,7 +2138,7 @@ [email protected], mime-db@^1.51.0:
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@~2.1.24, mime-types@~2.1.34:
mime-types@^2.1.12, mime-types@~2.1.24, mime-types@~2.1.34:
version "2.1.35"
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
Expand Down Expand Up @@ -2269,6 +2312,14 @@ onetime@^5.1.0:
dependencies:
mimic-fn "^2.1.0"

openai@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/openai/-/openai-3.3.0.tgz#a6408016ad0945738e1febf43f2fccca83a3f532"
integrity sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==
dependencies:
axios "^0.26.0"
form-data "^4.0.0"

optionator@^0.8.1:
version "0.8.3"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.3.tgz#84fa1d036fe9d3c7e21d99884b601167ec8fb495"
Expand Down

0 comments on commit 5179faf

Please sign in to comment.