-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_image_generation.rs
More file actions
156 lines (137 loc) · 5.03 KB
/
Copy path14_image_generation.rs
File metadata and controls
156 lines (137 loc) · 5.03 KB
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
//! Image Generation Example
//!
//! Demonstrates image generation from text prompts using various providers.
//!
//! Requirements:
//! - Set OPENAI_API_KEY environment variable (for DALL-E)
//!
//! Run:
//! cargo run --example 14_image_generation
use llmkit::{ImageGenerationRequest, ImageQuality, ImageSize, ImageStyle, LLMKitClient};
#[tokio::main]
async fn main() -> llmkit::Result<()> {
let client = LLMKitClient::builder()
.with_openai_from_env()
.build()
.await?;
println!("=== Image Generation Example ===\n");
// Example 1: Simple image generation
println!("--- Example 1: Simple Image Generation ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"A serene landscape with mountains and a clear blue lake at sunset",
);
let response = client.generate_image(request).await?;
println!("Generated {} image(s)", response.images.len());
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
if let Some(revised) = &image.revised_prompt {
println!("Revised prompt: {}", revised);
}
}
// Example 2: High-quality image with vivid style
println!("\n--- Example 2: High-Quality Vivid Image ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"A portrait of a Renaissance noble in ornate clothing",
)
.with_quality(ImageQuality::Hd)
.with_style(ImageStyle::Vivid);
let response = client.generate_image(request).await?;
println!("Generated HD image");
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
}
// Example 3: Natural style image
println!("\n--- Example 3: Natural Style Image ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"A photograph of a coffee shop interior with warm lighting",
)
.with_style(ImageStyle::Natural);
let response = client.generate_image(request).await?;
println!("Generated natural-style image");
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
}
// Example 4: Landscape orientation
println!("\n--- Example 4: Landscape Orientation ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"A panoramic view of a futuristic city skyline at night",
)
.with_size(ImageSize::Landscape1792x1024);
let response = client.generate_image(request).await?;
println!("Generated landscape image (1792x1024)");
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
}
// Example 5: Portrait orientation
println!("\n--- Example 5: Portrait Orientation ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"A detailed character portrait of a fantasy elf warrior",
)
.with_size(ImageSize::Portrait1024x1792)
.with_quality(ImageQuality::Hd);
let response = client.generate_image(request).await?;
println!("Generated portrait image (1024x1792)");
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
}
// Example 6: Square image
println!("\n--- Example 6: Square Image ---");
let request = ImageGenerationRequest::new(
"openai/dall-e-3",
"An abstract geometric pattern with bold colors",
)
.with_size(ImageSize::Square1024);
let response = client.generate_image(request).await?;
println!("Generated square image (1024x1024)");
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!("Image URL: {}", url);
}
}
// Example 7: Multiple use cases
println!("\n--- Example 7: Different Use Cases ---");
let use_cases = [
(
"Marketing",
"A modern product packaging design for organic tea",
),
("Education", "An illustrated diagram of the solar system"),
(
"Entertainment",
"A fantasy dragon flying over a medieval castle",
),
("Art", "An impressionist painting of a garden in spring"),
];
for (category, prompt) in use_cases {
let request = ImageGenerationRequest::new("openai/dall-e-3", prompt);
let response = client.generate_image(request).await?;
println!(
" {}: Generated image for '{}'",
category,
&prompt[..40.min(prompt.len())]
);
if let Some(image) = response.images.first() {
if let Some(url) = &image.url {
println!(" URL: {}", url);
}
}
}
println!("\nImage generation examples completed!");
println!("\nNote: Images are hosted temporarily. Download them if you want to keep them.");
Ok(())
}