rig/rig-core/examples/sentiment_classifier.rs

36 lines
823 B
Rust

use rig::providers::openai;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, JsonSchema, Serialize)]
/// An enum representing the sentiment of a document
enum Sentiment {
Positive,
Negative,
Neutral,
}
#[derive(Debug, Deserialize, JsonSchema, Serialize)]
struct DocumentSentiment {
/// The sentiment of the document
sentiment: Sentiment,
}
#[tokio::main]
async fn main() {
// Create OpenAI client
let openai_client = openai::Client::from_env();
// Create extractor
let data_extractor = openai_client
.extractor::<DocumentSentiment>("gpt-4")
.build();
let sentiment = data_extractor
.extract("I am happy")
.await
.expect("Failed to extract sentiment");
println!("GPT-4: {:?}", sentiment);
}