Expand description
§Batch OpenAI SDK with YARA Rule Validation and OpenAI API Client
This crate provides a comprehensive SDK for integrating OpenAI’s API with YARA rule validation,
including support for chat completions, streaming responses, and automated testing.
§Features
OpenAIAPI Client: Full-featured client with streaming support- Chat Completions: Support for role-based conversations and prompt templates
- Streaming Support: Real-time streaming responses with Server-Sent Events
- YARA Rule Validation: Complete validation pipeline using the yara-x engine
OpenAIBatch API Integration: Generate and manage batch jobs for rule creation- Comprehensive Testing: Unit tests, integration tests, and performance benchmarks
- Error Handling: Robust error handling with detailed validation results
- Performance Metrics: Compilation time, memory usage, and rule complexity analysis
§Quick Start - OpenAI API
use openai_rust_sdk::{OpenAIClient, ChatBuilder};
let client = OpenAIClient::new("your-api-key")?;
// Simple text generation
let response = client.generate_text("gpt-4", "Hello, world!").await?;
println!("Response: {response}");
// Chat conversation
let conversation = ChatBuilder::new()
.developer("You are a helpful assistant")
.user("What is Rust?");
let response = client.chat("gpt-4", conversation).await?;
println!("Chat response: {response}");§Quick Start - YARA Validation
ⓘ
use openai_rust_sdk::testing::YaraValidator;
let validator = YaraValidator::new();
let rule = r#"
rule test_rule {
strings:
$hello = "Hello World"
condition:
$hello
}
"#;
let result = validator.validate_rule(rule)?;
println!("Rule is valid: {}", result.is_valid);§Modules
client: MainOpenAIclient with all API functionalityapi: Individual API modules (responses, streaming)models: Data models for requests and responsestesting: YARA validation and testing functionalityerror: Error types and handling
§Examples
See the examples/ directory for complete usage examples:
chat_completion.rs: Chat completions and streaming examplesfull_integration.rs: Complete workflow demonstrationbasic_validation.rs: Simple rule validationstreaming_demo.rs: Streaming API integrationerror_handling.rs: Error handling patterns
Re-exports§
pub use api::batch::BatchApi;pub use api::streaming::StreamingApi;pub use api::AssistantsApi;pub use api::CustomToolsApi;pub use api::FineTuningApi;pub use api::FunctionsApi;pub use api::RealtimeAudioApi;pub use api::ResponsesApi;pub use api::RunsApi;pub use api::ThreadsApi;pub use api::VectorStoresApi;pub use builders::FunctionBuilder;pub use builders::ObjectSchemaBuilder;pub use client::from_env;pub use client::from_env_with_base_url;pub use client::ChatBuilder;pub use client::OpenAIClient;pub use error::OpenAIError;pub use error::Result;pub use prompt_engineering::Example;pub use prompt_engineering::PromptBuilder;pub use prompt_engineering::PromptPatterns;pub use prompt_engineering::PromptTemplateBuilder;pub use prompt_engineering::XmlContentBuilder;pub use schema::EnhancedSchemaBuilder;pub use schema::JsonSchema;pub use schema::SchemaBuilder;pub use testing::batch_generator::BatchJobGenerator;pub use models::assistants::*;pub use models::functions::*;pub use models::responses::*;
Modules§
- api
OpenAIAPI functionality- builders
- Function and schema builders
- client
- Client implementations and builders
- constants
- Constants used throughout the SDK Constants used throughout the OpenAI API SDK
- error
- Error types and handling
- helpers
- Helper utilities to reduce code duplication Helper modules for reducing code duplication
- macros
- Common macros for reducing code duplication Common macros for reducing code duplication across the codebase
- models
- Data models and types
- prompt_
engineering - Prompt engineering utilities and builders
- schema
- JSON Schema utilities JSON Schema builder for structured outputs
- testing
- YARA testing and validation functionality
Macros§
- file_
err - Convenience macro for FileError mapping with format string
- http_
delete - Macro to generate HTTP DELETE method wrappers
- http_
delete_ beta - Macro to generate HTTP DELETE method wrappers with beta headers
- http_
get - Macro to generate simple HTTP GET method wrappers
- http_
get_ beta - Macro to generate HTTP GET method wrappers with beta headers
- http_
post - Macro to generate HTTP POST method wrappers
- http_
post_ beta - Macro to generate HTTP POST method wrappers with beta headers
- impl_
api_ client - Macro to implement the
ApiClienttrait for a struct that contains anHttpClient - impl_
api_ test_ suite - Macro to generate comprehensive test suites for API clients and models
- impl_
audio_ format_ methods - Macro to generate common audio format methods for audio builders
- impl_
builder - Macro to implement the basic Builder trait
- impl_
builder_ build - Macro to generate standard builder
build()methods with field validation - impl_
builder_ methods - Macro for implementing common builder methods
- impl_
builder_ setters - Macro to implement standard builder pattern setter methods
- impl_
bytes_ to_ human_ readable - Macro to generate bytes to human readable formatting
- impl_
bytes_ usage - Generic macro to implement BytesUsage
- impl_
comprehensive_ test_ suite - Macro to generate comprehensive test suites that can replace entire test modules
- impl_
default_ object_ type - Macro to generate default object type functions
- impl_
delete_ response - Generic macro to implement DeleteResponse
- impl_
enum_ display - Macro to generate Display implementations for enums that serialize as lowercase strings
- impl_
file_ supported - Generic macro to implement FileSupported
- impl_
filterable_ list_ response - Generic macro to implement FilterableListResponse
- impl_
fine_ tuning_ params - Macro to generate fine-tuning job parameter types with pagination
- impl_
fluent_ setters - Macro to generate fluent setter methods for builders
- impl_
format_ methods - Macro to generate common format methods for builders
- impl_
image_ size_ methods - Macro to generate common size methods for image builders
- impl_
list_ params - Macro to generate list parameter structures with pagination support
- impl_
list_ params_ with_ builder - Macro to generate common list parameter types with builder pattern methods This eliminates massive duplication in vector stores, fine tuning, and similar modules
- impl_
list_ query_ params - Macro to implement ListQueryParams trait for pagination structures
- impl_
list_ response - Macro to generate list response structures
- impl_
list_ response_ methods - Macro to generate list response filtering methods for responses with data field
- impl_
map_ setters - Macro to generate builder methods for
HashMapfields - impl_
metadata_ builder - Generic macro to implement MetadataBuilder for any builder type
- impl_
option_ setters - Macro to generate builder methods for optional non-string fields
- impl_
pagination_ params - Generic macro to implement PaginationParams
- impl_
params_ test_ suite - Macro to generate parameter testing suites with pagination
- impl_
query_ params - Macro for implementing query parameter building with common fields
- impl_
response_ constructors - Macro to generate success/failure response constructors
- impl_
run_ builder_ methods - Macro to generate run builder methods that delegate to RunConfigurationBuilder trait
- impl_
run_ config_ builder - Macro to generate RunConfigurationBuilder trait implementations
- impl_
status_ enum - Generic macro to implement StatusEnum for snake_case serialized enums
- impl_
status_ filters - Macro to generate status filtering methods for list responses
- impl_
status_ methods - Macro to generate status checking methods for entities
- impl_
string_ setters - Macro to generate builder methods for optional string fields
- impl_
test_ helpers - Macro to generate common test helper functions for request/response testing
- impl_
transcription_ format_ methods - Macro to generate common transcription format methods
- impl_
usage_ methods - Macro to generate usage bytes methods for list responses
- impl_
validation - Macro to implement common validation methods
- impl_
vec_ setters - Macro to generate builder methods for vector fields
- impl_
with_ format - Macro to implement WithFormat trait
- impl_
with_ n - Macro to implement WithN trait
- impl_
with_ quality - Macro to implement WithQuality trait
- impl_
with_ size - Macro to implement WithSize trait
- impl_
with_ speed - Macro to implement WithSpeed trait
- impl_
with_ temperature - Macro to implement WithTemperature trait
- impl_
with_ user - Macro to implement WithUser trait
- invalid_
request_ err - Convenience macro for InvalidRequest mapping
- map_err
- Macro to simplify error mapping with format strings
Usage:
map_err!(FileError, "Failed to read file: {}") - network_
err - Convenience macro for Unknown error mapping (network errors)
- parse_
err - Convenience macro for ParseError mapping
- parsing_
err - Convenience macro for parsing errors that go to InvalidRequest
- request_
err - Convenience macro for RequestError mapping
- streaming_
err - Convenience macro for Streaming error mapping
- validation_
err - Convenience macro for validation errors (parsing errors that go to InvalidRequest)
Traits§
- De
- A data structure that can be deserialized from any data format supported by Serde.
- Ser
- A data structure that can be serialized into any data format supported by Serde.