Spaces:
Runtime error
Runtime error
Commit
•
3f09ee7
1
Parent(s):
8fb2ec4
improvements
Browse files- src/app/main.tsx +3 -3
- src/app/predict.ts +6 -0
- src/app/prompts.ts +49 -46
- src/components/business/image-renderer.tsx +15 -5
- src/components/business/video-renderer.tsx +1 -1
- src/lib/createLlamaPrompt.ts +25 -0
src/app/main.tsx
CHANGED
@@ -123,9 +123,9 @@ export default function Main() {
|
|
123 |
|
124 |
let newActionnables = []
|
125 |
try {
|
126 |
-
|
127 |
-
|
128 |
-
) as string[]).map(item =>
|
129 |
// clean the words to remove any punctuation
|
130 |
item.replace(/\W/g, '').trim()
|
131 |
)
|
|
|
123 |
|
124 |
let newActionnables = []
|
125 |
try {
|
126 |
+
// we remove all [ or ]
|
127 |
+
const sanitized = rawActionnables.replaceAll("[", "").replaceAll("]", "")
|
128 |
+
newActionnables = (JSON.parse(`[${sanitized}]`) as string[]).map(item =>
|
129 |
// clean the words to remove any punctuation
|
130 |
item.replace(/\W/g, '').trim()
|
131 |
)
|
src/app/predict.ts
CHANGED
@@ -24,6 +24,9 @@ export async function predict(inputs: string) {
|
|
24 |
if (
|
25 |
instructions.includes("</s>") ||
|
26 |
instructions.includes("<s>") ||
|
|
|
|
|
|
|
27 |
instructions.includes("</SYS>") ||
|
28 |
instructions.includes("<|end|>") ||
|
29 |
instructions.includes("<|assistant|>")
|
@@ -41,6 +44,9 @@ export async function predict(inputs: string) {
|
|
41 |
.replaceAll("<|end|>", "")
|
42 |
.replaceAll("<s>", "")
|
43 |
.replaceAll("</s>", "")
|
|
|
|
|
|
|
44 |
.replaceAll("</SYS>", "")
|
45 |
.replaceAll("<|assistant|>", "")
|
46 |
.replaceAll('""', '"')
|
|
|
24 |
if (
|
25 |
instructions.includes("</s>") ||
|
26 |
instructions.includes("<s>") ||
|
27 |
+
instructions.includes("[INST]") ||
|
28 |
+
instructions.includes("[/INST]") ||
|
29 |
+
instructions.includes("<SYS>") ||
|
30 |
instructions.includes("</SYS>") ||
|
31 |
instructions.includes("<|end|>") ||
|
32 |
instructions.includes("<|assistant|>")
|
|
|
44 |
.replaceAll("<|end|>", "")
|
45 |
.replaceAll("<s>", "")
|
46 |
.replaceAll("</s>", "")
|
47 |
+
.replaceAll("[INST]", "")
|
48 |
+
.replaceAll("[/INST]", "")
|
49 |
+
.replaceAll("<SYS>", "")
|
50 |
.replaceAll("</SYS>", "")
|
51 |
.replaceAll("<|assistant|>", "")
|
52 |
.replaceAll('""', '"')
|
src/app/prompts.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import { Game } from "./games/types"
|
2 |
-
|
3 |
|
4 |
export const getPrompts = (game: Game, situation: string = "", actionnable: string = "") => {
|
5 |
|
@@ -8,72 +8,75 @@ export const getPrompts = (game: Game, situation: string = "", actionnable: stri
|
|
8 |
const currentPrompt = situation
|
9 |
? [...game.getScenePrompt(situation)].join(", ")
|
10 |
: initialPrompt
|
11 |
-
|
12 |
-
const userSituationPrompt =
|
13 |
-
`[PROMPT] Player is currently in "${currentPrompt}". Player clicked on the "${actionnable}".`
|
14 |
-
]
|
15 |
|
16 |
const baseSituationPromptIfWeHaveHistory = initialPrompt !== currentPrompt
|
17 |
? ` You must imagine the most plausible next scene, based on where the player was located before and is now, and also what the player did before and are doing now.
|
18 |
Here is the original scene in which the user was located at first, which will inform you about the general settings to follow (you must respect this): "${initialPrompt}".`
|
19 |
: ""
|
20 |
|
21 |
-
const
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
const baseDialoguePromptIfWeHaveHistory = initialPrompt !== currentPrompt
|
48 |
? `for your information, the initial game panel and scene was: "${initialPrompt}".`
|
49 |
: ""
|
50 |
|
51 |
-
const
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
You
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
const dialoguePrompt = [
|
62 |
-
...dialogueSystemPrompt,
|
63 |
-
...userSituationPrompt,
|
64 |
-
].join(" ")
|
65 |
|
66 |
const prompts = {
|
67 |
initialPrompt,
|
68 |
currentPrompt,
|
69 |
userSituationPrompt,
|
70 |
baseSituationPromptIfWeHaveHistory,
|
71 |
-
situationSystemPrompt,
|
72 |
situationPrompt,
|
73 |
-
actionnablesSystemPrompt,
|
74 |
actionnablesPrompt,
|
75 |
baseDialoguePromptIfWeHaveHistory,
|
76 |
-
dialogueSystemPrompt,
|
77 |
dialoguePrompt,
|
78 |
}
|
79 |
|
|
|
1 |
import { Game } from "./games/types"
|
2 |
+
import { createLlamaPrompt } from "@/lib/createLlamaPrompt"
|
3 |
|
4 |
export const getPrompts = (game: Game, situation: string = "", actionnable: string = "") => {
|
5 |
|
|
|
8 |
const currentPrompt = situation
|
9 |
? [...game.getScenePrompt(situation)].join(", ")
|
10 |
: initialPrompt
|
11 |
+
|
12 |
+
const userSituationPrompt = `Player is currently in "${currentPrompt}". Player clicked on the "${actionnable}".`
|
|
|
|
|
13 |
|
14 |
const baseSituationPromptIfWeHaveHistory = initialPrompt !== currentPrompt
|
15 |
? ` You must imagine the most plausible next scene, based on where the player was located before and is now, and also what the player did before and are doing now.
|
16 |
Here is the original scene in which the user was located at first, which will inform you about the general settings to follow (you must respect this): "${initialPrompt}".`
|
17 |
: ""
|
18 |
|
19 |
+
const situationPrompt = createLlamaPrompt([
|
20 |
+
{
|
21 |
+
role: "system",
|
22 |
+
content: [
|
23 |
+
`You are the AI game master of a role video game.`,
|
24 |
+
baseSituationPromptIfWeHaveHistory,
|
25 |
+
`You are going to receive new information about the current whereabouts of the player.`,
|
26 |
+
`Please write a caption for the next plausible scene to display in intricate details: the environment, lights, era, characters, objects, textures, light etc. You must include important objects, that the user can click on (eg. characters, doors, vehicles, useful objects).`
|
27 |
+
].filter(item => item).join("\n")
|
28 |
+
},
|
29 |
+
{
|
30 |
+
role: "user",
|
31 |
+
content: userSituationPrompt
|
32 |
+
}
|
33 |
+
])
|
34 |
+
|
35 |
+
const actionnablesPrompt = createLlamaPrompt([
|
36 |
+
{
|
37 |
+
role: "system",
|
38 |
+
content: [
|
39 |
+
`You are an API endpoint that can return a list of objects thare are in a scene.`,
|
40 |
+
`You must list basic name of things (eg. "parrot", "chest", "spaceship", "glass", "door", "person", "window", "light", "knob", "button" etc)`,
|
41 |
+
`The answer must be a JSON array, ie. a list of quoted strings.`
|
42 |
+
].filter(item => item).join("\n")
|
43 |
+
},
|
44 |
+
{
|
45 |
+
role: "user",
|
46 |
+
content: userSituationPrompt
|
47 |
+
}
|
48 |
+
])
|
49 |
|
50 |
const baseDialoguePromptIfWeHaveHistory = initialPrompt !== currentPrompt
|
51 |
? `for your information, the initial game panel and scene was: "${initialPrompt}".`
|
52 |
: ""
|
53 |
|
54 |
+
const dialoguePrompt = createLlamaPrompt([
|
55 |
+
{
|
56 |
+
role: "system",
|
57 |
+
content: [
|
58 |
+
`You are the AI game master of a role video game.`,
|
59 |
+
`You are going to receive new information about the current whereabouts and action of the player.`,
|
60 |
+
baseDialoguePromptIfWeHaveHistory,
|
61 |
+
`You must imagine a funny response to speak in reaction to what the player did, like in some old point and click video games.`,
|
62 |
+
`Please limit yourself to only a 1 or 2 sentences, please.`,
|
63 |
+
].filter(item => item).join("\n")
|
64 |
+
},
|
65 |
+
{
|
66 |
+
role: "user",
|
67 |
+
content: userSituationPrompt
|
68 |
+
}
|
69 |
+
])
|
70 |
|
|
|
|
|
|
|
|
|
71 |
|
72 |
const prompts = {
|
73 |
initialPrompt,
|
74 |
currentPrompt,
|
75 |
userSituationPrompt,
|
76 |
baseSituationPromptIfWeHaveHistory,
|
|
|
77 |
situationPrompt,
|
|
|
78 |
actionnablesPrompt,
|
79 |
baseDialoguePromptIfWeHaveHistory,
|
|
|
80 |
dialoguePrompt,
|
81 |
}
|
82 |
|
src/components/business/image-renderer.tsx
CHANGED
@@ -23,6 +23,7 @@ export const ImageRenderer = ({
|
|
23 |
const contextRef = useRef<CanvasRenderingContext2D | null>(null)
|
24 |
const [actionnable, setActionnable] = useState<string>("")
|
25 |
const [progressPercent, setProcessPercent] = useState(0)
|
|
|
26 |
|
27 |
useEffect(() => {
|
28 |
if (maskBase64) {
|
@@ -135,26 +136,35 @@ export const ImageRenderer = ({
|
|
135 |
// note: when everything is fine, it takes about 45 seconds to render a new scene
|
136 |
|
137 |
const computeProgress = async () => {
|
138 |
-
|
139 |
-
|
140 |
-
console.log("Finished loading!")
|
141 |
setProcessPercent(100)
|
142 |
return
|
143 |
}
|
144 |
|
145 |
console.log("still loading..")
|
146 |
|
|
|
|
|
|
|
|
|
147 |
setTimeout(() => {
|
148 |
-
|
149 |
}, 1000)
|
150 |
|
151 |
}
|
152 |
|
153 |
computeProgress()
|
|
|
|
|
|
|
|
|
|
|
154 |
}, [isLoading, assetUrl])
|
|
|
155 |
|
156 |
if (!assetUrl) {
|
157 |
-
return <div className="flex w-full
|
158 |
<ProgressBar
|
159 |
text="⌛"
|
160 |
progressPercentage={progressPercent}
|
|
|
23 |
const contextRef = useRef<CanvasRenderingContext2D | null>(null)
|
24 |
const [actionnable, setActionnable] = useState<string>("")
|
25 |
const [progressPercent, setProcessPercent] = useState(0)
|
26 |
+
const showLoaderRef = useRef(true)
|
27 |
|
28 |
useEffect(() => {
|
29 |
if (maskBase64) {
|
|
|
136 |
// note: when everything is fine, it takes about 45 seconds to render a new scene
|
137 |
|
138 |
const computeProgress = async () => {
|
139 |
+
if (!showLoaderRef.current) {
|
140 |
+
console.log("Asked to hide the loader")
|
|
|
141 |
setProcessPercent(100)
|
142 |
return
|
143 |
}
|
144 |
|
145 |
console.log("still loading..")
|
146 |
|
147 |
+
console.log("updating progress")
|
148 |
+
progress = progress + 1
|
149 |
+
setProcessPercent(progress)
|
150 |
+
|
151 |
setTimeout(() => {
|
152 |
+
computeProgress()
|
153 |
}, 1000)
|
154 |
|
155 |
}
|
156 |
|
157 |
computeProgress()
|
158 |
+
}, [])
|
159 |
+
|
160 |
+
|
161 |
+
useEffect(() => {
|
162 |
+
showLoaderRef.current = isLoading || !assetUrl
|
163 |
}, [isLoading, assetUrl])
|
164 |
+
|
165 |
|
166 |
if (!assetUrl) {
|
167 |
+
return <div className="flex w-full pt-8 items-center justify-center text-center">
|
168 |
<ProgressBar
|
169 |
text="⌛"
|
170 |
progressPercentage={progressPercent}
|
src/components/business/video-renderer.tsx
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
export const VideoRenderer = ({ url }: { url?: string }) => {
|
4 |
|
5 |
if (!url) {
|
6 |
-
return <div className="flex w-full
|
7 |
<div>Rendering first frames.. (might take around 30s)</div>
|
8 |
</div>
|
9 |
}
|
|
|
3 |
export const VideoRenderer = ({ url }: { url?: string }) => {
|
4 |
|
5 |
if (!url) {
|
6 |
+
return <div className="flex w-full pt-8 items-center justify-center text-center">
|
7 |
<div>Rendering first frames.. (might take around 30s)</div>
|
8 |
</div>
|
9 |
}
|
src/lib/createLlamaPrompt.ts
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// adapted from https://huggingface.co/TheBloke/Llama-2-13B-chat-GPTQ/discussions/5
|
2 |
+
export function createLlamaPrompt(messages: Array<{ role: string, content: string }>) {
|
3 |
+
const B_INST = "[INST]", E_INST = "[/INST]";
|
4 |
+
const B_SYS = "<<SYS>>\n", E_SYS = "\n<</SYS>>\n\n";
|
5 |
+
const BOS = "<s>", EOS = "</s>";
|
6 |
+
const DEFAULT_SYSTEM_PROMPT = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.";
|
7 |
+
|
8 |
+
if (messages[0].role != "system"){
|
9 |
+
messages = [
|
10 |
+
{role: "system", content: DEFAULT_SYSTEM_PROMPT}
|
11 |
+
].concat(messages);
|
12 |
+
}
|
13 |
+
messages = [{role: messages[1].role, content: B_SYS + messages[0].content + E_SYS + messages[1].content}].concat(messages.slice(2));
|
14 |
+
|
15 |
+
let messages_list = messages.map((value, index, array) => {
|
16 |
+
if (index % 2 == 0 && index + 1 < array.length){
|
17 |
+
return `${BOS}${B_INST} ${array[index].content.trim()} ${E_INST} ${array[index+1].content.trim()} ${EOS}`
|
18 |
+
}
|
19 |
+
return '';
|
20 |
+
})
|
21 |
+
|
22 |
+
messages_list.push(`${BOS}${B_INST} ${messages[messages.length-1].content.trim()} ${E_INST}`)
|
23 |
+
|
24 |
+
return messages_list.join('');
|
25 |
+
}
|