Skip to content

Commit

Permalink
Update to use configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Radu Matei <radu.matei@fermyon.com>
  • Loading branch information
radu-matei committed Jun 18, 2023
1 parent ec19c23 commit aeea69e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ router.post("/api/generate", async (_req, extra) => {
// Open the default KV store.
let kv = spinSdk.kv.openDefault();
let configuration = new Configuration({
apiKey: decoder.decode(kv.get("openai_key")),
apiKey: spinSdk.config.get("openai_key")
});

let openai = new OpenAIApi(configuration);
Expand Down
9 changes: 5 additions & 4 deletions spin.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
spin_version = "1"
authors = ["Radu Matei <radu.matei@fermyon.com>"]
description = "Simple Spin application calling OpenAI APIs"
name = "openai-demo2"
name = "chatgpt"
trigger = { type = "http", base = "/" }
version = "0.1.0"

[variables]
openai_key = { required = true }

openai_key = { required = true, secret = true }

# Chat UI
[[component]]
Expand All @@ -16,6 +15,7 @@ id = "web"
files = [{ source = "web", destination = "/" }]
[component.trigger]
route = "/..."
watch = ["index.html", "main.js", "styles.css"]

# API
[[component]]
Expand All @@ -30,10 +30,11 @@ route = "/api/..."
[component.build]
command = "npm run build"
workdir = "api"
watch = ["src/index.ts", "package.json"]

# Spin KV explorer component
[[component]]
source = { url = "https://github.com/radu-matei/spin-kv-explorer/releases/download/v0.5.0/spin-kv-explorer.wasm", digest = "sha256:d5f9e1f6b61b90f7404e3800285f7860fe2cfc7d0116023efc370adbb403fe87" }
source = { url = "https://github.com/radu-matei/spin-kv-explorer/releases/download/v0.9.0/spin-kv-explorer.wasm", digest = "sha256:07f5f0b8514c14ae5830af0f21674fd28befee33cd7ca58bc0a68103829f2f9c" }
id = "kv-explorer"
# add or remove stores you want to explore here
key_value_stores = ["default"]
Expand Down
4 changes: 2 additions & 2 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function typeMessage(speaker, message) {
// Function to handle sending a message to the OpenAI API
async function sendMessageToAPI(id, message) {
let response = await fetch("/api/generate", { method: "POST", body: JSON.stringify({ id: id, message: message }) });
typeMessage("AI", await response.text());
typeMessage("OpenAI", await response.text());
}

// Event listener for send button click
Expand Down Expand Up @@ -123,7 +123,7 @@ function notify(message) {
const toggleDocsButton = document.getElementById('toggle-docs');
const docsSection = document.querySelector('.documentation');

toggleDocsButton.addEventListener('click', function () {
toggleDocsButton.addEventListener('click', function() {
if (docsSection.style.display === "none") {
docsSection.style.display = "block";
} else {
Expand Down

0 comments on commit aeea69e

Please sign in to comment.