// Copyright 2026 Cloudflavor GmbH

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Embeddings example
//!
//! This example demonstrates how to use the ollama-api-rs to generate
//! embeddings for text similarity comparisons.

use oai_sdk::{EmbedInput, EmbedRequest, ModelClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create the client
    let client = ModelClient::builder()
        .base_url("http://localhost:11434")
        .build()?;

    println!("Ollama Client Embeddings Example");
    println!("This example shows how to generate embeddings for text.\n");

    // Texts to embed
    let texts = vec![
        "The cat sat on the mat",
        "A feline rested on the rug",
        "Dogs are great pets",
        "Machine learning is fascinating",
    ];

    // Generate embeddings for each text
    let model = "llama3:8b"; // Make sure this model supports embeddings

    for text in texts {
        let request = EmbedRequest {
            model: model.to_string(),
            input: EmbedInput::Single(text.to_string()),
            truncate: Some(true),
            options: None,
            keep_alive: None,
            dimensions: None,
        };

        match client.embed(request).await {
            Ok(response) => {
                println!("Text: '{}'", text);
                println!("Embedding dimension: {}", response.embeddings[0].len());
                println!(
                    "First 5 values: {:?}",
                    &response.embeddings[0][..5.min(response.embeddings[0].len())]
                );
                println!();
            }
            Err(e) => {
                println!("Error generating embedding for '{}': {}", text, e);
            }
        }
    }

    // Calculate similarity between first two texts (simple dot product)
    println!("Calculating similarity between first two texts...");

    // In a real application, you would calculate cosine similarity here
    println!(
        "Note: In a real application, you would calculate cosine similarity between embeddings."
    );

    Ok(())
}
