secretary 0.2.30

Translate natural languages into structural data
Documentation
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# Secretary Examples

This document provides comprehensive examples of using the Secretary library for various use cases.

## Basic Examples

### Simple JSON Generation

Extract a sentiment classification from text:

```rust
use secretary::{openai::OpenAILLM, tasks::basic_task::BasicTask, traits::GenerateJSON};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sentiment {
    sentiment: String,
}

impl Default for Sentiment {
    fn default() -> Self {
        Sentiment {
            sentiment: String::from(
                "rate the text in terms of their sentiments. it can be: high, low or mid.",
            ),
        }
    }
}

fn main() {
    // Initialize the OpenAI LLM client
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )
    .unwrap();

    // Create task with schema and instructions
    let task = BasicTask::new(
        Sentiment::default(),
        vec![
            "Consider the context when determining sentiment.".to_string(),
            "Pay attention to intensifiers and negations that might change sentiment.".to_string(),
        ],
    );

    // Generate JSON from text
    let result: String = llm.generate_json(&task, "This is unacceptable!").unwrap();
    println!("{}", result);
    // Output: {"sentiment":"low"}
}
```

### Extracting Multiple Fields

Extract product information from a description:

```rust
use secretary::{openai::OpenAILLM, tasks::basic_task::BasicTask, traits::GenerateJSON};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Product {
    name: String,
    category: String,
    price_range: String,
    features: Vec<String>,
    target_audience: Option<String>,
}

impl Default for Product {
    fn default() -> Self {
        Self {
            name: "Product name".to_string(),
            category: "Product category (electronics, clothing, etc.)".to_string(),
            price_range: "Approximate price range (budget, mid-range, premium)".to_string(),
            features: vec!["Key product features mentioned in the text".to_string()],
            target_audience: Some("The intended users of this product".to_string()),
        }
    }
}

fn main() -> anyhow::Result<()> {
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )?;
    
    let task = BasicTask::new(
        Product::default(),
        vec![
            "Extract all product details mentioned in the text".to_string(),
            "Infer the price range and category if not explicitly stated".to_string(),
        ],
    );
    
    let description = "The new XPS 13 laptop features a 13-inch 4K display, \
                      16GB of RAM, and comes with the latest 12th Gen Intel processor. \
                      Perfect for professionals and students who need performance on the go.";
    
    let json_result = llm.generate_json(&task, description)?;
    println!("{}", json_result);
    
    // Parse the result into our struct
    let product: Product = serde_json::from_str(&json_result)?;
    println!("Product name: {}", product.name);
    println!("Features:");
    for feature in product.features {
        println!(" - {}", feature);
    }
    
    Ok(())
}
```

## Multi-Turn Conversations

### Basic Conversation Flow

Maintain context across multiple exchanges:

```rust
use secretary::{
    message_list::Role, 
    openai::OpenAILLM, 
    tasks::basic_task::BasicTask, 
    traits::{Context, GenerateJSON},
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recommendation {
    item: String,
    reason: String,
}

impl Default for Recommendation {
    fn default() -> Self {
        Recommendation {
            item: String::from("Recommended item name"),
            reason: String::from("Reason for the recommendation"),
        }
    }
}

fn main() -> anyhow::Result<()> {
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )?;

    let mut task = BasicTask::new(
        Recommendation::default(), 
        vec![
            "Provide personalized recommendations based on user preferences".to_string(),
            "Consider all previous messages in the conversation".to_string(),
        ],
    );

    // Start conversation
    task.push(Role::User, "I'm looking for a new book to read.")?;
    
    // Get first recommendation
    let response1 = llm.generate_json_with_context(&task)?;
    println!("First recommendation: {}", response1);
    
    // Add response to conversation context
    task.push(Role::Assistant, &response1)?;
    
    // Continue conversation
    task.push(Role::User, "I prefer science fiction with female protagonists.")?;
    
    // Get updated recommendation based on new information
    let response2 = llm.generate_json_with_context(&task)?;
    println!("Updated recommendation: {}", response2);
    
    Ok(())
}
```

## Contextual Tasks

### Progressive Data Building

Build a complex data structure through conversation:

```rust
use secretary::{
    message_list::Role,
    openai::OpenAILLM,
    tasks::contextual_task::ContextualTask,
    traits::{Context, GenerateJSON},
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Resume {
    name: String,
    education: Vec<String>,
    work_experience: Vec<String>,
    skills: Vec<String>,
    certifications: Option<Vec<String>>,
}

impl Default for Resume {
    fn default() -> Self {
        Self {
            name: "Full name of the person".to_string(),
            education: vec!["Education history with degrees, institutions, and years".to_string()],
            work_experience: vec!["Work history with company, position, and dates".to_string()],
            skills: vec!["Technical and soft skills mentioned".to_string()],
            certifications: Some(vec!["Professional certifications if any".to_string()]),
        }
    }
}

fn simulate_conversation() -> anyhow::Result<()> {
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )?;
    
    let mut task = ContextualTask::new(
        Resume::default(),
        vec![
            "Extract resume information progressively through conversation".to_string(),
            "Ask focused questions about missing information".to_string(),
            "Update the resume data structure as new information is provided".to_string(),
        ],
    );
    
    // Simulate a conversation to build a resume
    let conversation = [
        "I'm John Smith, a software engineer with 5 years of experience.",
        "I graduated from MIT with a computer science degree in 2018.",
        "I've worked at Google as a Senior Developer and at Tesla as a Software Engineer.",
        "My skills include Python, Rust, and machine learning.",
        "I'm certified in AWS and have a Project Management Professional certification."
    ];
    
    for (i, message) in conversation.iter().enumerate() {
        println!("\n--- Turn {} ---", i+1);
        println!("User: {}", message);
        
        // Add user message to context
        task.push(Role::User, message)?;
        
        // Generate response
        let response = llm.generate_json_with_context(&task)?;
        
        // Parse the response
        let json_value: serde_json::Value = serde_json::from_str(&response)?;
        
        // Display reasoning and content (question)
        if let Some(reasoning) = json_value.get("reasoning").and_then(|v| v.as_str()) {
            println!("\nAI Reasoning: {}", reasoning);
        }
        
        if let Some(content) = json_value.get("content").and_then(|v| v.as_str()) {
            println!("AI: {}", content);
        }
        
        // Display current resume state
        if let Some(data) = json_value.get("data_structure") {
            println!("\nCurrent Resume State:");
            println!("{}", serde_json::to_string_pretty(data)?);
        }
        
        // Add response to context
        task.push(Role::Assistant, &response)?;
    }
    
    Ok(())
}

fn main() {
    simulate_conversation().unwrap();
}
```

## Advanced Examples

### Asynchronous Processing

Process requests asynchronously:

```rust
use secretary::{
    openai::OpenAILLM, 
    tasks::basic_task::BasicTask, 
    traits::AsyncGenerateJSON,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct QueryAnalysis {
    intent: String,
    entities: Vec<String>,
    sentiment: String,
}

impl Default for QueryAnalysis {
    fn default() -> Self {
        Self {
            intent: "The user's primary intention".to_string(),
            entities: vec!["Key entities mentioned in the query".to_string()],
            sentiment: "Emotional tone: positive, negative, or neutral".to_string(),
        }
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )?;
    
    let task = BasicTask::new(
        QueryAnalysis::default(),
        vec![
            "Analyze user queries for intent, entities, and sentiment".to_string(),
            "Focus on identifying actionable intents".to_string(),
        ],
    );
    
    let queries = vec![
        "Can you find me flights to New York for next weekend?",
        "I'm extremely disappointed with your customer service",
        "What's the weather forecast for Chicago tomorrow?",
    ];
    
    let mut handles = Vec::new();
    
    // Process queries concurrently
    for query in queries {
        let llm_clone = llm.clone();
        let task_clone = task.clone();
        let query_clone = query.to_string();
        
        let handle = tokio::spawn(async move {
            let result = llm_clone.async_generate_json(&task_clone, &query_clone).await?;
            Ok::<(String, String), anyhow::Error>((query_clone, result))
        });
        
        handles.push(handle);
    }
    
    // Collect and display results
    for handle in handles {
        let (query, result) = handle.await??;
        println!("Query: {}", query);
        println!("Analysis: {}", result);
        println!();
    }
    
    Ok(())
}
```

### Custom Data Processing

Process structured data after extraction:

```rust
use secretary::{openai::OpenAILLM, tasks::basic_task::BasicTask, traits::GenerateJSON};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Serialize, Deserialize)]
struct MeetingNotes {
    title: String,
    date: String,
    participants: Vec<String>,
    action_items: Vec<ActionItem>,
    key_points: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ActionItem {
    description: String,
    assignee: String,
    due_date: Option<String>,
}

impl Default for MeetingNotes {
    fn default() -> Self {
        Self {
            title: "Meeting title or topic".to_string(),
            date: "Date when the meeting occurred".to_string(),
            participants: vec!["List of people who attended the meeting".to_string()],
            action_items: vec![ActionItem {
                description: "Task to be completed".to_string(),
                assignee: "Person responsible for the task".to_string(),
                due_date: Some("When the task should be completed".to_string()),
            }],
            key_points: vec!["Important points discussed in the meeting".to_string()],
        }
    }
}

fn process_meeting_notes(notes: &str) -> anyhow::Result<()> {
    let llm = OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
        &std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
    )?;
    
    let task = BasicTask::new(
        MeetingNotes::default(),
        vec![
            "Extract structured information from meeting notes".to_string(),
            "Identify all action items and their assignees".to_string(),
            "Extract key discussion points".to_string(),
        ],
    );
    
    let json_result = llm.generate_json(&task, notes)?;
    
    // Parse the result
    let meeting: MeetingNotes = serde_json::from_str(&json_result)?;
    
    // Process the structured data
    println!("Meeting: {} ({})", meeting.title, meeting.date);
    println!("Participants: {}", meeting.participants.join(", "));
    
    println!("\nKey Points:");
    for (i, point) in meeting.key_points.iter().enumerate() {
        println!("{}. {}", i+1, point);
    }
    
    // Organize action items by assignee
    let mut tasks_by_assignee: HashMap<String, Vec<&ActionItem>> = HashMap::new();
    for item in &meeting.action_items {
        tasks_by_assignee
            .entry(item.assignee.clone())
            .or_insert_with(Vec::new)
            .push(item);
    }
    
    println!("\nAction Items by Person:");
    for (person, tasks) in tasks_by_assignee {
        println!("\n{}", person);
        for (i, task) in tasks.iter().enumerate() {
            let due = task.due_date.as_deref().unwrap_or("No due date");
            println!("{}. {} (Due: {})", i+1, task.description, due);
        }
    }
    
    Ok(())
}

fn main() -> anyhow::Result<()> {
    let meeting_notes = "
        Product Planning Meeting - June 15, 2023
        
        Attendees: Alice (PM), Bob (Engineering), Carol (Design), David (Marketing)
        
        We discussed the Q3 roadmap for our mobile app. Alice presented the user feedback
        from our beta testers. Key points:
        - Users want better notification management
        - The checkout flow has too many steps
        - Performance issues on older Android devices
        
        Action items:
        - Bob will investigate the performance issues and propose solutions by June 30
        - Carol will redesign the checkout flow by June 22
        - David will create a marketing plan for the Q3 release by next Friday
        - Alice will prioritize the notification features for the next sprint
        
        Next meeting scheduled for June 29.
    ";
    
    process_meeting_notes(meeting_notes)?;
    Ok(())
}
```

## Performance Tips

- Provide clear, specific instructions in your task
- Use descriptive field names and annotations in your schema
- Start with simple schemas and add complexity gradually
- For large data structures, break the extraction into multiple steps
- Store examples of good extractions to improve your schemas

## Error Handling

```rust
use secretary::{openai::OpenAILLM, tasks::basic_task::BasicTask, traits::GenerateJSON};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Address {
    street: String,
    city: String,
    state: String,
    zip: String,
}

impl Default for Address {
    fn default() -> Self {
        Self {
            street: "Street address including house/apt number".to_string(),
            city: "City name".to_string(),
            state: "State name or abbreviation".to_string(),
            zip: "ZIP or postal code".to_string(),
        }
    }
}

fn extract_address(text: &str) -> Result<Address, String> {
    // Initialize the LLM client
    let llm = match OpenAILLM::new(
        &std::env::var("SECRETARY_OPENAI_API_BASE").unwrap_or("https://api.openai.com/v1".to_string()),
        &std::env::var("SECRETARY_OPENAI_API_KEY").map_err(|e| format!("API key error: {}", e))?,
        &std::env::var("SECRETARY_OPENAI_MODEL").unwrap_or("gpt-4o-mini".to_string()),
    ) {
        Ok(client) => client,
        Err(e) => return Err(format!("Failed to initialize LLM client: {}", e)),
    };
    
    // Create the extraction task
    let task = BasicTask::new(
        Address::default(),
        vec![
            "Extract address information from the text".to_string(),
            "If any field is missing, return null for that field".to_string(),
        ],
    );
    
    // Generate JSON and handle potential errors
    let json_result = match llm.generate_json(&task, text) {
        Ok(result) => result,
        Err(e) => return Err(format!("Generation error: {}", e)),
    };
    
    // Parse the JSON result
    match serde_json::from_str::<Address>(&json_result) {
        Ok(address) => Ok(address),
        Err(e) => Err(format!("Failed to parse address JSON: {}", e)),
    }
}

fn main() {
    let input = "Please ship to: 123 Main St, Springfield, IL 62701";
    
    match extract_address(input) {
        Ok(address) => {
            println!("Successfully extracted address:");
            println!("Street: {}", address.street);
            println!("City: {}", address.city);
            println!("State: {}", address.state);
            println!("ZIP: {}", address.zip);
        },
        Err(e) => {
            eprintln!("Error extracting address: {}", e);
        }
    }
}
```