pub struct FineTuning { /* private fields */ }Expand description
Client for interacting with the OpenAI Fine-tuning API.
This struct provides methods to create, list, retrieve, and cancel fine-tuning jobs, as well as access training events and checkpoints.
§Example
use openai_tools::fine_tuning::request::{FineTuning, CreateFineTuningJobRequest};
use openai_tools::fine_tuning::response::Hyperparameters;
use openai_tools::common::models::FineTuningModel;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
// Create a fine-tuning job
let hyperparams = Hyperparameters {
n_epochs: Some(3),
batch_size: None,
learning_rate_multiplier: None,
};
let request = CreateFineTuningJobRequest::new(
FineTuningModel::Gpt4oMini_2024_07_18,
"file-abc123"
)
.with_suffix("my-custom-model")
.with_supervised_method(Some(hyperparams));
let job = fine_tuning.create(request).await?;
println!("Created job: {} ({:?})", job.id, job.status);
Ok(())
}Implementations§
Source§impl FineTuning
impl FineTuning
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new FineTuning client for OpenAI API.
Initializes the client by loading the OpenAI API key from
the environment variable OPENAI_API_KEY. Supports .env file loading
via dotenvy.
§Returns
Ok(FineTuning)- A new FineTuning client ready for useErr(OpenAIToolError)- If the API key is not found in the environment
§Example
use openai_tools::fine_tuning::request::FineTuning;
let fine_tuning = FineTuning::new().expect("API key should be set");Sourcepub fn with_auth(auth: AuthProvider) -> Self
pub fn with_auth(auth: AuthProvider) -> Self
Creates a new FineTuning client with a custom authentication provider
Sourcepub fn detect_provider() -> Result<Self>
pub fn detect_provider() -> Result<Self>
Creates a new FineTuning client by auto-detecting the provider
Sourcepub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
Creates a new FineTuning client with URL-based provider detection
Sourcepub fn from_url<S: Into<String>>(url: S) -> Result<Self>
pub fn from_url<S: Into<String>>(url: S) -> Result<Self>
Creates a new FineTuning client from URL using environment variables
Sourcepub fn auth(&self) -> &AuthProvider
pub fn auth(&self) -> &AuthProvider
Returns the authentication provider
Sourcepub async fn create(
&self,
request: CreateFineTuningJobRequest,
) -> Result<FineTuningJob>
pub async fn create( &self, request: CreateFineTuningJobRequest, ) -> Result<FineTuningJob>
Creates a new fine-tuning job.
§Arguments
request- The fine-tuning job creation request
§Returns
Ok(FineTuningJob)- The created job objectErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::fine_tuning::request::{FineTuning, CreateFineTuningJobRequest};
use openai_tools::common::models::FineTuningModel;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let request = CreateFineTuningJobRequest::new(
FineTuningModel::Gpt4oMini_2024_07_18,
"file-abc123"
)
.with_suffix("my-model");
let job = fine_tuning.create(request).await?;
println!("Created job: {}", job.id);
Ok(())
}Sourcepub async fn retrieve(&self, job_id: &str) -> Result<FineTuningJob>
pub async fn retrieve(&self, job_id: &str) -> Result<FineTuningJob>
Retrieves details of a specific fine-tuning job.
§Arguments
job_id- The ID of the job to retrieve
§Returns
Ok(FineTuningJob)- The job detailsErr(OpenAIToolError)- If the job is not found or the request fails
§Example
use openai_tools::fine_tuning::request::FineTuning;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let job = fine_tuning.retrieve("ftjob-abc123").await?;
println!("Status: {:?}", job.status);
if let Some(model) = &job.fine_tuned_model {
println!("Fine-tuned model: {}", model);
}
Ok(())
}Sourcepub async fn cancel(&self, job_id: &str) -> Result<FineTuningJob>
pub async fn cancel(&self, job_id: &str) -> Result<FineTuningJob>
Cancels an in-progress fine-tuning job.
§Arguments
job_id- The ID of the job to cancel
§Returns
Ok(FineTuningJob)- The updated job objectErr(OpenAIToolError)- If the job cannot be cancelled or the request fails
§Example
use openai_tools::fine_tuning::request::FineTuning;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let job = fine_tuning.cancel("ftjob-abc123").await?;
println!("Job status: {:?}", job.status);
Ok(())
}Sourcepub async fn list(
&self,
limit: Option<u32>,
after: Option<&str>,
) -> Result<FineTuningJobListResponse>
pub async fn list( &self, limit: Option<u32>, after: Option<&str>, ) -> Result<FineTuningJobListResponse>
Lists all fine-tuning jobs.
Supports pagination through limit and after parameters.
§Arguments
limit- Maximum number of jobs to return (default: 20)after- Cursor for pagination (job ID to start after)
§Returns
Ok(FineTuningJobListResponse)- The list of jobsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::fine_tuning::request::FineTuning;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let response = fine_tuning.list(Some(10), None).await?;
for job in &response.data {
println!("{}: {:?}", job.id, job.status);
}
Ok(())
}Sourcepub async fn list_events(
&self,
job_id: &str,
limit: Option<u32>,
after: Option<&str>,
) -> Result<FineTuningEventListResponse>
pub async fn list_events( &self, job_id: &str, limit: Option<u32>, after: Option<&str>, ) -> Result<FineTuningEventListResponse>
Lists events for a fine-tuning job.
Events provide insight into the training process.
§Arguments
job_id- The ID of the fine-tuning joblimit- Maximum number of events to return (default: 20)after- Cursor for pagination (event ID to start after)
§Returns
Ok(FineTuningEventListResponse)- The list of eventsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::fine_tuning::request::FineTuning;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let response = fine_tuning.list_events("ftjob-abc123", Some(10), None).await?;
for event in &response.data {
println!("[{}] {}: {}", event.level, event.event_type, event.message);
}
Ok(())
}Sourcepub async fn list_checkpoints(
&self,
job_id: &str,
limit: Option<u32>,
after: Option<&str>,
) -> Result<FineTuningCheckpointListResponse>
pub async fn list_checkpoints( &self, job_id: &str, limit: Option<u32>, after: Option<&str>, ) -> Result<FineTuningCheckpointListResponse>
Lists checkpoints for a fine-tuning job.
Checkpoints are saved at the end of each training epoch. Only the last 3 checkpoints are available.
§Arguments
job_id- The ID of the fine-tuning joblimit- Maximum number of checkpoints to return (default: 10)after- Cursor for pagination (checkpoint ID to start after)
§Returns
Ok(FineTuningCheckpointListResponse)- The list of checkpointsErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::fine_tuning::request::FineTuning;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fine_tuning = FineTuning::new()?;
let response = fine_tuning.list_checkpoints("ftjob-abc123", None, None).await?;
for checkpoint in &response.data {
println!("Step {}: loss={}", checkpoint.step_number, checkpoint.metrics.train_loss);
}
Ok(())
}Auto Trait Implementations§
impl Freeze for FineTuning
impl RefUnwindSafe for FineTuning
impl Send for FineTuning
impl Sync for FineTuning
impl Unpin for FineTuning
impl UnwindSafe for FineTuning
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more