File size: 2,398 Bytes
fd2aa6b
 
f0dc1c3
 
 
 
 
 
 
 
 
 
ab75c71
f0dc1c3
 
 
ab75c71
f0dc1c3
 
ab75c71
f0dc1c3
b1ecc22
ab75c71
b1ecc22
 
 
 
 
f0dc1c3
 
 
 
 
 
 
e62f50c
 
f0dc1c3
 
 
 
 
 
e66b0b0
f0dc1c3
 
e66b0b0
b1ecc22
 
97a7099
f0dc1c3
 
 
 
 
 
 
 
 
 
 
 
a44c1b5
 
 
f0dc1c3
 
 
a44c1b5
f0dc1c3
 
 
 
 
 
6a98c79
 
fd2aa6b
6a98c79
fd2aa6b
 
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
81
82
83
import sbd from "sbd"

import { Game } from "@/app/games/types"
import { createLlamaPrompt } from "@/lib/createLlamaPrompt"

import { getBase } from "./getBase"
import { predict } from "./predict"


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

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

  console.log("DEBUG", {
    game, situation, lastEvent,
    currentPrompt,
    initialPrompt,
    userSituationPrompt,

  })
  /*
      const basePrompt = initialPrompt !== currentPrompt
  ? `for your information, the initial game panel and scene was: ${initialPrompt}`
  : ""
  */

  const basePrompt = initialPrompt !== currentPrompt
    ? `You must imagine the most plausible next dialogue line from the game master, based on current and past situation.
Here is the original situation, which will inform you about the general game mood to follow (you must respect this): "${initialPrompt}".`
    : ""

  const prompt = createLlamaPrompt([
    {
      role: "system",
      content: [
        `You are an AI game master.`,
        `You are going to receive new information about the current whereabouts and action of the player.`,
        basePrompt,
        `You must imagine a funny response to speak in reaction to what the player did`,
        `Please only write between 2 to 3 short sentences, please.`,
        `Please add a few funny puns and jokes.`,
        `But please don't say things like "Well, well, well" or "Ah, the classic combination of" it is annoying.`
      ].filter(item => item).join("\n")
    },
    {
      role: "user",
      content: userSituationPrompt
    }
  ])


  let result = ""
  try {
    result = await predict(prompt)
    if (!result.trim().length) {
      throw new Error("empty dialogue!")
    }
  } catch (err) {
    console.log(`prediction of the dialogue failed, trying again..`)
    try {
      result = await predict(prompt+".")
    } catch (err) {
      console.error(`prediction of the dialogue failed again!`)
      throw new Error(`failed to generate the dialogue ${err}`)
    }
  }

  const tmp = result.split("game master:").pop() || result

  // llama-2 is too chatty, let's keep 3 sentences at most
  const sentences = sbd.sentences(tmp).slice(0, 3).join(" ").trim()

  return sentences
}