|
| 1 | +// Import necessary dependencies |
| 2 | +use rig::providers::openai; |
| 3 | +use schemars::JsonSchema; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +// Define enum for sentiment classification |
| 7 | +#[derive(Debug, Deserialize, JsonSchema, Serialize)] |
| 8 | +enum Sentiment { |
| 9 | + Positive, |
| 10 | + Negative, |
| 11 | + Neutral, |
| 12 | +} |
| 13 | + |
| 14 | +// Define an enum for sentiment confidence level |
| 15 | +#[derive(Debug, Deserialize, JsonSchema, Serialize)] |
| 16 | +struct SentimentClassification { |
| 17 | + sentiment: Sentiment, |
| 18 | + confidence: f32, |
| 19 | +} |
| 20 | + |
| 21 | +// Define an enum to represent news article topics |
| 22 | +#[derive(Debug, Deserialize, JsonSchema, Serialize)] |
| 23 | +enum Topic { |
| 24 | + Politics, |
| 25 | + Technology, |
| 26 | + Sports, |
| 27 | + Entertainment, |
| 28 | + Other(String), |
| 29 | +} |
| 30 | + |
| 31 | +// Define a struct to hold the news article classification result |
| 32 | +#[derive(Debug, Deserialize, JsonSchema, Serialize)] |
| 33 | +struct NewsArticleClassification { |
| 34 | + topic: Topic, |
| 35 | + sentiment: SentimentClassification, |
| 36 | + summary: String, |
| 37 | +} |
| 38 | + |
| 39 | +fn pretty_print_result(article: &str, result: &NewsArticleClassification) { |
| 40 | + println!("Article: \"{}...\"", &article[..100]); // Print first 100 characters |
| 41 | + println!("Classification Result:"); |
| 42 | + println!(" Topic: {:?}", result.topic); |
| 43 | + println!(" Sentiment: {:?}", result.sentiment.sentiment); |
| 44 | + println!(" Confidence: {:.2}%", result.sentiment.confidence * 100.0); |
| 45 | + println!(" Summary: {}", result.summary); |
| 46 | + println!(); |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::main] |
| 50 | +async fn main() { |
| 51 | + // Initialize the OpenAI client |
| 52 | + let openai_client = openai::Client::from_env(); |
| 53 | + |
| 54 | + // Create a news article classifier using Rig's Extractor |
| 55 | + let news_classifier = openai_client |
| 56 | + .extractor::<NewsArticleClassification>("gpt-4") |
| 57 | + .preamble(" |
| 58 | + You are a news article classification AI. For the given news article: |
| 59 | + 1. Classify the main topic (Politics, Technology, Sports, Entertainment, or Other). |
| 60 | + 2. Analyze the overall sentiment (Positive, Negative, or Neutral) with a confidence score. |
| 61 | + 3. Provide a brief summary of the article. |
| 62 | + ") |
| 63 | + .build(); |
| 64 | + |
| 65 | + // Sample news article to classify |
| 66 | + let article = " |
| 67 | + After conducting the first-ever commercial spacewalk and traveling farther from Earth than anyone \ |
| 68 | + in more than half a century, the astronauts of the Polaris Dawn mission returned to Earth safely \ |
| 69 | + early Sunday. |
| 70 | +
|
| 71 | + The SpaceX Crew Dragon capsule splashed down in the Gulf of Mexico, off the coast of Dry Tortugas, \ |
| 72 | + Fla., shortly after 3:30 a.m., carrying Jared Isaacman, a billionaire entrepreneur, and his crew \ |
| 73 | + of three private astronauts, according to a SpaceX livestream. |
| 74 | +
|
| 75 | + The ambitious space mission, a collaboration between Mr. Isaacman and Elon Musk's SpaceX, spent \ |
| 76 | + five days in orbit, achieved several milestones in private spaceflight and was further evidence \ |
| 77 | + that space travel and spacewalks are no longer the exclusive domain of professional astronauts \ |
| 78 | + working at government agencies like NASA. |
| 79 | +
|
| 80 | + The Crew Dragon capsule launched on Tuesday, after delays because of a helium leak and bad weather. \ |
| 81 | + On board were Mr. Isaacman, the mission commander and the founder of the payment services company \ |
| 82 | + Shift4; Sarah Gillis and Anna Menon, SpaceX employees; and Scott Poteet, a retired U.S. Air Force \ |
| 83 | + lieutenant colonel. |
| 84 | +
|
| 85 | + Late on Tuesday, its orbit reached a high point of about 870 miles above the Earth's surface. That \ |
| 86 | + beat the record distance for astronauts on a mission not headed to the moon, which the Gemini XI \ |
| 87 | + mission set in 1966 at 853 miles high, and made Ms. Gillis and Ms. Menon the first women ever to \ |
| 88 | + fly so far from Earth. |
| 89 | +
|
| 90 | + On Thursday, Mr. Isaacman and Ms. Gillis became the first private astronauts to successfully complete \ |
| 91 | + a spacewalk. The operation involved the crew letting all the air out of the spacecraft, because it \ |
| 92 | + had no airlock, while the other two crew members wore spacesuits inside the airless capsule. Mr. \ |
| 93 | + Isaacman moved outside and conducted mobility tests of his spacesuit for a few minutes before \ |
| 94 | + re-entering the capsule. Ms Gillis then moved outside and performed the same tests. |
| 95 | +
|
| 96 | + This was the first of three Polaris missions aimed at accelerating technological advances needed to \ |
| 97 | + fulfill Mr. Musk's dream of sending people to Mars someday. A key goal of the mission was to further \ |
| 98 | + the development of more advanced spacesuits that would be needed for SpaceX to try any future \ |
| 99 | + off-world colonization. |
| 100 | +
|
| 101 | + During a news conference before the launch, Mr. Isaacman mused that one day, someone might step onto \ |
| 102 | + Mars wearing a version of the spacesuit that SpaceX had developed for this flight. Closer to Earth, \ |
| 103 | + commercial spacewalks also present other possibilities, like technicians repairing private satellites \ |
| 104 | + in orbit. |
| 105 | +
|
| 106 | + During the spaceflight, the four astronauts conducted about 40 experiments, mostly about how \ |
| 107 | + weightlessness and radiation affect the human body. They also tested laser communications between \ |
| 108 | + the Crew Dragon and SpaceX's constellation of Starlink internet satellites.\ |
| 109 | + "; |
| 110 | + |
| 111 | + // Perform news article classification |
| 112 | + match news_classifier.extract(article).await { |
| 113 | + Ok(result) => pretty_print_result(article, &result), |
| 114 | + Err(e) => eprintln!("Error classifying article: {}", e), |
| 115 | + } |
| 116 | +} |
0 commit comments