azure_ai_foundry_models

Chat completions and embeddings client for the Azure AI Foundry Rust SDK.
Features
- Chat Completions — Synchronous and streaming responses
- Embeddings — Generate vector embeddings for text
- Streaming — SSE with optimized parsing and 1MB buffer protection
- Builder Pattern — Type-safe request construction with parameter validation
- Tracing — Full instrumentation with
tracing spans
Installation
[dependencies]
azure_ai_foundry_core = "0.4"
azure_ai_foundry_models = "0.4"
tokio = { version = "1", features = ["full"] }
Usage
Chat Completions
use azure_ai_foundry_core::client::FoundryClient;
use azure_ai_foundry_core::auth::FoundryCredential;
use azure_ai_foundry_models::chat::{ChatCompletionRequest, Message};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = FoundryClient::builder()
.endpoint("https://your-resource.services.ai.azure.com")
.credential(FoundryCredential::api_key("your-key"))
.build()?;
let request = ChatCompletionRequest::builder()
.model("gpt-4o")
.message(Message::system("You are a helpful assistant."))
.message(Message::user("What is Rust?"))
.build();
let response = azure_ai_foundry_models::chat::complete(&client, &request).await?;
println!("{}", response.choices[0].message.content.as_deref().unwrap_or_default());
Ok(())
}
Streaming Chat Completions
use azure_ai_foundry_core::client::FoundryClient;
use azure_ai_foundry_core::auth::FoundryCredential;
use azure_ai_foundry_models::chat::{ChatCompletionRequest, Message, complete_stream};
use futures::StreamExt;
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
# let client = FoundryClient::builder()
# .endpoint("https://your-resource.services.ai.azure.com")
# .credential(FoundryCredential::api_key("your-key"))
# .build()?;
let request = ChatCompletionRequest::builder()
.model("gpt-4o")
.message(Message::user("Tell me a story"))
.build();
let stream = complete_stream(&client, &request).await?;
let mut stream = std::pin::pin!(stream);
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
if let Some(content) = chunk.choices[0].delta.content.as_deref() {
print!("{}", content);
}
}
# Ok(())
# }
Embeddings
use azure_ai_foundry_core::client::FoundryClient;
use azure_ai_foundry_core::auth::FoundryCredential;
use azure_ai_foundry_models::embeddings::{EmbeddingRequest, embed};
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
# let client = FoundryClient::builder()
# .endpoint("https://your-resource.services.ai.azure.com")
# .credential(FoundryCredential::api_key("your-key"))
# .build()?;
let request = EmbeddingRequest::builder()
.model("text-embedding-ada-002")
.input("The quick brown fox jumps over the lazy dog")
.build();
let response = embed(&client, &request).await?;
println!("Embedding dimensions: {}", response.data[0].embedding.len());
# Ok(())
# }
Multiple Embeddings
use azure_ai_foundry_core::client::FoundryClient;
use azure_ai_foundry_core::auth::FoundryCredential;
use azure_ai_foundry_models::embeddings::{EmbeddingRequest, embed};
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
# let client = FoundryClient::builder()
# .endpoint("https://your-resource.services.ai.azure.com")
# .credential(FoundryCredential::api_key("your-key"))
# .build()?;
let request = EmbeddingRequest::builder()
.model("text-embedding-ada-002")
.inputs(vec![
"First document",
"Second document",
"Third document",
])
.build();
let response = embed(&client, &request).await?;
for (i, item) in response.data.iter().enumerate() {
println!("Document {}: {} dimensions", i, item.embedding.len());
}
# Ok(())
# }
Modules
| Module |
Description |
chat |
Chat completions API with sync and streaming support |
embeddings |
Vector embeddings generation |
Related Crates
License
This project is licensed under the MIT License.