Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dotenv": "^11.0.0",
"express": "^4.17.2",
"isomorphic-fetch": "^3.0.0",
"prompt-engine": "^0.0.21",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
Expand Down
109 changes: 0 additions & 109 deletions src/server/Context.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/server/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import path from "path";
import { context, getCompletion } from "./model";
import { promptEngine, getCompletion } from "./model";
import cors from "cors";

const app = express();
Expand All @@ -23,10 +23,10 @@ app.post("/codegen", async (req, res) => {

// Gets natural language and returns code
app.get("/reset", async (_req, res) => {
context.resetContext();
promptEngine.resetContext();
res.send(
JSON.stringify({
context: context.getContext()
context: promptEngine.buildContext()
})
);
});
Expand Down
121 changes: 68 additions & 53 deletions src/server/contexts/context1.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,84 @@
export const baseContext = `/* This document contains a BabylonJS scene, natural language commands and the BabylonJS code needed to accomplish them */

state = {};

/* Make a cube */
state.cube = BABYLON.MeshBuilder.CreateBox("cube", {size: 1}, scene);

/* Move the cube up */
state.cube.position.y += 1;

/* Move it to the left */
state.cube.position.x -= 1;

/* Make the block teal */
state.cube.material = new BABYLON.StandardMaterial("mat", scene);
state.cube.material.diffuseColor = new BABYLON.Color3(0, 1, 1);

/* Now make it spin */
state.intervals["spinningCubeInterval"] = setInterval(() => {
export const promptDescription = "This document contains a BabylonJS scene, natural language commands and the BabylonJS code needed to accomplish them";

export const promptExamples = [
{
input: `Initial state`,
response: `state = {};`
},
{
input: `Make a cube`,
response: `state.cube = BABYLON.MeshBuilder.CreateBox("cube", {size: 1}, scene);`
},
{
input: `Move the cube up`,
response: `state.cube.position.y += 1;`
},
{
input: `Move it to the left`,
response: `state.cube.position.x -= 1;`
},
{
input: `Make the block teal`,
response: `state.cube.material = new BABYLON.StandardMaterial("mat", scene);
state.cube.material.diffuseColor = new BABYLON.Color3(0, 1, 1);`
},
{
input: `Now make it spin`,
response: `state.intervals["spinningCubeInterval"] = setInterval(() => {
scene.meshes[0].rotation.y += 0.02
}, 10);

/* Make it stop */
clearInterval(state.intervals.spinningCubeInterval);

/* Make it change color when the mouse is over it */
state.cube.actionManager = new BABYLON.ActionManager(scene);

}, 10);`
},
{
input: `Make it stop`,
response: `clearInterval(state.intervals.spinningCubeInterval);`
},
{
input: `Make it change color when the mouse is over it`,
response: `state.cube.actionManager = new BABYLON.ActionManager(scene);
state.hoverAction = new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPointerOverTrigger, function () {
state.cube.material = new BABYLON.StandardMaterial("mat", scene);
state.cube.material.diffuseColor = new BABYLON.Color3(1, 0, 0);
});

state.unHoverAction = new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPointerOutTrigger, function () {
state.cube.material = new BABYLON.StandardMaterial("mat", scene);
state.cube.material.diffuseColor = new BABYLON.Color3(0, 1, 1);
});

state.cube.actionManager.registerAction(state.hoverAction);
state.cube.actionManager.registerAction(state.unHoverAction);

/* Put a sphere on top of the cube */
state.sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 1}, scene);
state.sphere.position.y = 1;

/* Delete the sphere and the cube */
state.sphere.dispose();
state.cube.dispose();

/* make 50 cubes side by side */
state.cubes = [];
state.cube.actionManager.registerAction(state.unHoverAction);`
},
{
input: `Put a sphere on top of the cube`,
response: `state.sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 1}, scene);
state.sphere.position.y = 1;`
},
{
input: `Delete the sphere and the cube`,
response: `state.sphere.dispose();
state.cube.dispose();`
},
{
input: `make 50 cubes side by side`,
response: `state.cubes = [];
for (let i = 0; i < 50; i++) {
state.cubes[i] = BABYLON.MeshBuilder.CreateBox("cube", {size: 1}, scene);
state.cubes[i].position.x = i;
}

/* stack them like stairs */
for (let i = 0; i < 50; i++) {
}`
},
{
input: `stack them like stairs`,
response: `for (let i = 0; i < 50; i++) {
state.cubes[i].position.y = i;
}

/* remove them */
for (let i = 0; i < 50; i++) {
}`
},
{
input: `remove them`,
response: `for (let i = 0; i < 50; i++) {
state.cubes[i].dispose();
}
}`
},
{
input: `make the background red`,
response: `scene.clearColor = new BABYLON.Color3(1, 0, 0);`
}
];

/* make the background red */
scene.clearColor = new BABYLON.Color3(1, 0, 0);
`;
17 changes: 7 additions & 10 deletions src/server/model.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
require("dotenv").config();
import fetch from "isomorphic-fetch";

import {CodeEngine} from 'prompt-engine'
// Contains the helper methods for interacting with Codex and crafting model prompts
import { baseContext } from "./contexts/context1";
import Context from "./Context";
import { promptDescription, promptExamples } from "./contexts/context1";
import { detectSensitiveContent } from "./contentFiltering";

const maxPromptLength = 3200;

// CURRENTLY SINGLE TENANT - WOULD NEED TO UPDATE THIS TO A MAP OF TENANT IDs TO PROMPTS TO MAKE MULTI-TENANT
export const context = new Context(baseContext);
export const promptEngine: CodeEngine = new CodeEngine(promptDescription, promptExamples, {
maxTokens: maxPromptLength,
});

export async function getCompletion(command: string) {
let prompt = context.getPrompt(command);

if (prompt.length > maxPromptLength) {
context.trimContext(maxPromptLength - command.length + 6); // The max length of the prompt, including the command, comment operators and spacing.
}
let prompt = promptEngine.buildPrompt(command);

// To learn more about making requests to OpanAI API, please refer to https://beta.openai.com/docs/api-reference/making-requests.
// Here we use the following endpoint pattern for engine selection.
Expand Down Expand Up @@ -65,7 +62,7 @@ export async function getCompletion(command: string) {
}
else {
//only allow safe interactions to be added to the context history
context.addInteraction(command, code);
promptEngine.addInteraction(command, code);
}

return {
Expand Down