Skip to content

Commit 264d2ea

Browse files
committed
prompt update
1 parent 5d2ce04 commit 264d2ea

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

examples/bedtime-story-teller/assets/app.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function initSocketIO() {
1010
socket.on('response', (data) => {
1111
document.getElementById('story-container').style.display = 'flex';
1212
const storyResponse = document.getElementById('story-response');
13+
storyResponse.textContent = data;
1314
document.getElementById('loading-spinner').style.display = 'none';
1415
const clearStoryButton = document.getElementById('clear-story-button');
1516
clearStoryButton.style.display = 'block';
@@ -135,6 +136,7 @@ function gatherDataAndGenerateStory() {
135136
const endingType = storyTypeContainer.querySelector('.story-type-paragraph:nth-child(2) .chip.selected')?.textContent.trim() || 'any';
136137
const narrativeStructure = storyTypeContainer.querySelector('.story-type-paragraph:nth-child(3) .chip.selected')?.textContent.trim() || 'any';
137138
const duration = storyTypeContainer.querySelector('.story-type-paragraph:nth-child(4) .chip.selected')?.textContent.trim() || 'any';
139+
138140
const characters = [];
139141
const characterGroups = document.querySelectorAll('.character-input-group');
140142
characterGroups.forEach(group => {
@@ -145,29 +147,32 @@ function gatherDataAndGenerateStory() {
145147
characters.push({ name, role, description });
146148
}
147149
});
148-
const protagonist = characters.find(c => c.role === 'protagonist');
149-
const helper = characters.find(c => c.role === 'positive-helper');
150-
const antagonist = characters.find(c => c.role === 'antagonist');
150+
151151
const other = document.querySelector('.other-textarea').value.trim();
152-
const formattedPrompt = `As a parent who loves to read bedtime stories to my <strong>${age}</strong> year old child, I need a delightful and age-appropriate story about an <strong>${protagonist ? protagonist.description : ''}</strong>, <strong>${protagonist ? protagonist.name : 'a character'}</strong> accompanied by his <strong>${helper ? helper.description : ''}</strong> helper <strong>${helper ? helper.name : 'a friend'}</strong> who will have to face the <strong>${antagonist ? antagonist.description : ''}</strong> antagonist <strong>${antagonist ? antagonist.name : 'a villain'}</strong>. The story type is <strong>${theme}</strong>. The tone should be <strong>${tone}</strong>. The format should be a narrative-style story with a clear beginning, middle, and end, allowing for a smooth and engaging reading experience. The objective is to entertain and soothe the child before bedtime. Provide a brief introduction to set the scene and introduce the main character. The scope should revolve around the topic: managing emotions and conflicts. The length should be approximately <strong>${duration}</strong>. Please ensure the story has a <strong>${narrativeStructure}</strong> narrative structure, leaving the child with a sense of <strong>${endingType}</strong>. The language should be easy to understand and suitable for my child's age comprehension.
153-
${other ? `
154-
155-
Other on optional stuff for the story: <strong>${other}</strong>` : ''}`;
156-
document.getElementById('prompt-display').innerHTML = formattedPrompt;
157-
document.getElementById('prompt-container').style.display = 'flex';
158-
const rawPrompt = formattedPrompt.replace(/<strong>/g, '').replace(/<\/strong>/g, '');
159-
generateStory(rawPrompt);
152+
153+
const storyData = {
154+
age,
155+
theme,
156+
tone,
157+
endingType,
158+
narrativeStructure,
159+
duration,
160+
characters,
161+
other,
162+
};
163+
164+
generateStory(storyData);
160165
}
161166

162-
function generateStory(msg) {
167+
function generateStory(data) {
163168
document.querySelector('.story-output-placeholder').style.display = 'none';
164169
const responseArea = document.getElementById('story-response-area');
165170
responseArea.style.display = 'flex';
166171
document.getElementById('story-container').style.display = 'none';
167172
document.getElementById('loading-spinner').style.display = 'block';
168173
document.getElementById('story-response').textContent = '';
169174
document.getElementById('clear-story-button').style.display = 'none';
170-
socket.emit('generate_story', msg);
175+
socket.emit('generate_story', data);
171176
}
172177

173178
function resetStoryView() {

examples/bedtime-story-teller/assets/index.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ <h3 class="parameter-title">Other</h3>
175175
</div>
176176
<div id="story-response-area" style="display: none;">
177177
<div id="loading-spinner" class="spinner" style="display: none;"></div>
178-
<div class="response-container" id="prompt-container" style="display: none;">
179-
<h3 class="response-title">Prompt</h3>
180-
<div id="prompt-display" class="response-content"></div>
181-
</div>
182178
<div class="response-container" id="story-container" style="display: none;">
183179
<div class="response-header">
184180
<h3 class="response-title">Story</h3>

examples/bedtime-story-teller/python/main.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,47 @@
1717

1818
ui = WebUI()
1919

20-
def generate_story(_, msg):
21-
for resp in llm.chat_stream(msg):
20+
def find_character_by_role(characters, role):
21+
for char in characters:
22+
if char['role'] == role:
23+
return char
24+
return None
25+
26+
def generate_story(_, data):
27+
age = data.get('age', 'any')
28+
theme = data.get('theme', 'any')
29+
tone = data.get('tone', 'any')
30+
ending_type = data.get('endingType', 'any')
31+
narrative_structure = data.get('narrativeStructure', 'any')
32+
duration = data.get('duration', 'any')
33+
characters = data.get('characters', [])
34+
other = data.get('other', '')
35+
36+
protagonist = find_character_by_role(characters, 'protagonist')
37+
helper = find_character_by_role(characters, 'positive-helper')
38+
antagonist = find_character_by_role(characters, 'antagonist')
39+
40+
prompt = f"As a parent who loves to read bedtime stories to my {age} year old child, I need a delightful and age-appropriate story."
41+
if protagonist:
42+
prompt += f" about an {protagonist['description']}, {protagonist['name']}"
43+
else:
44+
prompt += " about a character"
45+
46+
if helper:
47+
prompt += f" accompanied by his {helper['description']} helper {helper['name']}"
48+
else:
49+
prompt += " accompanied by a friend"
50+
51+
if antagonist:
52+
prompt += f" who will have to face the {antagonist['description']} antagonist {antagonist['name']}"
53+
else:
54+
prompt += " who will have to face a villain"
55+
56+
prompt += f". The story type is {theme}. The tone should be {tone}. The format should be a narrative-style story with a clear beginning, middle, and end, allowing for a smooth and engaging reading experience. The objective is to entertain and soothe the child before bedtime. Provide a brief introduction to set the scene and introduce the main character. The scope should revolve around the topic: managing emotions and conflicts. The length should be approximately {duration}. Please ensure the story has a {narrative_structure} narrative structure, leaving the child with a sense of {ending_type}. The language should be easy to understand and suitable for my child's age comprehension."
57+
if other:
58+
prompt += f"\n\nOther on optional stuff for the story: {other}"
59+
60+
for resp in llm.chat_stream(prompt):
2261
ui.send_message("response", resp)
2362

2463
ui.on_message("generate_story", generate_story)

0 commit comments

Comments
 (0)