File size: 1,858 Bytes
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cef24b
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import YAML from "yaml"

import { predict } from "@/app/api/providers/huggingface/predictWithHuggingFace"
import { LatentStory } from "@/app/api/v1/types"

import { systemPrompt } from "./systemPrompt"

export async function generateMusicPrompts({
  prompt = "",
  latentStory = [],
  turbo = false,
}: {
  prompt?: string
  latentStory?: LatentStory[]
  turbo?: boolean
} = {
  prompt: "",
  latentStory: [],
  turbo: false
}): Promise<string[]> {

  if (!prompt.length) { throw new Error(`please provide a prompt`) }
  console.log("generateMusicPrompts(): prompt:", prompt)


  if (!latentStory.length) { throw new Error(`please provide a story`) }

  // console.log("generateMusicPrompts(): latentStory:", latentStory)

  const userPrompt = `The input story is about: ${prompt}.

The input story is:
\`\`\`yaml
${YAML.stringify(
  // we need to help the LLM by marking the shots with a simple numeric ID
  latentStory.map((shot, i) => ({
    shot: i,
    ...shot,
  }))
)}
\`\`\`

# Output`

  const prefix = "\""

  // we don't need a lot here!
  const nbMaxNewTokens = 120

  // TODO use streaming for the Hugging Face prediction
  //
  // note that a Clap file is actually a YAML stream of documents
  // so technically we could stream everything from end-to-end
  // (but I haven't coded the helpers to do this yet)
  let rawString = await predict({
    systemPrompt,
    userPrompt,
    nbMaxNewTokens,
    prefix,
    turbo,
  })

  // console.log("generateEntityPrompts(): rawString: ", rawString)

  let results: string[] = []
  
  // we remove everything after the last ``` (or ``)
  rawString = rawString.split(/```?/)[0].trim()
  results.push(rawString)

  if (!Array.isArray(results) || typeof results.at(0) !== "string" || !results) {
    throw new Error(`failed to generate the output (rawString is: ${rawString})`)
  }

  return results
}