openrouter_rs/api/
models.rs

1use crate::{error::OpenRouterError, types::ApiResponse, utils::handle_error};
2use reqwest::Client;
3use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, Debug)]
6pub struct Model {
7    id: String,
8    name: String,
9    created: u64,
10    description: String,
11    context_length: u32,
12    architecture: Architecture,
13    top_provider: TopProvider,
14    pricing: Pricing,
15    per_request_limits: Option<std::collections::HashMap<String, String>>,
16}
17
18#[derive(Serialize, Deserialize, Debug)]
19pub struct Architecture {
20    modality: String,
21    tokenizer: String,
22    instruct_type: Option<String>,
23}
24
25#[derive(Serialize, Deserialize, Debug)]
26pub struct TopProvider {
27    context_length: f64,
28    max_completion_tokens: f64,
29    is_moderated: bool,
30}
31
32#[derive(Serialize, Deserialize, Debug)]
33pub struct Pricing {
34    prompt: String,
35    completion: String,
36    image: String,
37    request: String,
38    input_cache_read: String,
39    input_cache_write: String,
40    web_search: String,
41    internal_reasoning: String,
42}
43
44#[derive(Serialize, Deserialize, Debug)]
45pub struct Endpoint {
46    name: String,
47    context_length: f64,
48    pricing: EndpointPricing,
49    provider_name: String,
50    supported_parameters: Vec<String>,
51    quantization: Option<String>,
52    max_completion_tokens: Option<f64>,
53    max_prompt_tokens: Option<f64>,
54    status: Option<String>,
55}
56
57#[derive(Serialize, Deserialize, Debug)]
58pub struct EndpointPricing {
59    request: String,
60    image: String,
61    prompt: String,
62    completion: String,
63}
64
65#[derive(Serialize, Deserialize, Debug)]
66pub struct EndpointData {
67    id: String,
68    name: String,
69    created: f64,
70    description: String,
71    architecture: EndpointArchitecture,
72    endpoints: Vec<Endpoint>,
73}
74
75#[derive(Serialize, Deserialize, Debug)]
76pub struct EndpointArchitecture {
77    tokenizer: Option<String>,
78    instruct_type: Option<String>,
79    modality: Option<String>,
80}
81
82/// Returns a list of models available through the API
83///
84/// # Arguments
85///
86/// * `client` - The HTTP client to use for the request.
87/// * `api_key` - The API key for authentication.
88///
89/// # Returns
90///
91/// * `Result<Vec<Model>, OpenRouterError>` - A list of models or an error.
92pub async fn list_models(client: &Client, api_key: &str) -> Result<Vec<Model>, OpenRouterError> {
93    let url = "https://openrouter.ai/api/v1/models";
94
95    let response = client.get(url).bearer_auth(api_key).send().await?;
96
97    if response.status().is_success() {
98        let model_list_response = response.json::<ApiResponse<Vec<Model>>>().await?;
99        Ok(model_list_response.data)
100    } else {
101        handle_error(response).await?;
102        unreachable!()
103    }
104}
105
106/// Returns details about the endpoints for a specific model
107///
108/// # Arguments
109///
110/// * `client` - The HTTP client to use for the request.
111/// * `api_key` - The API key for authentication.
112/// * `author` - The author of the model.
113/// * `slug` - The slug identifier for the model.
114///
115/// # Returns
116///
117/// * `Result<EndpointData, OpenRouterError>` - The endpoint data or an error.
118pub async fn list_model_endpoints(
119    client: &Client,
120    api_key: &str,
121    author: &str,
122    slug: &str,
123) -> Result<EndpointData, OpenRouterError> {
124    let url = format!("https://openrouter.ai/api/v1/models/{}/{}", author, slug);
125
126    let response = client.get(&url).bearer_auth(api_key).send().await?;
127
128    if response.status().is_success() {
129        let endpoint_list_response = response.json::<ApiResponse<EndpointData>>().await?;
130        Ok(endpoint_list_response.data)
131    } else {
132        handle_error(response).await?;
133        unreachable!()
134    }
135}