let mut openai = OpenAI::new();
let messages = vec![
Message::new(String::from("user"), String::from("Hi there!"))
];
openai
.model_id(String::from("gpt-4o-mini"))
.messages(messages)
.temperature(1.0);
let response: Response = openai.chat().unwrap();
println!("{}", &response.choices[0].message.content);
#[derive(Debug, Serialize, Deserialize)]
struct Weather {
location: String,
date: String,
weather: String,
error: String,
}
let mut openai = OpenAI::new();
let messages = vec![Message::new(
String::from("user"),
String::from("Hi there! How's the weather tomorrow in Tokyo? If you can't answer, report error."),
)];
let mut json_schema = JsonSchema::new("weather".to_string());
json_schema.add_property(
String::from("location"),
String::from("string"),
Option::from(String::from("The location to check the weather for.")),
);
json_schema.add_property(
String::from("date"),
String::from("string"),
Option::from(String::from("The date to check the weather for.")),
);
json_schema.add_property(
String::from("weather"),
String::from("string"),
Option::from(String::from("The weather for the location and date.")),
);
json_schema.add_property(
String::from("error"),
String::from("string"),
Option::from(String::from("Error message. If there is no error, leave this field empty.")),
);
openai
.model_id(String::from("gpt-4o-mini"))
.messages(messages)
.temperature(1.0)
.response_format(ResponseFormat::new(String::from("json_schema"), json_schema));
let response = openai.chat().unwrap();
let answer: Weather = serde_json::from_str::<Weather>(&response.choices[0].message.content).unwrap();
println!("{:?}", answer)