forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_listing_example.rs
More file actions
34 lines (28 loc) · 1.09 KB
/
model_listing_example.rs
File metadata and controls
34 lines (28 loc) · 1.09 KB
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
//! This example demonstrates how to list available models from an LLM backend.
//! It uses the `ModelListRequest` structure to filter models based on specific criteria.
//! The example demonstrates how to build an LLM client, create a model list request,
//! and handle the response from the LLM backend.
use llm::{
builder::{LLMBackend, LLMBuilder},
models::ModelListRequest,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = std::env::var("OPENAI_API_KEY").unwrap_or("sk-TESTKEY".into());
let llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(api_key)
.build()
.expect("Failed to build LLM (OpenAI)");
let request: Option<&ModelListRequest> = None;
match llm.list_models(request).await {
Ok(response) => {
println!("Models available for backend {:?}:", response.get_backend());
for model_id in response.get_models() {
println!("- {model_id}");
}
}
Err(e) => eprintln!("Error listing models: {e}"),
}
Ok(())
}