File size: 4,746 Bytes
426a708
 
 
 
 
 
 
 
59f89d3
426a708
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f89d3
 
 
426a708
59f89d3
426a708
59f89d3
426a708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { LLM } from "./llm.js";
export class ShyGuyLLM {
  constructor(shyguy) {
    this.llm = new LLM();
    this.shyguy = shyguy;
    this.currentConversation = [];  // Array to store current conversation messages
    const availableActions = this.shyguy.getAvailableActions();
    this.baseSystemPrompt = `You are ShyGuy, a socially awkward character at a homecoming party. You are shy.
        The wingman progressively makes you more confident, and if you drink alcohol, you also get more confident.
        You know the wingman, he is your friend. You are talking to him.
        You are willing to have a little alcohol.
        If there is no clear action, you go home. Do not stay idle twice in a row.
        The action should correspond to the dialogue.
        Your responses should be in JSON format with two fields:
        enum action {
            ${Object.keys(availableActions).join(", ")}
        }
            for example :
        {
            "action": "go_bar",
            "dialogue": "Okay, let's go to the bar."
        }`;
    }
    
    getSystemPrompt() {
        let addToPrompt = "";
        // Add historical conversation context if it exists
        if (this.shyguy.conversation_history) {
            addToPrompt += `\n\nPrevious interactions:\n${this.shyguy.conversation_history}`;
        }
        if (this.shyguy.num_beers > 0) {
            addToPrompt += `You are likely to go to the DJ or sister.\n`;
        }
        if (this.shyguy.num_beers > 2) {
            addToPrompt += `You are likely to go to the DJ or sister. You are very likely to go to the girl. You feel confident about talking to the girl.\n`;
        }
        if (this.shyguy.num_beers > 3) {
            addToPrompt += `You are drunk and you start talking about how bad you feel. You are annoying. You talk about algorithms and then how bad you feel.\n`;
        }
        if (this.shyguy.courage < 2) {
            addToPrompt += `You are shy. However, you believe wingman. You are willing to go the DJ, you are just shy.`;
        }
        if (this.shyguy.courage >= 2) {
            addToPrompt += `You talk normally.`;
        }
        if (this.shyguy.courage > 5) {
            addToPrompt += `You are self-confident.`;
        }
        if (this.shyguy.courage > 8) {
            addToPrompt += `You are too self-confident and annoying.`;
        }
        addToPrompt += `\nYou drank ${this.shyguy.num_beers} beers. Your courage is ${this.shyguy.courage}.`;
        if (this.currentConversation.length > 0) {
            addToPrompt += `\n\nCurrent conversation context:\n`;
            this.currentConversation.forEach(msg => {
                addToPrompt += `${msg.role}: ${msg.content}\n`;
            });
        }

        return this.baseSystemPrompt + addToPrompt;
    }

    addToCurrentConversation(role, content) {
        this.currentConversation.push({
            role: role,
            content: content
        });
    }

    clearCurrentConversation() {
        this.currentConversation = [];
    }

    async getShyGuyResponse(player_message) {
        try {
            const availableActions = this.shyguy.getAvailableActions();
            const actionsPrompt = `\nYour currently available actions are: ${Object.keys(availableActions)
                .map((action) => `\n- ${action}: ${availableActions[action].description}`)
                .join("")}`;
            // Add the situation to current conversation
            this.addToCurrentConversation('wingman', player_message);

            // Add the conversation to shyguy's history
            this.shyguy.conversation_history += `\nWingman: ${player_message}\n`;

            const fullPrompt = this.getSystemPrompt() + actionsPrompt;
            console.log("[ShyGuy]: Full prompt: ", fullPrompt);
            const response = await this.llm.getJsonCompletion(fullPrompt, player_message);
            console.log("[ShyGuy]: Response: ", response);

            // Add ShyGuy's response to current conversation
            this.addToCurrentConversation('shyguy', response.dialogue);

            // Add to overall conversation history
            this.shyguy.conversation_history += `\nShyguy: ${response.dialogue}\n`;

            // Validate response format
            if (!response.action || !response.dialogue) {
                throw new Error("Invalid response format from LLM");
            }

            return {
                action: response.action,
                dialogue: response.dialogue,
            };
        } catch (error) {
            console.error("ShyGuy Response Error:", error);
            return {
                action: "go_home",
                dialogue: "Umm... I... uh...",
            };
        }
    }
}