A local-first private AI assistant for everyday use. Runs on-device models with encrypted P2P sync, and supports sharing chats publicly on ATProto.
10
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1 from tileshq/feat/modelfile-parser

Modelfile Parser v1

authored by

Anandu Pavanan and committed by
GitHub
34b52153 e38301e5

+802 -13
+18
Cargo.lock
··· 3 3 version = 4 4 4 5 5 [[package]] 6 + name = "memchr" 7 + version = "2.7.6" 8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" 10 + 11 + [[package]] 12 + name = "nom" 13 + version = "8.0.0" 14 + source = "registry+https://github.com/rust-lang/crates.io-index" 15 + checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" 16 + dependencies = [ 17 + "memchr", 18 + ] 19 + 20 + [[package]] 6 21 name = "tilekit" 7 22 version = "0.1.0" 23 + dependencies = [ 24 + "nom", 25 + ]
+1
Cargo.toml
··· 4 4 edition = "2024" 5 5 6 6 [dependencies] 7 + nom = "8"
+3
b.modelfile
··· 1 + # Using llama model yo 2 + FROM llama3.2 3 + MESSAGE user hi
+26
fixtures/a.modelfile
··· 1 + # Modelfile generated by "ollama show" 2 + # To build a new Modelfile based on this one, replace the FROM line with: 3 + FROM llama3.2:latest 4 + # FROM /Users/pdevine/.ollama/models/blobs/sha256-00e1317cbf74d901080d7100f57580ba8dd8de57203072dc6f668324ba545f29 5 + TEMPLATE """{{ if .System }}<|start_header_id|>system<|end_header_id|> 6 + 7 + {{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|> 8 + 9 + {{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|> 10 + 11 + {{ .Response }}<|eot_id|>""" 12 + 13 + PARAMETER stop "<|start_header_id|>" 14 + PARAMETER stop "<|end_header_id|>" 15 + PARAMETER stop "<|eot_id|>" 16 + PARAMETER stop "<|reserved_special_token" 17 + 18 + MESSAGE user """Is Toronto in Canada? 19 + or Is cologne in france""" 20 + MESSAGE assistant yes 21 + MESSAGE user Is Sacramento in Canada? 22 + MESSAGE assistant no 23 + MESSAGE user Is Ontario in Canada? 24 + MESSAGE assistant yes 25 + # FROM llama3.2 26 +
+14
fixtures/llama_bad.Modelfile
··· 1 + # Modelfile generated by "ollama show" 2 + # To build a new Modelfile based on this, replace FROM with: 3 + # FROM x/llama3.2-vision:latest 4 + 5 + FROM /mnt/space/ollama/models/blobs/sha256-652e85aa1e14c9087a4ccc3ab516fb794cbcf152f8b4b8d3c0b828da4ada62d9 6 + FROM /mnt/space/ollama/models/blobs/sha256-622429e8d31810962dd984bc98559e706db2fb1d40e99cb073beb7148d909d73 7 + TEMPLATE "{{- range $i, $_ := .Messages }}<|start_header_id|>{{ .Role }}<|end_header_id|> 8 + 9 + {{ .Content }}<|eot_id|> 10 + {{- end }}<|start_header_id|>assistant<|end_header_id|> 11 + 12 + " 13 + PARAMETER temperature 0 14 + PARAMETER top_p 0.9
+223
fixtures/mistral.modelfile
··· 1 + # Modelfile generated by "ollama show" 2 + # To build a new Modelfile based on this, replace FROM with: 3 + # FROM mistral-nemo:latest 4 + 5 + FROM /mnt/space/ollama/models/blobs/sha256-b559938ab7a0392fc9ea9675b82280f2a15669ec3e0e0fc491c9cb0a7681cf94 6 + TEMPLATE """{{- range $i, $_ := .Messages }} 7 + {{- if eq .Role "user" }} 8 + {{- if and $.Tools (le (len (slice $.Messages $i)) 2) }}[AVAILABLE_TOOLS] {{ $.Tools }}[/AVAILABLE_TOOLS] 9 + {{- end }}[INST] {{ if and $.System (eq (len (slice $.Messages $i)) 1) }}{{ $.System }} 10 + 11 + {{ end }}{{ .Content }}[/INST] 12 + {{- else if eq .Role "assistant" }} 13 + {{- if .Content }} {{ .Content }}{{ if not (eq (len (slice $.Messages $i)) 1) }}</s>{{ end }} 14 + {{- else if .ToolCalls }}[TOOL_CALLS] [ 15 + {{- range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} 16 + {{- end }}]</s> 17 + {{- end }} 18 + {{- else if eq .Role "tool" }}[TOOL_RESULTS] {"content": {{ .Content }}} [/TOOL_RESULTS] 19 + {{- end }} 20 + {{- end }}""" 21 + PARAMETER stop [INST] 22 + PARAMETER stop [/INST] 23 + LICENSE """ Apache License 24 + Version 2.0, January 2004 25 + http://www.apache.org/licenses/ 26 + 27 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 28 + 29 + 1. Definitions. 30 + 31 + "License" shall mean the terms and conditions for use, reproduction, 32 + and distribution as defined by Sections 1 through 9 of this document. 33 + 34 + "Licensor" shall mean the copyright owner or entity authorized by 35 + the copyright owner that is granting the License. 36 + 37 + "Legal Entity" shall mean the union of the acting entity and all 38 + other entities that control, are controlled by, or are under common 39 + control with that entity. For the purposes of this definition, 40 + "control" means (i) the power, direct or indirect, to cause the 41 + direction or management of such entity, whether by contract or 42 + otherwise, or (ii) ownership of fifty percent (50%) or more of the 43 + outstanding shares, or (iii) beneficial ownership of such entity. 44 + 45 + "You" (or "Your") shall mean an individual or Legal Entity 46 + exercising permissions granted by this License. 47 + 48 + "Source" form shall mean the preferred form for making modifications, 49 + including but not limited to software source code, documentation 50 + source, and configuration files. 51 + 52 + "Object" form shall mean any form resulting from mechanical 53 + transformation or translation of a Source form, including but 54 + not limited to compiled object code, generated documentation, 55 + and conversions to other media types. 56 + 57 + "Work" shall mean the work of authorship, whether in Source or 58 + Object form, made available under the License, as indicated by a 59 + copyright notice that is included in or attached to the work 60 + (an example is provided in the Appendix below). 61 + 62 + "Derivative Works" shall mean any work, whether in Source or Object 63 + form, that is based on (or derived from) the Work and for which the 64 + editorial revisions, annotations, elaborations, or other modifications 65 + represent, as a whole, an original work of authorship. For the purposes 66 + of this License, Derivative Works shall not include works that remain 67 + separable from, or merely link (or bind by name) to the interfaces of, 68 + the Work and Derivative Works thereof. 69 + 70 + "Contribution" shall mean any work of authorship, including 71 + the original version of the Work and any modifications or additions 72 + to that Work or Derivative Works thereof, that is intentionally 73 + submitted to Licensor for inclusion in the Work by the copyright owner 74 + or by an individual or Legal Entity authorized to submit on behalf of 75 + the copyright owner. For the purposes of this definition, "submitted" 76 + means any form of electronic, verbal, or written communication sent 77 + to the Licensor or its representatives, including but not limited to 78 + communication on electronic mailing lists, source code control systems, 79 + and issue tracking systems that are managed by, or on behalf of, the 80 + Licensor for the purpose of discussing and improving the Work, but 81 + excluding communication that is conspicuously marked or otherwise 82 + designated in writing by the copyright owner as "Not a Contribution." 83 + 84 + "Contributor" shall mean Licensor and any individual or Legal Entity 85 + on behalf of whom a Contribution has been received by Licensor and 86 + subsequently incorporated within the Work. 87 + 88 + 2. Grant of Copyright License. Subject to the terms and conditions of 89 + this License, each Contributor hereby grants to You a perpetual, 90 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable 91 + copyright license to reproduce, prepare Derivative Works of, 92 + publicly display, publicly perform, sublicense, and distribute the 93 + Work and such Derivative Works in Source or Object form. 94 + 95 + 3. Grant of Patent License. Subject to the terms and conditions of 96 + this License, each Contributor hereby grants to You a perpetual, 97 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable 98 + (except as stated in this section) patent license to make, have made, 99 + use, offer to sell, sell, import, and otherwise transfer the Work, 100 + where such license applies only to those patent claims licensable 101 + by such Contributor that are necessarily infringed by their 102 + Contribution(s) alone or by combination of their Contribution(s) 103 + with the Work to which such Contribution(s) was submitted. If You 104 + institute patent litigation against any entity (including a 105 + cross-claim or counterclaim in a lawsuit) alleging that the Work 106 + or a Contribution incorporated within the Work constitutes direct 107 + or contributory patent infringement, then any patent licenses 108 + granted to You under this License for that Work shall terminate 109 + as of the date such litigation is filed. 110 + 111 + 4. Redistribution. You may reproduce and distribute copies of the 112 + Work or Derivative Works thereof in any medium, with or without 113 + modifications, and in Source or Object form, provided that You 114 + meet the following conditions: 115 + 116 + (a) You must give any other recipients of the Work or 117 + Derivative Works a copy of this License; and 118 + 119 + (b) You must cause any modified files to carry prominent notices 120 + stating that You changed the files; and 121 + 122 + (c) You must retain, in the Source form of any Derivative Works 123 + that You distribute, all copyright, patent, trademark, and 124 + attribution notices from the Source form of the Work, 125 + excluding those notices that do not pertain to any part of 126 + the Derivative Works; and 127 + 128 + (d) If the Work includes a "NOTICE" text file as part of its 129 + distribution, then any Derivative Works that You distribute must 130 + include a readable copy of the attribution notices contained 131 + within such NOTICE file, excluding those notices that do not 132 + pertain to any part of the Derivative Works, in at least one 133 + of the following places: within a NOTICE text file distributed 134 + as part of the Derivative Works; within the Source form or 135 + documentation, if provided along with the Derivative Works; or, 136 + within a display generated by the Derivative Works, if and 137 + wherever such third-party notices normally appear. The contents 138 + of the NOTICE file are for informational purposes only and 139 + do not modify the License. You may add Your own attribution 140 + notices within Derivative Works that You distribute, alongside 141 + or as an addendum to the NOTICE text from the Work, provided 142 + that such additional attribution notices cannot be construed 143 + as modifying the License. 144 + 145 + You may add Your own copyright statement to Your modifications and 146 + may provide additional or different license terms and conditions 147 + for use, reproduction, or distribution of Your modifications, or 148 + for any such Derivative Works as a whole, provided Your use, 149 + reproduction, and distribution of the Work otherwise complies with 150 + the conditions stated in this License. 151 + 152 + 5. Submission of Contributions. Unless You explicitly state otherwise, 153 + any Contribution intentionally submitted for inclusion in the Work 154 + by You to the Licensor shall be under the terms and conditions of 155 + this License, without any additional terms or conditions. 156 + Notwithstanding the above, nothing herein shall supersede or modify 157 + the terms of any separate license agreement you may have executed 158 + with Licensor regarding such Contributions. 159 + 160 + 6. Trademarks. This License does not grant permission to use the trade 161 + names, trademarks, service marks, or product names of the Licensor, 162 + except as required for reasonable and customary use in describing the 163 + origin of the Work and reproducing the content of the NOTICE file. 164 + 165 + 7. Disclaimer of Warranty. Unless required by applicable law or 166 + agreed to in writing, Licensor provides the Work (and each 167 + Contributor provides its Contributions) on an "AS IS" BASIS, 168 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 169 + implied, including, without limitation, any warranties or conditions 170 + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 171 + PARTICULAR PURPOSE. You are solely responsible for determining the 172 + appropriateness of using or redistributing the Work and assume any 173 + risks associated with Your exercise of permissions under this License. 174 + 175 + 8. Limitation of Liability. In no event and under no legal theory, 176 + whether in tort (including negligence), contract, or otherwise, 177 + unless required by applicable law (such as deliberate and grossly 178 + negligent acts) or agreed to in writing, shall any Contributor be 179 + liable to You for damages, including any direct, indirect, special, 180 + incidental, or consequential damages of any character arising as a 181 + result of this License or out of the use or inability to use the 182 + Work (including but not limited to damages for loss of goodwill, 183 + work stoppage, computer failure or malfunction, or any and all 184 + other commercial damages or losses), even if such Contributor 185 + has been advised of the possibility of such damages. 186 + 187 + 9. Accepting Warranty or Additional Liability. While redistributing 188 + the Work or Derivative Works thereof, You may choose to offer, 189 + and charge a fee for, acceptance of support, warranty, indemnity, 190 + or other liability obligations and/or rights consistent with this 191 + License. However, in accepting such obligations, You may act only 192 + on Your own behalf and on Your sole responsibility, not on behalf 193 + of any other Contributor, and only if You agree to indemnify, 194 + defend, and hold each Contributor harmless for any liability 195 + incurred by, or claims asserted against, such Contributor by reason 196 + of your accepting any such warranty or additional liability. 197 + 198 + END OF TERMS AND CONDITIONS 199 + 200 + APPENDIX: How to apply the Apache License to your work. 201 + 202 + To apply the Apache License to your work, attach the following 203 + boilerplate notice, with the fields enclosed by brackets "[]" 204 + replaced with your own identifying information. (Don't include 205 + the brackets!) The text should be enclosed in the appropriate 206 + comment syntax for the file format. We also recommend that a 207 + file or class name and description of purpose be included on the 208 + same "printed page" as the copyright notice for easier 209 + identification within third-party archives. 210 + 211 + Copyright [yyyy] [name of copyright owner] 212 + 213 + Licensed under the Apache License, Version 2.0 (the "License"); 214 + you may not use this file except in compliance with the License. 215 + You may obtain a copy of the License at 216 + 217 + http://www.apache.org/licenses/LICENSE-2.0 218 + 219 + Unless required by applicable law or agreed to in writing, software 220 + distributed under the License is distributed on an "AS IS" BASIS, 221 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 222 + See the License for the specific language governing permissions and 223 + limitations under the License."""
+2 -12
src/lib.rs
··· 1 - pub fn add(left: u64, right: u64) -> u64 { 2 - left + right 3 - } 1 + pub mod modelfile; 4 2 5 3 #[cfg(test)] 6 - mod tests { 7 - use super::*; 8 - 9 - #[test] 10 - fn it_works() { 11 - let result = add(2, 2); 12 - assert_eq!(result, 4); 13 - } 14 - } 4 + mod tests {}
+9 -1
src/main.rs
··· 1 - pub fn main() { 1 + use std::error::Error; 2 + 3 + use tilekit::modelfile; 4 + pub fn main() -> Result<(), Box<dyn Error>> { 2 5 println!("{}:{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); 6 + 7 + let modf = modelfile::parse_from_file("assets/tests/mistral.modelfile"); 8 + 9 + println!("{:?}", modf); 10 + Ok(()) 3 11 }
+506
src/modelfile.rs
··· 1 + // A parser for ollama modelfile 2 + // https://github.com/ollama/ollama/blob/main/docs/modelfile.md 3 + 4 + // Modelfile grammar 5 + // command -> Instruction arguments* 6 + // Instruction -> "FROM" | "PARAMETER" | "TEMPLATE"... 7 + // arguments -> WORD | quoted_string | multiline_string 8 + // quoted_string -> "<str>" 9 + // multiline_string -> """<str>""" 10 + 11 + use std::{fmt::Display, fs, str::FromStr}; 12 + 13 + use nom::{ 14 + AsChar, IResult, Parser, 15 + branch::alt, 16 + bytes::complete::{tag_no_case, take_until1, take_while1}, 17 + character::complete::multispace0, 18 + combinator::map, 19 + multi::separated_list1, 20 + sequence::{delimited, pair}, 21 + }; 22 + 23 + #[allow(dead_code)] 24 + #[derive(Debug, Clone)] 25 + enum ParamValue { 26 + Int(i32), 27 + Float(f32), 28 + Str(String), 29 + } 30 + 31 + #[derive(Debug, Clone)] 32 + enum Role { 33 + System, 34 + User, 35 + Assistant, 36 + } 37 + 38 + #[derive(Clone, Debug)] 39 + enum Output<'a> { 40 + Single(&'a str), 41 + Pair((&'a str, &'a str)), 42 + } 43 + 44 + impl FromStr for Role { 45 + type Err = String; 46 + fn from_str(s: &str) -> Result<Self, Self::Err> { 47 + match s.to_lowercase().as_str() { 48 + "system" => Ok(Role::System), 49 + "user" => Ok(Role::User), 50 + "assistant" => Ok(Role::Assistant), 51 + _ => Err("Invalid Role".to_owned()), 52 + } 53 + } 54 + } 55 + #[allow(dead_code)] 56 + #[derive(Debug, Clone)] 57 + struct Parameter { 58 + param_type: String, 59 + value: ParamValue, 60 + } 61 + 62 + #[allow(dead_code)] 63 + #[derive(Debug, Clone)] 64 + struct Message { 65 + role: Role, 66 + message: String, 67 + } 68 + 69 + impl Parameter { 70 + fn new(param_type: String, value: ParamValue) -> Self { 71 + Self { param_type, value } 72 + } 73 + } 74 + 75 + #[derive(Debug, Clone)] 76 + pub struct Modelfile { 77 + from: Option<String>, 78 + parameters: Vec<Parameter>, 79 + template: Option<String>, 80 + adapter: Option<String>, 81 + system: Option<String>, 82 + license: Option<String>, 83 + messages: Vec<Message>, 84 + data: Vec<String>, 85 + errors: Vec<String>, 86 + } 87 + 88 + impl Modelfile { 89 + pub fn new() -> Self { 90 + Self { 91 + from: None, 92 + data: vec![], 93 + parameters: vec![], 94 + template: None, 95 + messages: vec![], 96 + license: None, 97 + adapter: None, 98 + system: None, 99 + errors: vec![], 100 + } 101 + } 102 + 103 + pub fn add_from(&mut self, value: &str) -> Result<(), String> { 104 + if self.from.is_some() { 105 + let error = "Modelfile can only have one FROM instruction".to_owned(); 106 + self.errors.push(error.clone()); 107 + Err(error) 108 + } else { 109 + self.from = Some(value.to_owned()); 110 + self.data.push(format!("FROM {}", value)); 111 + Ok(()) 112 + } 113 + } 114 + 115 + pub fn add_template(&mut self, value: &str) -> Result<(), String> { 116 + if self.template.is_some() { 117 + let error = "Modelfile can only have one TEMPLATE instruction".to_owned(); 118 + self.errors.push(error.clone()); 119 + Err(error) 120 + } else { 121 + self.template = Some(value.to_owned()); 122 + self.data.push(format!("TEMPLATE {}", value)); 123 + Ok(()) 124 + } 125 + } 126 + 127 + pub fn add_license(&mut self, value: &str) -> Result<(), String> { 128 + if self.license.is_some() { 129 + let error = "Modelfile can only have one LICENSE instruction".to_owned(); 130 + self.errors.push(error.clone()); 131 + Err(error) 132 + } else { 133 + self.license = Some(value.to_owned()); 134 + self.data.push(format!("LICENSE {}", value)); 135 + Ok(()) 136 + } 137 + } 138 + 139 + pub fn add_adapter(&mut self, value: &str) -> Result<(), String> { 140 + if self.adapter.is_some() { 141 + let error = "Modelfile can only have one ADAPTER instruction".to_owned(); 142 + self.errors.push(error.clone()); 143 + Err(error) 144 + } else { 145 + self.adapter = Some(value.to_owned()); 146 + self.data.push(format!("ADAPTER {}", value)); 147 + Ok(()) 148 + } 149 + } 150 + 151 + pub fn add_system(&mut self, value: &str) -> Result<(), String> { 152 + if self.system.is_some() { 153 + let error = "Modelfile can only have one SYSTEM instruction".to_owned(); 154 + self.errors.push(error.clone()); 155 + 156 + self.data.push(format!("SYSTEM {}", value)); 157 + Err(error) 158 + } else { 159 + self.system = Some(value.to_owned()); 160 + Ok(()) 161 + } 162 + } 163 + 164 + pub fn add_comment(&mut self, value: &str) -> Result<(), String> { 165 + self.data.push(format!("# {}", value)); 166 + Ok(()) 167 + } 168 + 169 + pub fn add_parameter(&mut self, param_type: &str, param_value: &str) -> Result<(), String> { 170 + match parse_parameter(param_type, param_value) { 171 + Ok(parameter) => { 172 + self.parameters.push(parameter); 173 + self.data 174 + .push(format!("PARAMETER {} {}", param_type, param_value)); 175 + Ok(()) 176 + } 177 + Err(err) => { 178 + self.errors.push(err.clone()); 179 + Err(err) 180 + } 181 + } 182 + } 183 + 184 + pub fn add_message(&mut self, role: &str, message: &str) -> Result<(), String> { 185 + match parse_message(role, message) { 186 + Ok(msg) => { 187 + self.messages.push(msg); 188 + self.data.push(format!("MESSAGE {} {}", role, message)); 189 + Ok(()) 190 + } 191 + Err(err) => { 192 + self.errors.push(err.clone()); 193 + Err(err) 194 + } 195 + } 196 + } 197 + 198 + pub fn build(&mut self) -> Result<(), String> { 199 + if self.from.is_none() { 200 + let error = String::from("Modelfile should need a FROM instruction"); 201 + self.errors.push(error.clone()); 202 + Err(error) 203 + } else { 204 + Ok(()) 205 + } 206 + } 207 + } 208 + 209 + impl Default for Modelfile { 210 + fn default() -> Self { 211 + Modelfile::new() 212 + } 213 + } 214 + 215 + impl FromStr for Modelfile { 216 + type Err = String; 217 + fn from_str(s: &str) -> Result<Self, Self::Err> { 218 + parse(s) 219 + } 220 + } 221 + 222 + impl Display for Modelfile { 223 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 224 + write!(f, "{}", self.data.join("\n")) 225 + } 226 + } 227 + 228 + pub fn parse_from_file(path: &str) -> Result<Modelfile, String> { 229 + let content = fs::read_to_string(path).expect("File read failed"); 230 + parse(content.as_str()) 231 + } 232 + 233 + pub fn parse(input: &str) -> Result<Modelfile, String> { 234 + match parse_file(input) { 235 + Ok((_rest, parsed_data)) => create_modelfile(parsed_data.clone()), 236 + Err(err) => Err(format!("Modelfile failed to parse due to {:?}", err)), 237 + } 238 + } 239 + 240 + fn parse_file(input: &str) -> IResult<&str, Vec<(&str, Output)>> { 241 + separated_list1(multispace0, parse_command).parse(input) 242 + } 243 + 244 + fn parse_command(input: &str) -> IResult<&str, (&str, Output)> { 245 + pair( 246 + delimited(multispace0, parse_instruction, multispace0), 247 + alt(( 248 + map(parse_multiquote, Output::Single), 249 + map(parse_singlequote, Output::Single), 250 + map(parse_multi_arguments, Output::Pair), 251 + map(parse_singleline, Output::Single), 252 + )), 253 + ) 254 + .parse(input) 255 + } 256 + 257 + fn parse_instruction(input: &str) -> IResult<&str, &str> { 258 + alt(( 259 + tag_no_case("FROM"), 260 + tag_no_case("PARAMETER"), 261 + tag_no_case("TEMPLATE"), 262 + tag_no_case("SYSTEM"), 263 + tag_no_case("ADAPTER"), 264 + tag_no_case("LICENSE"), 265 + tag_no_case("MESSAGE"), 266 + tag_no_case("#"), 267 + )) 268 + .parse(input) 269 + } 270 + 271 + fn parse_multi_arguments(input: &str) -> IResult<&str, (&str, &str)> { 272 + pair( 273 + delimited( 274 + multispace0, 275 + alt(( 276 + tag_no_case("stop"), 277 + tag_no_case("num_ctx"), 278 + tag_no_case("repeat_last_n"), 279 + tag_no_case("temperature"), 280 + tag_no_case("seed"), 281 + tag_no_case("top_k"), 282 + tag_no_case("top_p"), 283 + tag_no_case("min_p"), 284 + tag_no_case("num_predict"), 285 + tag_no_case("repeat_penalty"), 286 + tag_no_case("user"), 287 + tag_no_case("assistant"), 288 + tag_no_case("system"), 289 + )), 290 + multispace0, 291 + ), 292 + alt((parse_multiquote, parse_singlequote, parse_singleline)), 293 + ) 294 + .parse(input) 295 + } 296 + 297 + fn parse_multiquote(input: &str) -> IResult<&str, &str> { 298 + delimited( 299 + tag_no_case("\"\"\""), 300 + take_until1("\"\"\""), 301 + tag_no_case("\"\"\""), 302 + ) 303 + .parse(input) 304 + } 305 + 306 + fn parse_singlequote(input: &str) -> IResult<&str, &str> { 307 + delimited(tag_no_case("\""), take_until1("\""), tag_no_case("\"")).parse(input) 308 + } 309 + fn parse_singleline(input: &str) -> IResult<&str, &str> { 310 + delimited( 311 + multispace0, 312 + take_while1(|c: char| !c.is_newline()), 313 + multispace0, 314 + ) 315 + .parse(input) 316 + } 317 + fn create_modelfile(commands: Vec<(&str, Output)>) -> Result<Modelfile, String> { 318 + // TODO: There might be a better way 319 + let mut modelfile: Modelfile = Modelfile::new(); 320 + for command in commands { 321 + let _ = match (command.0.to_lowercase().as_str(), command.1) { 322 + //TODO: Can add validations for path if its a gguf file later 323 + ("from", Output::Single(from)) => modelfile.add_from(from), 324 + ("parameter", Output::Pair((param, argument))) => { 325 + modelfile.add_parameter(param, argument) 326 + } 327 + ("template", Output::Single(template)) => modelfile.add_template(template), 328 + ("system", Output::Single(system)) => modelfile.add_system(system), 329 + ("adapter", Output::Single(adapter)) => modelfile.add_adapter(adapter), 330 + ("message", Output::Pair((role, message))) => modelfile.add_message(role, message), 331 + ("license", Output::Single(license)) => modelfile.add_license(license), 332 + ("#", Output::Single(comment)) => modelfile.add_comment(comment), 333 + _ => { 334 + modelfile.errors.push(String::from("Invalid instruction")); 335 + Ok(()) 336 + } 337 + }; 338 + } 339 + 340 + modelfile.build()?; 341 + if modelfile.errors.is_empty() { 342 + Ok(modelfile.clone()) 343 + } else { 344 + let error = modelfile.errors.join(" , "); 345 + Err(error) 346 + } 347 + } 348 + 349 + fn parse_parameter(param: &str, argument: &str) -> Result<Parameter, String> { 350 + let param_type: String = param.to_lowercase(); 351 + match (param_type.as_str(), argument) { 352 + ("num_ctx", value) => parse_int(param_type, value), 353 + ("repeat_last_n", value) => parse_int(param_type, value), 354 + ("repeat_penalty", value) => parse_float(param_type, value), 355 + ("temperature", value) => parse_float(param_type, value), 356 + ("seed", value) => parse_int(param_type, value), 357 + 358 + ("stop", value) => Ok(Parameter::new( 359 + param_type, 360 + ParamValue::Str(value.to_owned()), 361 + )), 362 + 363 + ("num_predict", value) => parse_int(param_type, value), 364 + 365 + ("top_k", value) => parse_int(param_type, value), 366 + 367 + ("top_p", value) => parse_float(param_type, value), 368 + 369 + ("min_p", value) => parse_float(param_type, value), 370 + _ => Err("Invalid Parameter type".to_owned()), 371 + } 372 + } 373 + 374 + fn parse_int(param_type: String, value: &str) -> Result<Parameter, String> { 375 + if let Ok(parsed_val) = value.parse::<i32>() { 376 + Ok(Parameter::new(param_type, ParamValue::Int(parsed_val))) 377 + } else { 378 + Err(format!("{} not an Integer", param_type)) 379 + } 380 + } 381 + 382 + fn parse_float(param_type: String, value: &str) -> Result<Parameter, String> { 383 + if let Ok(parsed_val) = value.parse::<f32>() { 384 + Ok(Parameter::new(param_type, ParamValue::Float(parsed_val))) 385 + } else { 386 + Err(format!("{} not a Float", param_type)) 387 + } 388 + } 389 + 390 + fn parse_message(role: &str, message: &str) -> Result<Message, String> { 391 + let binding = role.to_lowercase(); 392 + let param_type = binding.as_str(); 393 + if let Ok(role) = param_type.parse::<Role>() { 394 + Ok(Message { 395 + role, 396 + message: message.to_owned(), 397 + }) 398 + } else { 399 + Err(format!("{} not a valid role", param_type)) 400 + } 401 + } 402 + 403 + #[cfg(test)] 404 + mod tests { 405 + use std::error::Error; 406 + 407 + use nom::Err; 408 + 409 + use super::*; 410 + 411 + #[test] 412 + fn test_empty_modelfile() { 413 + let res = parse(""); 414 + assert!(res.is_err()); 415 + } 416 + #[test] 417 + fn test_wrong_instruction() { 418 + assert!(parse("FRO llama").is_err()); 419 + } 420 + 421 + #[test] 422 + fn test_valid_modelfile() { 423 + let modelfile = " 424 + FROM llama3.2 425 + PARAMETER num_ctx 4096 426 + "; 427 + 428 + assert!(parse(modelfile).is_ok()); 429 + } 430 + 431 + #[test] 432 + fn test_parse_modelfile_without_from() { 433 + let modelfile = " 434 + PARAMETER num_ctx 4096 435 + "; 436 + assert!(parse(modelfile).is_err()) 437 + } 438 + 439 + #[test] 440 + fn test_parse_multiline_single_arguments() { 441 + let modelfile = " 442 + FROM llama3.2 443 + SYSTEM \"\"\" 444 + You are a bot 445 + You also not a bot 446 + \"\"\""; 447 + let res = parse(modelfile); 448 + println!("{:?}", res); 449 + assert!(parse(modelfile).is_ok()) 450 + } 451 + 452 + #[test] 453 + fn test_modelfile_builder() -> Result<(), Box<dyn Error>> { 454 + let mut modelfile = Modelfile::new(); 455 + modelfile.add_from("llama3.2")?; 456 + modelfile.add_parameter("num_ctx", "4096")?; 457 + assert!(modelfile.build().is_ok()); 458 + Ok(()) 459 + } 460 + 461 + #[test] 462 + fn test_building_over_parsed_modelfile() -> Result<(), Box<dyn Error>> { 463 + let modelfile_content = " 464 + FROM llama3.2 465 + PARAMETER num_ctx 4096 466 + "; 467 + let mut modelfile = parse(modelfile_content)?; 468 + modelfile.add_parameter("temperature", "3.2")?; 469 + assert!(modelfile.build().is_ok()); 470 + Ok(()) 471 + } 472 + 473 + #[test] 474 + fn test_parse_modelfile_from_file() { 475 + let modelfile = parse_from_file("fixtures/a.modelfile").unwrap(); 476 + assert_eq!(modelfile.from, Some("llama3.2:latest".to_owned())) 477 + } 478 + 479 + #[test] 480 + fn test_e2e_pipeline() -> Result<(), Box<dyn Error>> { 481 + let modelfile_content = " 482 + FROM llama3.2 483 + PARAMETER num_ctx 4096 484 + "; 485 + let mut modelfile = parse(modelfile_content)?; 486 + modelfile.add_parameter("temperature", "3.2")?; 487 + assert!(modelfile.build().is_ok()); 488 + let modelfile_str = modelfile.to_string(); 489 + assert!(modelfile_str.contains("temperature")); 490 + Ok(()) 491 + } 492 + 493 + #[test] 494 + fn test_parse_modelfile_from_file_mistral() -> Result<(), Box<dyn Error>> { 495 + let modelfile = parse_from_file("fixtures/mistral.modelfile")?; 496 + // There should be 8 tokens in the modelfile including comments 497 + assert_eq!(modelfile.data.len(), 8); 498 + Ok(()) 499 + } 500 + 501 + #[test] 502 + fn test_parse_bad_modelfile() { 503 + // modelfile has more than 2 FROM 504 + assert!(parse_from_file("fixtures/llama_bad.Modelfile").is_err()) 505 + } 506 + }