Inference Gateway Rust SDK
An SDK written in Rust for the Inference Gateway.
Installation
Run cargo add inference-gateway-sdk.
Usage
Creating a Client
Here is a full example of how to create a client and interact with the Inference Gateway API:
use inference_gateway_sdk::{
GatewayError,
InferenceGatewayAPI,
InferenceGatewayClient,
Message,
Provider,
MessageRole
};
use log::info;
use std::env;
#[tokio::main]
async fn main() -> Result<(), GatewayError> {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
env_logger::init();
let client = InferenceGatewayClient::new("http://localhost:8080");
let response = client.list_models().await?;
for model in response.data {
info!("Model: {:?}", model.id);
}
let response = client.list_models_by_provider(Provider::Groq).await?;
info!("Models for provider: {:?}", response.provider);
for model in response.data {
info!("Model: {:?}", model.id);
}
let resp = client.generate_content(Provider::Groq, "deepseek-r1-distill-llama-70b", vec![
Message{
role: MessageRole::System,
content: "You are an helpful assistent.".to_string()
},
Message{
role: MessageRole::User,
content: "Tell me a funny joke".to_string()
}
]).await?;
log::info!("Generated from provider: {:?}", resp.provider);
log::info!("Generated response: {:?}", resp.response.role);
log::info!("Generated content: {:?}", resp.response.content);
Ok(())
}
Listing Models
To list all available models from all configured providers, use the list_models method:
use inference_gateway_sdk::{
GatewayError
InferenceGatewayAPI,
InferenceGatewayClient,
Message,
};
use log::info;
#[tokio::main]
fn main() -> Result<(), GatewayError> {
let models = client.list_models().await?;
for provider_models in models {
info!("Provider: {:?}", provider_models.provider);
for model in provider_models.models {
info!("Model: {:?}", model.name);
}
}
}
Listing Models from a specific provider
To list all available models from a specific provider, use the list_models_by_provider method:
use inference_gateway_sdk::{
GatewayError
InferenceGatewayAPI,
InferenceGatewayClient,
Provider,
};
use log::info;
let resp = client.list_models_by_provider(Provider::Ollama).await?;
let models = resp.models;
info!("Provider: {:?}", resp.provider);
for model in models {
info!("Model: {:?}", model.name);
}
Generating Content
To generate content using a model, use the generate_content method:
use inference_gateway_sdk::{
GatewayError,
InferenceGatewayAPI,
InferenceGatewayClient,
Message,
Provider,
MessageRole
};
let resp = client.generate_content(Provider::Groq, "deepseek-r1-distill-llama-70b", vec![
Message{
role: MessageRole::System,
content: "You are an helpful assistent.".to_string()
},
Message{
role: MessageRole::User,
content: "Tell me a funny joke".to_string()
}
]).await?;
log::info!("Generated from provider: {:?}", resp.provider);
log::info!("Generated response: {:?}", resp.response.role);
log::info!("Generated content: {:?}", resp.response.content);
Streaming Content
You need to add the following tiny dependencies:
futures-util for the StreamExt trait
serde with feature derive and serde_json for serialization and deserialization of the response content
use inference_gateway_sdk::{
InferenceGatewayAPI,
InferenceGatewayClient, Message, MessageRole, Provider, ResponseContent
};
use futures_util::{StreamExt, pin_mut};
let system_message = "You are an helpful assistent.".to_string();
let model = "deepseek-r1-distill-llama-70b";
let messages = vec![
Message {
role: MessageRole::System,
content: system_message,
tool_call_id: None
},
Message {
role: MessageRole::User,
content: "Write a poem".to_string(),
tool_call_id: None
},
];
let client = InferenceGatewayClient::new("http://localhost:8080");
let stream = client.generate_content_stream(Provider::Groq, model, messages);
pin_mut!(stream);
let content_delta = Some("content-delta".to_string());
while let Some(ssevent) = stream.next().await {
let resp = ssevent?;
if resp.event != content_delta {
continue;
}
let generate_response: ResponseContent = serde_json::from_str(&resp.data)?;
print!("{}", generate_response.content);
}
Tool-Use
You can pass to the generate_content function also tools, which will be available for the LLM to use:
use inference_gateway_sdk::{
GatewayError,
InferenceGatewayAPI,
InferenceGatewayClient,
Message,
Provider,
MessageRole,
Tool,
ToolFunction,
ToolType
};
let tools = vec![
Tool {
r#type: ToolType::Function,
function: ToolFunction {
name: "get_current_weather".to_string(),
description: "Get the weather for a location".to_string(),
parameters: json!({
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name"
}
},
"required": ["location"]
}),
},
},
];
let resp = client.with_tools(Some(tools)).generate_content(Provider::Groq, "deepseek-r1-distill-llama-70b", vec![
Message {
role: MessageRole::System,
content: "You are an helpful assistent.".to_string(),
..Default::default()
},
Message {
role: MessageRole::User,
content: "What is the current weather in Berlin?".to_string(),
..Default::default()
}
]).await?;
for tool_call in resp.response.tool_calls {
log::info!("Tool Call Requested by the LLM: {:?}", tool_call);
let message = Message {
role: MessageRole::Tool,
content: "The content from the tool".to_string(),
tool_call_id: Some(tool_call.id) };
}
Health Check
To check if the Inference Gateway is running, use the health_check method:
use log::info;
let is_healthy = client.health_check().await?;
info!("API is healthy: {}", is_healthy);
Contributing
Please refer to the CONTRIBUTING.md file for information about how to get involved. We welcome issues, questions, and pull requests.
License
This SDK is distributed under the MIT License, see LICENSE for more information.