File size: 1,531 Bytes
3d4392e
 
 
 
 
 
 
 
 
 
f24ad59
3d4392e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server"

import YAML from "yaml"

import { predict as predictWithHuggingFace } from "@/app/api/providers/huggingface/predictWithHuggingFace"
import { predict as predictWithOpenAI } from "@/app/api/providers/openai/predictWithOpenAI"

import { LatentScenes } from "./types"
import { getSystemPrompt } from "./getSystemPrompt"
import { unknownObjectToLatentScenes } from "./unknownObjectToLatentScenes"
import { parseRawStringToYAML } from "../../parsers/parseRawStringToYAML"

export async function getLatentScenes({
  prompt = "",
  debug = false
}: {
  prompt?: string
  debug?: boolean
} = {}): Promise<LatentScenes> {

  // abort early
  if (!prompt) { 
    return []
  }

  const systemPrompt = getSystemPrompt()

  const userPrompt = `generate a short story about: ${prompt}`

  let scenes: LatentScenes = []
  try {
    // we use Hugging Face for now, as our users might try funny things,
    // which could get us banned from OpenAI
    let rawString = await predictWithHuggingFace({
      systemPrompt,
      userPrompt,
      nbMaxNewTokens: 1200,
      prefix: "",
    })

    if (debug) {
      console.log("getLatentScenes: rawString = " + rawString)
    }

    const maybeLatentScenes = parseRawStringToYAML<LatentScenes>(rawString, [])

    scenes = unknownObjectToLatentScenes(maybeLatentScenes)

    if (debug) {
      console.log(`getLatentScenes: scenes = ` + JSON.stringify(scenes, null, 2))
    }
  } catch (err) {
    scenes = []
    console.error(`getLatentScenes failed (${err})`)
  }

  return scenes
}