๐Ÿ“ฆ sleepyfran / duets

๐Ÿ“„ LanguageModel.fs ยท 163 lines
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163module Duets.Agents.LanguageModel

open Duets.Common

open System
open System.IO
open System.Threading
open FSharp.Control
open LLama
open LLama.Common
open LLama.Native
open LLama.Sampling

type private LanguageModelState =
    { Executor: InteractiveExecutor
      PreviousChatHistory: ChatHistory option }

let private inferenceParams =
    InferenceParams(
        SamplingPipeline =
            // Settings taken from model card: https://huggingface.co/unsloth/gemma-3-270m-it-GGUF
            new DefaultSamplingPipeline(
                Temperature = 1f,
                TopK = 64,
                TopP = 0.95f,
                MinP = 0f
            ),
        AntiPrompts = [ "<end_of_turn>" ]
    )

type private SavegameAgentMessage =
    | Initialize of AsyncReplyChannel<unit>
    | StreamMessage of prompt: string * AsyncReplyChannel<AsyncSeq<String>>

let private waitForFirstToken executor =
    let chatHistory = ChatHistory()
    let session = ChatSession(executor, chatHistory)

    let cts = new CancellationTokenSource()

    let rawAsyncEnumerable: Collections.Generic.IAsyncEnumerable<string> =
        session.ChatAsync(
            ChatHistory.Message(AuthorRole.User, "Give me an A"),
            inferenceParams,
            cts.Token
        )

    rawAsyncEnumerable
    |> AsyncSeq.ofAsyncEnum
    |> AsyncSeq.iter (fun token -> if token <> "" then cts.Cancel() else ())
    |> Async.RunSynchronously

/// Agent in charge of writing and loading the stats of the game.
type LanguageModelAgent() =
    let agent =
        MailboxProcessor.Start
        <| fun inbox ->
            let rec loop state =
                async {
                    let! msg = inbox.Receive()

                    match msg with
                    | Initialize(channel) ->
                        try
                            NativeLibraryConfig.All.WithLogCallback(fun _ _ ->
                                ())
                            |> ignore

                            // Force to load now instead of after the first inference.
                            NativeApi.llama_empty_call ()

                            let modelPath =
                                Path.Combine(
                                    AppDomain.CurrentDomain.BaseDirectory,
                                    "models",
                                    "model.gguf"
                                )

                            let parameters =
                                ModelParams(modelPath, GpuLayerCount = 5)

                            let model = LLamaWeights.LoadFromFile(parameters)
                            let context = model.CreateContext(parameters)
                            let executor = InteractiveExecutor(context)

                            let newState =
                                { Executor = executor
                                  PreviousChatHistory = None }

                            waitForFirstToken executor

                            channel.Reply()
                            return! loop (Some newState)
                        with ex ->
                            printfn
                                $"Error initializing LanguageModelAgent: %s{ex.Message}"

                            channel.Reply()
                            return! loop None

                    | StreamMessage(prompt, channel) ->
                        let executor = state.Value.Executor
                        let chatHistory = ChatHistory()

                        let session = ChatSession(executor, chatHistory)

                        let rawAsyncEnumerable
                            : Collections.Generic.IAsyncEnumerable<string> =
                            session.ChatAsync(
                                ChatHistory.Message(AuthorRole.User, prompt),
                                inferenceParams,
                                CancellationToken.None
                            )

                        let mutable previousToken = ""

                        rawAsyncEnumerable
                        |> AsyncSeq.ofAsyncEnum
                        |> AsyncSeq.map (fun token ->
                            let sanitizedToken =
                                token
                                |> String.replace @"\s+" " "
                                |> String.replace @"(?<!\n)\n(?!\n)" " "

                            let sanitizedToken =
                                (*
                                Sometimes tokens include a space on them, other
                                times they come in a "space word" fashion, from
                                what I have seen mostly to separate words from
                                a full stop, so only allow empty spaces if the
                                previous token was a period to allow that.
                                *)
                                if
                                    sanitizedToken = " "
                                    && previousToken <> "."
                                then
                                    ""
                                else
                                    sanitizedToken

                            previousToken <- sanitizedToken
                            sanitizedToken)
                        |> channel.Reply

                        return! loop state
                }

            loop None

    member _.Initialize() =
        agent.PostAndReply(fun channel -> Initialize(channel))

    member _.StreamMessage(context) =
        agent.PostAndReply(fun channel -> StreamMessage(context, channel))

let agent = LanguageModelAgent()

/// Initializes the language model agent.
let initialize = agent.Initialize

/// Streams a message from the language model given a prompt.
let streamMessage prompt = agent.StreamMessage prompt