Functions and structs for working with openai api.

Input with stdout message
This commit is contained in:
Martin Berg Alstad
2024-07-31 19:37:46 +02:00
parent 865cc6ddb9
commit 695605977f
15 changed files with 2784 additions and 11 deletions

View File

@ -0,0 +1,32 @@
use futures::StreamExt;
use lib::{
openai::{assistants::Assistant, streams::TokenStream},
prompt_read_line,
};
/// Expects the OPENAI_API_KEY environment variable to be set
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let assistant = Assistant::new("gpt-4o-mini", "Be a helpful assistant").await?;
let thread = assistant.create_thread().await?;
while let Some(input) = get_user_input() {
let mut stream: TokenStream = thread.run_stream(&input).await?.into();
while let Some(result) = stream.next().await {
if let Ok(text) = result {
print!("{}", text);
}
}
println!();
}
assistant.delete().await?;
Ok(())
}
fn get_user_input() -> Option<String> {
prompt_read_line!("> ")
.ok()
.take_if(|input| !input.is_empty())
}