Function vectorize_image_concurrently

Source
pub async fn vectorize_image_concurrently<C>(
    prompts: Vec<String>,
    vector: &mut Vector<DynamicImage>,
    client: Client<C>,
    model_parameters: ModelParameters,
) -> Result<(), Error>
where C: Config + Send + Sync + 'static,
Expand description

Concurrently vectorizes an image with multiple prompts.

§Arguments

  • model - The name/identifier of the LLM model to use
  • prompts - A vector of prompts to process concurrently
  • vector - A mutable reference to the Vector struct containing the image
  • client - The OpenAI API client

§Returns

  • Result<(), Error> - Ok(()) on success, Error on failure

Each prompt’s dimensionality is specified by how many digits that it requires the LLM to return. The final dimensionality of the vector is calculated by number of prompts * digits specified by each prompt.

Examples found in repository?
examples/vectorize_images.rs (lines 45-50)
16async fn main() -> Result<(), Error> {
17    // Load image
18    let image_path: &str = "./examples/images/54e2c8ea-58ef-4871-ae3f-75eabd9a2c6c.jpg";
19    let test_image: DynamicImage = image::open(image_path).unwrap();
20
21    // Create a Vector object from the image
22    let mut vector: Vector<DynamicImage> = Vector::from_image(test_image);
23
24    // Initialize client
25    let client: Client<OpenAIConfig> = Client::with_config(
26        OpenAIConfig::new()
27            .with_api_base("http://192.168.0.101:11434/v1") // comment this out if you use OpenAI instead of Ollama
28            .with_api_key("your_api_key")
29    );
30
31    // Initialize prompts
32    let prompts: Vec<String> = vec![
33        "output in json. Rate the image's offensiveness from 0.0 to 10.0. {'offensiveness': your score}".to_string(),
34        "output in json. Rate the image's friendliness from 0.0 to 10.0. {'friendliness': your score}".to_string(),
35    ];
36
37    // Initialize model parameters
38    let model_parameters = ModelParameters::new(
39        "minicpm-v".to_string(), 
40        Some(0.7), 
41        None
42    );
43
44    // Vectorize image
45    vectorize_image_concurrently(
46        prompts,
47        &mut vector, 
48        client,
49        model_parameters
50    ).await?;
51
52    // Print vectorized result
53    println!("Vector: {:?}", vector.get_vector());
54    println!("Vector Length: {:?}", vector.get_vector().len());
55
56    Ok(())
57}