ask_llm 2.2.2

make a request to whatever llm is the best these days, without hardcoding model/provider
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#![feature(default_field_values)]
use std::{future::Future, pin::Pin};

use eyre::{Result, bail};

mod claude;
mod ollama;

impl Client {
	/// Create a new client using default config (reads from environment).
	pub fn new(config: config::AppConfig) -> Self {
		let backend = Model::default().into_backend(&config);
		Self {
			config,
			backend,
			temperature: None,
			max_tokens: None,
			stop_sequences: None,
			force_json: false,
			files: Vec::new(),
			thinking: ThinkingLevel::default(),
		}
	}

	pub fn model(mut self, model: Model) -> Self {
		self.backend = model.into_backend(&self.config);
		self
	}

	pub fn temperature(mut self, temperature: f32) -> Self {
		self.temperature = Some(temperature);
		self
	}

	pub fn max_tokens(mut self, max_tokens: usize) -> Self {
		self.max_tokens = Some(max_tokens);
		self
	}

	pub fn stop_sequences<T: Into<String>>(mut self, sequences: Vec<T>) -> Self {
		self.stop_sequences = Some(sequences.into_iter().map(Into::into).collect());
		self
	}

	pub fn force_json(mut self) -> Self {
		self.force_json = true;
		self
	}

	pub fn thinking(mut self, level: ThinkingLevel) -> Self {
		self.thinking = level;
		self
	}

	/// Append a file to be included with the request.
	/// Supported media types: application/pdf, text/plain, text/markdown, text/csv,
	/// application/vnd.openxmlformats-officedocument.wordprocessingml.document (docx),
	/// application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx)
	pub fn append_file(mut self, base64_data: String, media_type: String) -> Self {
		self.files.push(FileAttachment { base64_data, media_type });
		self
	}

	/// Append a file from a filesystem path.
	pub fn append_file_from_path(self, path: impl AsRef<std::path::Path>) -> Result<Self> {
		let path = path.as_ref();
		let data = std::fs::read(path)?;
		let base64_data = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &data);
		let media_type = mime_type_from_extension(path.extension().and_then(|s| s.to_str()).unwrap_or(""));
		Ok(self.append_file(base64_data, media_type.to_string()))
	}

	pub async fn ask(&self, message: impl Into<String>) -> Result<Response> {
		let mut conv = Conversation::new();
		conv.add(Role::User, message.into());
		self.conversation(&conv).await
	}

	pub async fn conversation(&self, conv: &Conversation) -> Result<Response> {
		let stop_seqs: Option<Vec<&str>> = self.stop_sequences.as_ref().map(|v| v.iter().map(|s| s.as_str()).collect());
		let request = Request {
			conversation: conv,
			temperature: self.temperature,
			max_tokens: self.max_tokens,
			stop_sequences: stop_seqs,
			force_json: self.force_json,
			files: &self.files,
			thinking: self.thinking,
		};
		let start = std::time::Instant::now();
		let mut response = self.backend.conversation(&request).await?;
		response.duration = start.elapsed();
		Ok(response)
	}
}

impl Model {
	fn into_backend(self, config: &config::AppConfig) -> Box<dyn Backend> {
		match self {
			Model::Cheap => Box::new(ollama::Ollama {
				model: "qwen3.5:4b".to_string(),
				url: "http://localhost:11434/api/chat".to_string(),
			}),
			Model::Translate => Box::new(ollama::Ollama {
				model: "translategemma:4b".to_string(),
				url: "http://localhost:11434/api/chat".to_string(),
			}),
			Model::Fast => {
				let api_key = claude_api_key(config);
				Box::new(claude::Claude {
					api_key,
					model: claude::ClaudeModel::Haiku45,
				})
			}
			Model::Medium => {
				let api_key = claude_api_key(config);
				Box::new(claude::Claude {
					api_key,
					model: claude::ClaudeModel::Sonnet45,
				})
			}
			Model::Slow => {
				let api_key = claude_api_key(config);
				Box::new(claude::Claude {
					api_key,
					model: claude::ClaudeModel::Opus41,
				})
			}
		}
	}
}

impl Message {
	fn new(role: Role, content: impl Into<String>) -> Self {
		Self {
			role,
			content: MessageContent::Text(content.into()),
		}
	}

	pub fn new_with_image(role: Role, base64_data: String, media_type: String) -> Self {
		Self {
			role,
			content: MessageContent::Image { base64_data, media_type },
		}
	}

	pub fn new_with_text_and_images(role: Role, text: String, images: Vec<ImageContent>) -> Self {
		Self {
			role,
			content: MessageContent::TextAndImages { text, images },
		}
	}
}

impl Conversation {
	pub fn new() -> Self {
		Self(Vec::new())
	}

	pub fn new_with_system(system_message: impl Into<String>) -> Self {
		Self(vec![Message::new(Role::System, system_message)])
	}

	pub fn add(&mut self, role: Role, content: impl Into<String>) {
		self.0.push(Message::new(role, content));
	}

	pub fn add_exchange(&mut self, user_message: impl Into<String>, assistant_message: impl Into<String>) {
		self.add(Role::User, user_message);
		self.add(Role::Assistant, assistant_message);
	}
}

impl Response {
	/// Extract codeblocks with optional extension filtering.
	/// If extensions is None or empty, all codeblocks are returned.
	/// Extensions are tried in reverse sorted order (longer extensions first).
	/// Returns an empty Vec if no matching codeblocks are found.
	pub fn extract_codeblocks(&self, extensions: Option<Vec<&str>>) -> Vec<String> {
		let sorted_extensions = extensions.map(|mut exts| {
			exts.sort_by_key(|b| std::cmp::Reverse(b.len()));
			exts
		});

		self.text
			.split("```")
			.enumerate()
			.filter_map(|(i, s)| {
				if i % 2 == 1 {
					match &sorted_extensions {
						Some(exts) if !exts.is_empty() => {
							for ext in exts {
								if s.starts_with(ext) {
									return Some(s.strip_prefix(ext).unwrap().trim().to_string());
								}
							}
							None
						}
						_ => {
							let code = match s.split_once('\n') {
								Some((_, rest)) => rest.trim().to_string(),
								_ => s.trim().to_string(),
							};
							Some(code)
						}
					}
				} else {
					None
				}
			})
			.collect()
	}

	/// Convenience wrapper around [extract_codeblocks](#method.extract_codeblocks).
	/// Returns an error unless exactly one codeblock is found.
	pub fn extract_codeblock(&self, extensions: Option<Vec<&str>>) -> Result<String> {
		let blocks = self.extract_codeblocks(extensions);
		if blocks.len() == 1 {
			Ok(blocks.into_iter().next().unwrap())
		} else {
			bail!("No codeblocks found or more than one codeblock found.")
		}
	}

	pub fn extract_html_tag(&self, tag_name: &str) -> Result<String> {
		let opening_tag = format!("<{tag_name}>");
		let closing_tag = format!("</{tag_name}>");
		let from_start = self.text.split_once(&opening_tag).unwrap().1;
		let extracted = from_start.split_once(&closing_tag).unwrap().0;
		Ok(extracted.to_string())
	}
}

pub mod config;
mod shortcuts;
pub use shortcuts::*;

#[derive(Debug)]
pub struct Response {
	pub text: String,
	pub cost_cents: f32,
	pub duration: std::time::Duration,
	/// Overhead before generation starts (model load for Ollama, network TTFB for Claude).
	pub overhead: std::time::Duration,
	pub model: String,
	pub thinking: ThinkingLevel,
}

#[derive(Clone, Debug, Default)]
pub struct Conversation(pub Vec<Message>);

#[derive(Clone, Debug)]
pub struct Message {
	pub(crate) role: Role,
	pub(crate) content: MessageContent,
}

#[derive(Clone, Debug)]
pub struct ImageContent {
	pub base64_data: String,
	pub media_type: String,
}

#[derive(Clone, Debug)]
pub enum ContentPart {
	Text(String),
	Image { base64_data: String, media_type: String },
	Document { base64_data: String, media_type: String },
}

#[derive(Clone, Debug)]
pub enum MessageContent {
	Text(String),
	Image { base64_data: String, media_type: String },
	TextAndImages { text: String, images: Vec<ImageContent> },
	Document { base64_data: String, media_type: String },
	Mixed { parts: Vec<ContentPart> },
}

#[derive(Clone, Copy, Debug)]
pub enum Role {
	System,
	User,
	Assistant,
}

#[derive(Clone, Copy, Debug, Default)]
pub enum ThinkingLevel {
	#[default]
	None,
	Low,
	Medium,
	High,
}

#[derive(Clone, Copy, Debug, Default, derive_more::FromStr)]
pub enum Model {
	Cheap,
	Translate,
	Fast,
	#[default]
	Medium,
	Slow,
}

#[derive(Clone, Debug)]
pub struct FileAttachment {
	pub base64_data: String,
	pub media_type: String,
}

/// Client for interacting with LLMs.
///
/// Default settings produce a simple oneshot call with Model::Medium.
pub struct Client {
	config: config::AppConfig,
	backend: Box<dyn Backend>,
	temperature: Option<f32>,
	max_tokens: Option<usize>,
	stop_sequences: Option<Vec<String>>,
	force_json: bool,
	files: Vec<FileAttachment>,
	thinking: ThinkingLevel,
}
pub(crate) trait Backend: Send + Sync {
	fn conversation<'a>(&'a self, request: &'a Request<'a>) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'a>>;
}
fn claude_api_key(config: &config::AppConfig) -> String {
	config
		.claude_token
		.clone()
		.or_else(|| std::env::var("CLAUDE_TOKEN").ok())
		.expect("CLAUDE_TOKEN not set in config or environment")
}

pub(crate) struct Request<'a> {
	pub conversation: &'a Conversation,
	pub temperature: Option<f32>,
	pub max_tokens: Option<usize>,
	pub stop_sequences: Option<Vec<&'a str>>,
	pub force_json: bool,
	pub files: &'a [FileAttachment],
	pub thinking: ThinkingLevel,
}

impl std::fmt::Debug for Client {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Client")
			.field("temperature", &self.temperature)
			.field("max_tokens", &self.max_tokens)
			.field("stop_sequences", &self.stop_sequences)
			.field("force_json", &self.force_json)
			.field("thinking", &self.thinking)
			.field("files", &self.files)
			.finish_non_exhaustive()
	}
}

fn mime_type_from_extension(ext: &str) -> &'static str {
	match ext.to_lowercase().as_str() {
		"pdf" => "application/pdf",
		"txt" => "text/plain",
		"md" => "text/markdown",
		"csv" => "text/csv",
		"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
		"png" => "image/png",
		"jpg" | "jpeg" => "image/jpeg",
		"gif" => "image/gif",
		"webp" => "image/webp",
		_ => "application/octet-stream",
	}
}

impl Default for Client {
	fn default() -> Self {
		Self::new(config::AppConfig::default())
	}
}

impl std::fmt::Display for ThinkingLevel {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::None => write!(f, "none"),
			Self::Low => write!(f, "low"),
			Self::Medium => write!(f, "medium"),
			Self::High => write!(f, "high"),
		}
	}
}

impl std::fmt::Display for Response {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let secs = self.duration.as_secs_f32();
		let overhead = self.overhead.as_secs_f32();
		let gen_secs = secs - overhead;
		let chars = self.text.len();
		let ms_per_char = if chars > 0 { gen_secs * 1000.0 / chars as f32 } else { 0.0 };
		write!(
			f,
			"[model: {} | thinking: {} | cost: {:.4}ยข | overhead: {overhead:.1}s | gen: {gen_secs:.1}s | {ms_per_char:.1}ms/char]",
			self.model, self.thinking, self.cost_cents
		)
	}
}