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

1554
examples/openai-assistant/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
[package]
name = "openai-assistant"
version = "0.1.0"
edition = "2021"
[dependencies]
lib = { path = "../..", features = ["openai", "io"] }
tokio = { version = "1.38.0", features = ["rt-multi-thread"] }
futures = "0.3.0"
async-openai = "0.23.0"

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())
}