File size: 2,540 Bytes
f0dc1c3
 
 
 
 
 
1e2c870
f0dc1c3
e62f50c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0dc1c3
 
 
ab75c71
f0dc1c3
 
 
ab75c71
f0dc1c3
 
ab75c71
f0dc1c3
 
 
 
 
 
 
 
 
e66b0b0
f0dc1c3
25e7ef4
2f0f4c5
f0dc1c3
 
 
 
 
 
 
 
 
e62f50c
 
f0dc1c3
 
e62f50c
f0dc1c3
 
 
e62f50c
 
f0dc1c3
e62f50c
 
f0dc1c3
 
 
1e2c870
f0dc1c3
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
import { Game } from "@/app/games/types"
import { createLlamaPrompt } from "@/lib/createLlamaPrompt"
import { parseJsonList } from "@/lib/parseJsonList"

import { getBase } from "./getBase"
import { predict } from "./predict"
import { normalizeActionnables } from "@/lib/normalizeActionnables"

const parseActionnablesOrThrow = (input: string) => {
  let result: string[] = []
  try {
    result = parseJsonList(input)

    if (!result.length) {
      throw new Error("no actionnables")
    }
  } catch (err) {
    console.log("failed to find a valid JSON! attempting method 2..")

    const sanitized = input.replaceAll("[", "").replaceAll("]", "")
    result = (JSON.parse(`[${sanitized}]`) as string[])

    if (!result.length) {
      throw new Error("no actionnables")
    }
  }
  return result
}

export const getActionnables = async ({
  game,
  situation = "",
  lastEvent = "",
}: {
  game: Game;
  situation: string;
  lastEvent: string;
}) => {

  const { currentPrompt, initialPrompt, userSituationPrompt } = getBase({ game, situation, lastEvent })

  const basePrompt = initialPrompt !== currentPrompt
    ? `Here is some context information about the initial scene: ${initialPrompt}`
    : ""

  const prompt = createLlamaPrompt([
    {
      role: "system",
      content: [
        `You are an API endpoint that can return a list of objects visible in the background image of a game.`,
        basePrompt,
        `You must list twelve (12) basic names of visible objects (eg. "door", "person", "window", "light", "floor", "knob", "button", "rock", "tree", "box", "glass".. etc) but don't list any word from abstract or immaterial concepts (ig. don't list words like "secret", "danger", "next move", "game" etc)`,
        `The answer must be a JSON array, ie. a list of 12 quoted strings.`
      ].filter(item => item).join("\n")
    },
    {
      role: "user",
      content: userSituationPrompt
    }
  ])

  let rawStringOutput = ""
  let result: string[] = []

  try {
    rawStringOutput = await predict(prompt)
    result = parseActionnablesOrThrow(rawStringOutput)
  } catch (err) {
    console.log(`prediction of the actionnables failed, trying again..`)
    try {
      rawStringOutput = await predict(prompt+".")
      result = parseActionnablesOrThrow(rawStringOutput)
    } catch (err) {
      console.error(`prediction of the actionnables failed again! going to use default value`)
      console.log("for reference, rawStringOutput was: ", rawStringOutput)
    }
  }
  
  return normalizeActionnables(result)
}