Skip to content

Talk to yourself

 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
using System.Text;
using LLama.Abstractions;
using LLama.Common;

namespace LLama.Examples.Examples
{
    // Let two bots chat with each other.
    public class TalkToYourself
    {
        public static async Task Run()
        {
            string modelPath = UserSettings.GetModelPath();

            // Load weights into memory
            var @params = new ModelParams(modelPath);
            using var weights = LLamaWeights.LoadFromFile(@params);

            // Create 2 contexts sharing the same weights
            using var aliceCtx = weights.CreateContext(@params);
            var alice = new InteractiveExecutor(aliceCtx);
            using var bobCtx = weights.CreateContext(@params);
            var bob = new InteractiveExecutor(bobCtx);

            // Initial alice prompt
            var alicePrompt = "Transcript of a dialog, where the Alice interacts a person named Bob. Alice is friendly, kind, honest and good at writing.\nAlice: Hello";
            var aliceResponse = await Prompt(alice, ConsoleColor.Green, alicePrompt, false, false);

            // Initial bob prompt
            var bobPrompt = $"Transcript of a dialog, where the Bob interacts a person named Alice. Bob is smart, intellectual and good at writing.\nAlice: Hello{aliceResponse}";
            var bobResponse = await Prompt(bob, ConsoleColor.Red, bobPrompt, true, true);

            // swap back and forth from Alice to Bob
            while (true)
            {
                aliceResponse = await Prompt(alice, ConsoleColor.Green, bobResponse, false, true);
                bobResponse = await Prompt(bob, ConsoleColor.Red, aliceResponse, false, true);

                if (Console.KeyAvailable)
                    break;
            }
        }

        private static async Task<string> Prompt(ILLamaExecutor executor, ConsoleColor color, string prompt, bool showPrompt, bool showResponse)
        {
            var inferenceParams = new InferenceParams
            {
                Temperature = 0.9f,
                AntiPrompts = new List<string> { "Alice:", "Bob:", "User:" },
                MaxTokens = 128,
                Mirostat = MirostatType.Mirostat2,
                MirostatTau = 10,
            };

            Console.ForegroundColor = ConsoleColor.White;
            if (showPrompt)
                Console.Write(prompt);

            Console.ForegroundColor = color;
            var builder = new StringBuilder();
            await foreach (var text in executor.InferAsync(prompt, inferenceParams))
            {
                builder.Append(text);
                if (showResponse)
                    Console.Write(text);
            }

            return builder.ToString();
        }
    }
}