batch-mode-batch-schema
The batch-mode-batch-schema
crate provides the data structures and schemas necessary for representing and handling batch-related data in batch processing workflows. It includes structures for batch choices, error responses, token details, and other key elements that are essential for processing batches in a consistent way. The crate also offers the ability to deserialize JSON responses, ensuring the integrity of the batch data and handling any errors that may arise.
Key Features
- Batch Choices: Represents the choices made during batch processing, including the batch index, message, log probabilities, and finish reason.
- Message Role Management: Handles different message roles (assistant, user, system), with support for unknown roles.
- Error Handling: Includes error structures that manage batch errors and their responses, allowing for consistent handling and reporting.
- Batch Response Handling: Provides structures for processing batch success and error responses, including the ability to retrieve details about the response such as choices, model, usage, and more.
- Batch Token Details: Tracks token usage across different stages of batch processing, including cached, audio, and reasoning tokens.
- Deserialization with Validation: Automatically handles JSON deserialization with strict validation to ensure the integrity of data, rejecting invalid fields or missing required fields.
Usage
Creating and Using BatchChoice
use ;
use json;
let json = r#"{
"index": 0,
"message": {
"role": "assistant",
"content": "This is the assistant's response.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}"#;
let choice: BatchChoice = from_str.unwrap;
println!;
Handling Message Roles
use MessageRole;
use json;
let json = r#""assistant""#;
let role: MessageRole = from_str.unwrap;
println!;
Batch Error Handling
use ;
let error_json = r#"{
"error": {
"message": "Invalid API key",
"type": "authentication_error",
"param": null,
"code": "invalid_api_key"
}
}"#;
let error_body: BatchErrorResponseBody = from_str.unwrap;
println!;
Token Details
use BatchTokenDetails;
use json;
let token_json = r#"{
"cached_tokens": 100,
"audio_tokens": 50,
"reasoning_tokens": 25,
"accepted_prediction_tokens": 20,
"rejected_prediction_tokens": 5
}"#;
let token_details: BatchTokenDetails = from_str.unwrap;
println!;
Error Handling
This crate utilizes the error-tree
library for structured error handling, including errors for invalid content, missing required fields, and deserialization issues. Some of the key error types are:
ContentParseError
: Indicates errors while parsing batch content.BatchReconciliationError
: Indicates errors in the reconciliation process.JsonParseError
: Indicates issues during JSON parsing.
License
This crate is licensed under the MIT License. See LICENSE for details.