opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Comprehensive async-openai integration examples for OpenCrates
//!
//! This example demonstrates all the advanced features of the async-openai crate
//! integrated into the OpenCrates ecosystem:
//!
//! - Chat completions with streaming
//! - Embeddings for semantic search
//! - Image generation and manipulation
//! - Audio transcription and speech synthesis
//! - Structured outputs with JSON schema
//! - Function calling capabilities
//! - Vision processing
//! - Azure OpenAI compatibility

use anyhow::Result;
use async_openai::types::{
    ChatCompletionRequestMessage, ChatCompletionRequestSystemMessage,
    ChatCompletionRequestSystemMessageContent, ChatCompletionRequestUserMessage,
};
use base64::{engine::general_purpose, Engine as _};
use opencrates::{
    core::OpenCrates,
    providers::{
        enhanced_openai::{
            AudioTranscriptionRequest, EmbeddingRequest, EnhancedOpenAIConfig,
            EnhancedOpenAIProvider, ImageGenerationRequest, SpeechRequest,
        },
        GenerationRequest, LLMProvider,
    },
    utils::config::OpenCratesConfig,
    utils::templates::CrateSpec,
};
use serde_json::json;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
use tokio::fs;
use tracing::{error, info};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing for logging
    tracing_subscriber::fmt::init();

    println!("OpenCrates async-openai Integration Examples");
    println!("===============================================");

    // Initialize OpenCrates with enhanced OpenAI
    let config = OpenCratesConfig::default();
    let opencrates = OpenCrates::new_with_config(config).await?;

    // Create enhanced OpenAI provider
    let enhanced_provider = EnhancedOpenAIProvider::new().await?;

    // Run comprehensive examples
    run_chat_examples(&enhanced_provider).await?;
    run_embedding_examples(&enhanced_provider).await?;
    run_image_examples(&enhanced_provider).await?;
    run_audio_examples(&enhanced_provider).await?;
    run_structured_output_examples(&enhanced_provider).await?;
    run_function_calling_examples(&enhanced_provider).await?;
    run_code_generation_examples(&enhanced_provider, &opencrates).await?;

    println!("\n✅ All examples completed successfully!");
    Ok(())
}

/// Demonstrate chat completion capabilities
async fn run_chat_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\nChat Completion Examples");
    println!("---------------------------");

    // Basic chat completion
    let messages = vec![
        ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessage {
            content: ChatCompletionRequestSystemMessageContent::Text(
                "You are a helpful Rust programming assistant.".to_string(),
            ),
            name: None,
        }),
        ChatCompletionRequestUserMessage::new(
            async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                "Generate a simple hello world function in Rust.".to_string(),
            ),
        )
        .into(),
    ];

    let response = provider
        .chat_completion(
            messages.clone(),
            Some("gpt-4o".to_string()),
            Some(500),
            Some(0.7),
            false,
        )
        .await?;

    println!(" Chat Response:");
    println!("{}", response.content);
    println!("Tokens used: {}", response.usage.total_tokens);

    // Streaming chat completion example
    println!("\n🌊 Streaming Chat Example:");
    let streaming_future = provider.chat_completion(messages.clone(), None, Some(100), None, true);
    if let Ok(streaming_response) = streaming_future.await {
        info!("Successfully received streaming response");
        println!("Streamed content: {}", streaming_response.preview);
    } else {
        error!("Failed to receive streaming response");
    }

    Ok(())
}

/// Demonstrate embedding generation for semantic search
async fn run_embedding_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\nEmbedding Examples");
    println!("---------------------");

    let embedding_request = EmbeddingRequest {
        input: vec![
            "Rust programming language".to_string(),
            "Memory safety without garbage collection".to_string(),
            "Zero-cost abstractions in systems programming".to_string(),
            "Async/await concurrency model".to_string(),
        ],
        model: Some("text-embedding-3-large".to_string()),
    };

    let embedding_response = provider.generate_embeddings(embedding_request).await?;

    println!(
        "Generated {} embeddings",
        embedding_response.embeddings.len()
    );
    println!(
        "Embedding dimensions: {}",
        embedding_response.embeddings[0].len()
    );
    println!("Tokens used: {}", embedding_response.usage.total_tokens);

    // Demonstrate semantic similarity
    if embedding_response.embeddings.len() >= 2 {
        let similarity = cosine_similarity(
            &embedding_response.embeddings[0],
            &embedding_response.embeddings[1],
        );
        println!(
            " Similarity between first two embeddings: {:.4}",
            similarity
        );
    }

    Ok(())
}

/// Demonstrate image generation capabilities
async fn run_image_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\n Image Generation Examples");
    println!("----------------------------");

    let image_request = ImageGenerationRequest {
        prompt:
            "A futuristic Rust programming IDE with holographic code displays, cyberpunk aesthetic"
                .to_string(),
        n: Some(1),
        size: Some("1024x1024".to_string()),
        quality: Some("standard".to_string()),
        response_format: Some("url".to_string()),
    };

    let image_response = provider.generate_image(image_request).await?;

    if let Some(first_image) = image_response.images.first() {
        if let Some(url) = &first_image.url {
            println!("  Generated image URL: {}", url);
        }
        if let Some(revised_prompt) = &first_image.revised_prompt {
            println!("  Revised prompt: {}", revised_prompt);
        }
    }

    // Generate image as base64 for local processing
    let base64_request = ImageGenerationRequest {
        prompt: "Rust crab mascot wearing a space helmet, cartoon style".to_string(),
        n: Some(1),
        size: Some("512x512".to_string()),
        quality: Some("standard".to_string()),
        response_format: Some("b64_json".to_string()),
    };

    let base64_response = provider.generate_image(base64_request).await?;
    if let Some(first_image) = base64_response.images.first() {
        if let Some(b64_data) = &first_image.b64_json {
            println!(" Generated base64 image (length: {} chars)", b64_data.len());

            // Save the image to file
            let image_data = general_purpose::STANDARD.decode(b64_data)?;
            let mut file = File::create("generated_rust_crab.png")?;
            file.write_all(&image_data)?;
            println!(" Saved image to: generated_rust_crab.png");
        }
    }

    Ok(())
}

/// Demonstrate audio processing capabilities
async fn run_audio_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\n Audio Processing Examples");
    println!("----------------------------");

    // Text-to-speech example
    let speech_request = SpeechRequest {
        input: "Welcome to OpenCrates, the AI-powered Rust development companion!".to_string(),
        model: Some("tts-1-hd".to_string()),
        voice: Some("alloy".to_string()),
    };

    match provider.generate_speech(speech_request).await {
        Ok(audio_data) => {
            fs::write("welcome_message.mp3", audio_data).await?;
            println!(" Generated speech audio: welcome_message.mp3");
        }
        Err(e) => {
            println!("⚠  Speech generation skipped: {}", e);
        }
    }

    // Audio transcription example (only if audio file exists)
    if Path::new("test_audio.mp3").exists() {
        let transcription_request = AudioTranscriptionRequest {
            file_path: "test_audio.mp3".to_string(),
            model: Some("whisper-1".to_string()),
            language: Some("en".to_string()),
            response_format: Some("json".to_string()),
        };

        match provider.transcribe_audio(transcription_request).await {
            Ok(transcription_response) => {
                println!(" Transcription: {}", transcription_response.text);
            }
            Err(e) => {
                println!("⚠  Transcription skipped: {}", e);
            }
        }
    } else {
        println!("⚠  Audio transcription skipped: test_audio.mp3 not found");
    }

    Ok(())
}

/// Demonstrate structured output with JSON schema
async fn run_structured_output_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\n Structured Output Examples");
    println!("-----------------------------");

    let schema = json!({
        "type": "object",
        "properties": {
            "crate_name": {
                "type": "string",
                "description": "Name of the Rust crate"
            },
            "description": {
                "type": "string",
                "description": "Brief description of the crate"
            },
            "features": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "description": "List of key features"
            },
            "dependencies": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                        "version": {"type": "string"},
                        "features": {
                            "type": "array",
                            "items": {"type": "string"}
                        }
                    },
                    "required": ["name", "version"]
                }
            },
            "estimated_complexity": {
                "type": "string",
                "enum": ["simple", "moderate", "complex"]
            }
        },
        "required": ["crate_name", "description", "features", "dependencies", "estimated_complexity"],
        "additionalProperties": false
    });

    let messages = vec![
        ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessage {
            content: ChatCompletionRequestSystemMessageContent::Text("You are a Rust crate planning assistant. Generate structured crate specifications.".to_string()),
            name: None,
        }),
        ChatCompletionRequestUserMessage::new(
            async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                "Plan a crate named 'fast-http' with basic features.".to_string(),
            ),
        )
        .into(),
    ];

    match provider
        .structured_completion(messages, schema, "crate_specification".to_string())
        .await
    {
        Ok(structured_response) => {
            println!(" Structured Crate Specification:");
            println!("{}", serde_json::to_string_pretty(&structured_response)?);
        }
        Err(e) => {
            println!("⚠  Structured output skipped: {}", e);
        }
    }

    Ok(())
}

/// Demonstrate function calling capabilities
async fn run_function_calling_examples(provider: &EnhancedOpenAIProvider) -> Result<()> {
    println!("\n Function Calling Examples");
    println!("----------------------------");

    let tools = vec![
        json!({
            "type": "function",
            "function": {
                "name": "analyze_crate_dependencies",
                "description": "Analyze dependencies of a Rust crate",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "crate_name": {
                            "type": "string",
                            "description": "Name of the crate to analyze"
                        },
                        "include_dev_deps": {
                            "type": "boolean",
                            "description": "Whether to include dev dependencies"
                        }
                    },
                    "required": ["crate_name"]
                }
            }
        }),
        json!({
            "type": "function",
            "function": {
                "name": "get_crate_info",
                "description": "Get information about a Rust crate from crates.io",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "crate_name": {
                            "type": "string",
                            "description": "Name of the crate"
                        }
                    },
                    "required": ["crate_name"]
                }
            }
        }),
    ];

    let messages = vec![
        ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessage {
            content: ChatCompletionRequestSystemMessageContent::Text("You are a Rust development assistant with access to crate analysis tools. Use the available functions when needed.".to_string()),
            name: None,
        }),
        ChatCompletionRequestUserMessage::new(
            async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                "What are the dependencies of the 'serde' crate?",
            ),
        )
        .into(),
    ];

    let function_future = provider.function_calling(messages, tools);
    if let Ok(function_response) = function_future.await {
        if let Some(tool_calls) = &function_response.preview.tool_calls {
            println!("Function calls: {:?}", tool_calls);
        } else {
            println!("{}", function_response.preview);
        }
    } else {
        error!("Function call failed");
    }

    Ok(())
}

/// Demonstrate OpenCrates-specific code generation
async fn run_code_generation_examples(
    provider: &EnhancedOpenAIProvider,
    opencrates: &OpenCrates,
) -> Result<()> {
    println!("\n OpenCrates Code Generation Examples");
    println!("--------------------------------------");

    // Generate a complete Rust crate using OpenCrates + async-openai
    let generation_request = GenerationRequest {
        spec: CrateSpec::default(),
        prompt: Some("Create a comprehensive async HTTP client library for Rust with the following features:
- Connection pooling
- Automatic retries with exponential backoff
- Request/response middleware support
- Customizable timeouts and connection settings
- Gzip and Brotli decompression
- SSL/TLS verification options
- Async/await support throughout".to_string()),
        context: Some("This is for a production-ready HTTP client that will be used in enterprise applications. Focus on performance, reliability, and ease of use.".to_string()),
        max_tokens: Some(4000),
        temperature: Some(0.7),
        model: Some("gpt-4o".to_string()),
    };

    if let Ok(response) = provider.generate(&generation_request).await {
        println!("\n--- Generated Crate Code ---");
        println!("{}", response.preview);
        println!("--- End Generated Crate Code ---");

        if let Some(finish_reason) = response.finish_reason {
            println!("  Finish Reason: {}", finish_reason);
        }
        println!("  Tokens: {}", response.metrics.total_tokens);
    } else {
        error!("Crate generation failed");
    }

    // Health check
    match provider.health_check().await {
        Ok(healthy) => {
            println!(
                "\n💚 Provider health check: {}",
                if healthy { "HEALTHY" } else { "UNHEALTHY" }
            );
        }
        Err(e) => {
            println!("⚠  Health check failed: {}", e);
        }
    }

    Ok(())
}

/// Calculate cosine similarity between two vectors
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    let dot_product: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let magnitude_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let magnitude_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if magnitude_a == 0.0 || magnitude_b == 0.0 {
        0.0
    } else {
        dot_product / (magnitude_a * magnitude_b)
    }
}