use_openai_assistant/
use_openai_assistant.rs

1use std::path::Path;
2
3use openai_safe::OpenAIAssistant;
4use openai_safe::OpenAIFile;
5use openai_safe::OpenAIModels;
6
7use anyhow::Result;
8use schemars::JsonSchema;
9use serde::Deserialize;
10use serde::Serialize;
11
12#[derive(Deserialize, Serialize, Debug, Clone, JsonSchema)]
13pub struct ConcertInfo {
14    dates: Vec<String>,
15    band: String,
16    genre: String,
17    venue: String,
18    city: String,
19    country: String,
20    ticket_price: String,
21}
22
23#[tokio::main]
24async fn main() -> Result<()> {
25    env_logger::init();
26    let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
27    // Read invoice file
28    let path = Path::new("examples/metallica.pdf");
29    let bytes = std::fs::read(path)?;
30
31    let openai_file = OpenAIFile::new(bytes, &api_key, true).await?;
32
33    let bands_genres = vec![
34        ("Metallica", "Metal"),
35        ("The Beatles", "Rock"),
36        ("Daft Punk", "Electronic"),
37        ("Miles Davis", "Jazz"),
38        ("Johnny Cash", "Country"),
39    ];
40
41    // Extract concert information using Assistant API
42    let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4Turbo, &api_key, true)
43        .await?
44        .set_context(
45            "bands_genres",
46            &bands_genres
47        )
48        .await?
49        .get_answer::<ConcertInfo>(
50            "Extract the information requested in the response type from the attached concert information.
51            The response should include the genre of the music the 'band' represents.
52            The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
53            &[openai_file.id],
54        )
55        .await?;
56
57    println!("Concert Info: {:?}", concert_info);
58    Ok(())
59}