File size: 3,172 Bytes
9052a89
f4dea7d
 
faf4ba4
c32ec0d
9052a89
f4dea7d
faf4ba4
 
 
f4dea7d
 
 
5dd2af5
f4dea7d
 
 
5dd2af5
faf4ba4
9bfb451
855183b
048071f
faf4ba4
f4dea7d
9052a89
f4dea7d
 
 
cea3188
faf4ba4
 
f4dea7d
9052a89
f4dea7d
 
 
 
 
 
faf4ba4
f4dea7d
 
 
2f7798c
f4dea7d
4f8f050
5dd2af5
879455c
f4dea7d
 
 
4f8f050
f4dea7d
5dd2af5
879455c
f4dea7d
 
 
4f8f050
f4dea7d
 
 
 
f5d8038
c32ec0d
2f7798c
faf4ba4
f4dea7d
 
faf4ba4
f4dea7d
4f8f050
 
11443d4
2f7798c
 
 
faf4ba4
c32ec0d
 
 
 
 
 
 
 
f4dea7d
2f7798c
faf4ba4
f4dea7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

import { predict } from "./predict"
import { Preset } from "../engine/presets"
import { GeneratedPanels } from "@/types"
import { cleanJson } from "@/lib/cleanJson"
import { createZephyrPrompt } from "@/lib/createZephyrPrompt"

import { dirtyGeneratedPanelCleaner } from "@/lib/dirtyGeneratedPanelCleaner"
import { dirtyGeneratedPanelsParser } from "@/lib/dirtyGeneratedPanelsParser"

export const getStory = async ({
  preset,
  prompt = "",
  nbTotalPanels = 4,
}: {
  preset: Preset;
  prompt: string;
  nbTotalPanels: number;
}): Promise<GeneratedPanels> => {
  throw new Error("legacy, deprecated")
  
  // In case you need to quickly debug the RENDERING engine you can uncomment this:
  // return mockGeneratedPanels

  const query = createZephyrPrompt([
    {
      role: "system",
      content: [
        `You are a writer specialized in ${preset.llmPrompt}`,
        `Please write detailed drawing instructions and short (2-3 sentences long) speech captions for the ${nbTotalPanels} panels of a new story. Please make sure each of the ${nbTotalPanels} panels include info about character gender, age, origin, clothes, colors, location, lights, etc.`,
        `Give your response as a VALID JSON array like this: \`Array<{ panel: number; instructions: string; caption: string; }>\`.`,
        // `Give your response as Markdown bullet points.`,
        `Be brief in your ${nbTotalPanels} instructions and narrative captions, don't add your own comments. The whole story must be captivating, smart, entertaining. Be straight to the point, and never reply things like "Sure, I can.." etc. Reply using valid JSON.`
      ].filter(item => item).join("\n")
    },
    {
      role: "user",
      content: `The story is: ${prompt}`,
    }
  ]) + "\n```[{"


  let result = ""

  try {
    // console.log(`calling predict(${query}, ${nbTotalPanels})`)
    result = `${await predict(query, nbTotalPanels) || ""}`.trim()
    if (!result.length) {
      throw new Error("empty result!")
    }
  } catch (err) {
    // console.log(`prediction of the story failed, trying again..`)
    try {
      result = `${await predict(query+".", nbTotalPanels) || ""}`.trim()
      if (!result.length) {
        throw new Error("empty result!")
      }
    } catch (err) {
      console.error(`prediction of the story failed again πŸ’©`)
      throw new Error(`failed to generate the story ${err}`)
    }
  }

  // console.log("Raw response from LLM:", result)
  const tmp = cleanJson(result)
  
  let GeneratedPanels: GeneratedPanels = []

  try {
    GeneratedPanels = dirtyGeneratedPanelsParser(tmp)
  } catch (err) {
    // console.log(`failed to read LLM response: ${err}`)
    // console.log(`original response was:`, result)

      // in case of failure here, it might be because the LLM hallucinated a completely different response,
      // such as markdown. There is no real solution.. but we can try a fallback:

    GeneratedPanels = (
      tmp.split("*")
      .map(item => item.trim())
      .map((cap, i) => ({
        panel: i,
        caption: cap,
        instructions: cap,
      }))
    )
  }

  return GeneratedPanels.map(res => dirtyGeneratedPanelCleaner(res))
}