Skip to content

Commit 774fe65

Browse files
committed
[chapter8] smart-cms update
Signed-off-by: danbugs <[email protected]>
1 parent 672c2c3 commit 774fe65

File tree

3 files changed

+261
-0
lines changed

3 files changed

+261
-0
lines changed

chapter08/smart-cms/index.html

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Smart CMS</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
max-width: 600px;
10+
margin: 20px auto;
11+
text-align: center;
12+
}
13+
input, button, textarea {
14+
margin: 10px 0;
15+
padding: 8px;
16+
width: 100%;
17+
max-width: 500px;
18+
font-size: 16px;
19+
}
20+
button {
21+
cursor: pointer;
22+
}
23+
.output {
24+
margin-top: 20px;
25+
padding: 10px;
26+
border: 1px solid #ccc;
27+
border-radius: 5px;
28+
background: #f9f9f9;
29+
white-space: pre-wrap;
30+
display: none;
31+
}
32+
</style>
33+
</head>
34+
<body>
35+
<h1>Smart CMS</h1>
36+
37+
<h2>Create a Story</h2>
38+
<p><em>Note: Story titles cannot contain spaces.</em></p>
39+
<input id="create-title" type="text" placeholder="Story Title">
40+
<textarea id="create-content" placeholder="Story Content"></textarea>
41+
<button onclick="createStory()">Create Story</button>
42+
43+
<h2>Get a Story</h2>
44+
<p><em>Note: Story titles cannot contain spaces.</em></p>
45+
<input id="get-title" type="text" placeholder="Story Title">
46+
<button onclick="getStory()">Get Story</button>
47+
48+
<h2>Generate a Story</h2>
49+
<button onclick="generateStory()">Generate Story</button>
50+
51+
<div id="output" class="output"></div>
52+
53+
<script>
54+
const API_BASE = 'http://127.0.0.1:8000';
55+
56+
async function createStory() {
57+
let title = document.getElementById('create-title').value.trim();
58+
let content = document.getElementById('create-content').value;
59+
60+
// Check for spaces in the title
61+
if (title.includes(' ')) {
62+
showOutput('Error: The story title cannot contain spaces.');
63+
return;
64+
}
65+
66+
if (!title || !content) {
67+
showOutput('Please enter both a title and content.');
68+
return;
69+
}
70+
71+
// Combine title and content with a newline separator
72+
const body = `${title}\n${content}`;
73+
74+
try {
75+
const response = await fetch(`${API_BASE}/create`, {
76+
method: 'POST',
77+
headers: { 'Content-Type': 'text/plain' },
78+
body: body
79+
});
80+
81+
const result = await response.text();
82+
showOutput(result || 'Story created successfully.');
83+
} catch (err) {
84+
showOutput('Error creating story.');
85+
}
86+
}
87+
88+
async function getStory() {
89+
let title = document.getElementById('get-title').value.trim();
90+
91+
// Check for spaces in the title
92+
if (title.includes(' ')) {
93+
showOutput('Error: The story title cannot contain spaces.');
94+
return;
95+
}
96+
97+
if (!title) {
98+
showOutput('Please enter a title.');
99+
return;
100+
}
101+
102+
try {
103+
const response = await fetch(`${API_BASE}/retrieve`, {
104+
method: 'POST',
105+
headers: { 'Content-Type': 'text/plain' },
106+
body: title
107+
});
108+
109+
const result = await response.text();
110+
111+
if (result === 'Story not found') {
112+
showOutput(result);
113+
return;
114+
}
115+
116+
showOutput(`Title: ${title}\n\n${result}`);
117+
} catch (err) {
118+
showOutput('Error retrieving story.');
119+
}
120+
}
121+
122+
async function generateStory() {
123+
try {
124+
const response = await fetch(`${API_BASE}/generate`, {
125+
method: 'GET',
126+
});
127+
128+
const result = await response.text();
129+
130+
// Split the result into lines
131+
const lines = result.split('\n');
132+
const title = lines[0];
133+
const content = lines.slice(1).join('\n');
134+
135+
showOutput(`Title: ${title}\n\n${content}`);
136+
} catch (err) {
137+
showOutput('Error generating story.');
138+
}
139+
}
140+
141+
function showOutput(message) {
142+
const output = document.getElementById('output');
143+
output.textContent = message;
144+
output.style.display = 'block';
145+
}
146+
</script>
147+
</body>
148+
</html>

chapter08/smart-cms/ollama.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: ollama
5+
namespace: default
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: ollama
11+
template:
12+
metadata:
13+
labels:
14+
app: ollama
15+
spec:
16+
containers:
17+
- name: ollama
18+
image: ollama/ollama:latest
19+
ports:
20+
- containerPort: 11434
21+
env:
22+
- name: PRELOAD_MODELS
23+
value: "gurubot/tinystories-656k-q8"
24+
- name: OLLAMA_KEEP_ALIVE
25+
value: "12h"
26+
volumeMounts:
27+
- name: ollama-storage
28+
mountPath: /root/.ollama
29+
lifecycle:
30+
postStart:
31+
exec:
32+
command: ["/bin/sh", "-c", "for model in $PRELOAD_MODELS; do ollama run $model \"\"; done"]
33+
volumes:
34+
- name: ollama-storage
35+
emptyDir: {}
36+
---
37+
apiVersion: v1
38+
kind: Service
39+
metadata:
40+
name: ollama
41+
namespace: default
42+
spec:
43+
selector:
44+
app: ollama
45+
ports:
46+
- port: 11434
47+
targetPort: 11434
48+
protocol: TCP

chapter08/smart-cms/wadm.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: core.oam.dev/v1beta1
2+
kind: Application
3+
metadata:
4+
name: smart-cms
5+
annotations:
6+
description: "A smart CMS!"
7+
spec:
8+
components:
9+
- name: http-component
10+
type: component
11+
properties:
12+
image: ghcr.io/danbugs/serverside-wasm-book-code/smart-cms:v1
13+
traits:
14+
- type: spreadscaler
15+
properties:
16+
instances: 100
17+
- type: link
18+
properties:
19+
namespace: wasi
20+
package: keyvalue
21+
interfaces: [store, atomics]
22+
target:
23+
name: kvnats
24+
config:
25+
- name: wasi-keyvalue-config
26+
properties:
27+
bucket: default
28+
enable_bucket_auto_create: 'true'
29+
- type: link
30+
properties:
31+
target: ollama
32+
namespace: thomastaylor312
33+
package: ollama
34+
interfaces: [generate]
35+
target_config:
36+
- name: ollama-conf
37+
properties:
38+
model_name: gurubot/tinystories-656k-q8
39+
host: 'http://ollama:11434'
40+
41+
- name: httpserver
42+
type: capability
43+
properties:
44+
image: ghcr.io/wasmcloud/http-server:0.23.2
45+
traits:
46+
- type: link
47+
properties:
48+
target: http-component
49+
namespace: wasi
50+
package: http
51+
interfaces: [incoming-handler]
52+
source_config:
53+
- name: default-http
54+
properties:
55+
address: 0.0.0.0:8000
56+
57+
- name: kvnats
58+
type: capability
59+
properties:
60+
image: ghcr.io/wasmcloud/keyvalue-nats:0.3.1
61+
62+
- name: ollama
63+
type: capability
64+
properties:
65+
image: ghcr.io/danbugs/serverside-wasm-book-code/ollama-provider:v1

0 commit comments

Comments
 (0)