Crate openai_rust_sdk

Crate openai_rust_sdk 

Source
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

  • OpenAI API 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
  • OpenAI Batch 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: Main OpenAI client with all API functionality
  • api: Individual API modules (responses, streaming)
  • models: Data models for requests and responses
  • testing: YARA validation and testing functionality
  • error: Error types and handling

§Examples

See the examples/ directory for complete usage examples:

  • chat_completion.rs: Chat completions and streaming examples
  • full_integration.rs: Complete workflow demonstration
  • basic_validation.rs: Simple rule validation
  • streaming_demo.rs: Streaming API integration
  • error_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
OpenAI API 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 ApiClient trait for a struct that contains an HttpClient
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 HashMap fields
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.

Derive Macros§

De
Ser