deprecated_use_openai_assistant/
deprecated_use_openai_assistant.rs

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