Add embed endpoint (POST /api/embed)

Implement the Ollama POST /api/embed endpoint for generating vector
embeddings from text input.

- Add EmbedInput, EmbedRequest, EmbedResponse types in src/types/embed.rs
- Add OllamaClient::embed() async method in src/lib.rs
- Register embed module in src/types/mod.rs
- Add usage example in examples/embed.rs
- Update README with embed endpoint documentation
This commit is contained in:
2026-02-01 21:26:44 +00:00
parent b885ca3c1c
commit 0f796f1a2f
5 changed files with 379 additions and 0 deletions

24
examples/embed.rs Normal file
View File

@@ -0,0 +1,24 @@
use std::{env, error::Error};
use ollama_rs::OllamaClient;
use ollama_rs::types::embed::EmbedRequest;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt().init();
let _ = dotenvy::dotenv();
let server_address = env::var("OLLAMA_SERVER")?;
let ollama_client = OllamaClient::new(server_address);
let request = EmbedRequest::builder("embeddinggemma")
.input("Generate embeddings for this text")
.build();
let response = ollama_client.embed(request).await?;
for (i, embedding) in response.embeddings.iter().enumerate() {
println!("Embedding {}: {} dimensions", i, embedding.len());
if embedding.len() >= 3 {
println!(" First 3 values: {:?}", &embedding[..3]);
}
}
Ok(())
}