blockless_sdk/
llm.rs

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use json::JsonValue;
use std::cmp::Ordering;

pub type Handle = u32;

#[link(wasm_import_module = "blockless_llm")]
extern "C" {
    fn llm_set_model_request(model_ptr: *const u8, model_len: u32, fd: *mut u32) -> i32;
    fn llm_get_model_response(buf: *mut u8, size: u32, num: *mut u32, fd: u32) -> i32;
    fn llm_set_model_options_request(options_ptr: *const u8, options_len: u32, fd: u32) -> i32;
    fn llm_get_model_options(buf: *mut u8, size: u32, num: *mut u32, fd: u32) -> i32;
    fn llm_prompt_request(prompt_ptr: *const u8, prompt_len: u32, fd: u32) -> i32;
    fn llm_read_prompt_response(buf: *mut u8, size: u32, num: *mut u32, fd: u32) -> i32;
    fn llm_close(fd: u32) -> i32;
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct BlocklessLlm {
    inner: Handle,
    model_name: String,
    options: LlmOptions,
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct LlmOptions {
    pub system_message: String,
    // pub max_tokens: u32,
    pub temperature: Option<f32>,
    pub top_p: Option<f32>,
    // pub frequency_penalty: f32,
    // pub presence_penalty: f32,
}

impl Default for LlmOptions {
    fn default() -> Self {
        LlmOptions {
            system_message: String::new(),
            temperature: None,
            top_p: None,
            // frequency_penalty: 0.0,
            // presence_penalty: 0.0,
        }
    }
}

impl LlmOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn dump(&self) -> String {
        let mut json = JsonValue::new_object();
        json["system_message"] = self.system_message.clone().into();
        if let Some(temperature) = self.temperature {
            json["temperature"] = temperature.into();
        }
        if let Some(top_p) = self.top_p {
            json["top_p"] = top_p.into();
        }
        json.dump()
    }
}

impl TryFrom<Vec<u8>> for LlmOptions {
    type Error = LlmErrorKind;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        // Convert bytes to UTF-8 string
        let json_str = String::from_utf8(bytes).map_err(|_| LlmErrorKind::Utf8Error)?;

        // Parse the JSON string
        let json = json::parse(&json_str).map_err(|_| LlmErrorKind::OptionsNotSet)?;

        // Extract system_message
        let system_message = json["system_message"]
            .as_str()
            .ok_or(LlmErrorKind::OptionsNotSet)?
            .to_string();

        Ok(LlmOptions {
            system_message,
            temperature: json["temperature"].as_f32(),
            top_p: json["top_p"].as_f32(),
        })
    }
}

impl BlocklessLlm {
    pub fn new(model_name: &str) -> Result<Self, LlmErrorKind> {
        let mut llm = Self::default();
        llm.set_model(model_name)?;
        Ok(llm)
    }

    pub fn handle(&self) -> Handle {
        self.inner
    }

    pub fn get_model(&self) -> Result<String, LlmErrorKind> {
        let mut buf = [0u8; 256];
        let mut num: u32 = 0;
        let rs = unsafe {
            llm_get_model_response(buf.as_mut_ptr(), buf.len() as _, &mut num, self.inner)
        };
        if rs != 0 {
            return Err(LlmErrorKind::from(rs));
        }
        let model = String::from_utf8(buf[0..num as _].to_vec()).unwrap();
        Ok(model)
    }

    pub fn set_model(&mut self, model_name: &str) -> Result<(), LlmErrorKind> {
        self.model_name = model_name.to_string();
        // handle (self.inner set from runtime)
        let rs = unsafe {
            llm_set_model_request(model_name.as_ptr(), model_name.len() as _, &mut self.inner)
        };
        if rs != 0 {
            return Err(LlmErrorKind::from(rs));
        }

        // validate model is set correctly in host/runtime
        if self.model_name != self.get_model()? {
            eprintln!(
                "Model not set correctly in host/runtime; model_name: {}, model_from_host: {}",
                self.model_name,
                self.get_model()?
            );
            return Err(LlmErrorKind::ModelNotSet);
        }
        Ok(())
    }

    pub fn get_options(&self) -> Result<LlmOptions, LlmErrorKind> {
        let mut buf = [0u8; 256];
        let mut num: u32 = 0;
        let rs = unsafe {
            llm_get_model_options(buf.as_mut_ptr(), buf.len() as _, &mut num, self.inner)
        };
        if rs != 0 {
            println!("Error getting model options: {}", rs);
            return Err(LlmErrorKind::from(rs));
        }

        // Convert buffer slice to Vec<u8> and try to parse into LlmOptions
        LlmOptions::try_from(buf[0..num as usize].to_vec())
    }

    pub fn set_options(&mut self, options: LlmOptions) -> Result<(), LlmErrorKind> {
        let options_json = options.dump();
        self.options = options;
        let rs = unsafe {
            llm_set_model_options_request(
                options_json.as_ptr(),
                options_json.len() as _,
                self.inner,
            )
        };
        if rs != 0 {
            return Err(LlmErrorKind::from(rs));
        }

        // Verify options were set correctly
        let host_options = self.get_options()?;
        if self.options != host_options {
            println!(
                "Options not set correctly in host/runtime; options: {:?}, options_from_host: {:?}",
                self.options, host_options
            );
            return Err(LlmErrorKind::OptionsNotSet);
        }

        Ok(())
    }

    pub fn chat_request(&self, prompt: &str) -> Result<String, LlmErrorKind> {
        // Perform the prompt request
        let rs = unsafe { llm_prompt_request(prompt.as_ptr(), prompt.len() as _, self.inner) };
        if rs != 0 {
            return Err(LlmErrorKind::from(rs));
        }

        // Read the response
        self.get_chat_response()
    }

    fn get_chat_response(&self) -> Result<String, LlmErrorKind> {
        let mut vec = Vec::new();
        loop {
            let mut buf = [0u8; 4096]; // Larger buffer for LLM responses
            let mut num: u32 = 0;
            let rs = unsafe {
                llm_read_prompt_response(buf.as_mut_ptr(), buf.len() as _, &mut num, self.inner)
            };

            if rs != 0 {
                return Err(LlmErrorKind::from(rs));
            }

            match num.cmp(&0) {
                Ordering::Greater => vec.extend_from_slice(&buf[0..num as _]),
                _ => break,
            }
        }
        String::from_utf8(vec).map_err(|_| LlmErrorKind::Utf8Error)
    }

    // TODO: response streaming - not yet supported
    // - read next available chunks
    // - block until chunk is read, repeat until no more chunks
    // pub fn read_response_chunk(&self, buf: &mut [u8]) -> Result<u32, LlmErrorKind> {
    //     let mut num: u32 = 0;
    //     let rs = unsafe {
    //         llm_read_prompt_response(self.inner, buf.as_mut_ptr(), buf.len() as _, &mut num)
    //     };

    //     if rs != 0 {
    //         return Err(LlmErrorKind::from(rs));
    //     }
    //     Ok(num)
    // }
}

impl Drop for BlocklessLlm {
    fn drop(&mut self) {
        unsafe {
            llm_close(self.inner);
        }
    }
}

#[derive(Debug)]
pub enum LlmErrorKind {
    ModelNotSet,
    OptionsNotSet,
    Utf8Error,
    Unknown(i32),
}

impl From<i32> for LlmErrorKind {
    fn from(code: i32) -> Self {
        match code {
            1 => LlmErrorKind::ModelNotSet,
            2 => LlmErrorKind::OptionsNotSet,
            3 => LlmErrorKind::Utf8Error,
            _ => LlmErrorKind::Unknown(code),
        }
    }
}