Functions and structs for working with openai api.
Input with stdout message
This commit is contained in:
1554
examples/openai-assistant/Cargo.lock
generated
Normal file
1554
examples/openai-assistant/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
examples/openai-assistant/Cargo.toml
Normal file
10
examples/openai-assistant/Cargo.toml
Normal 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"
|
32
examples/openai-assistant/src/main.rs
Normal file
32
examples/openai-assistant/src/main.rs
Normal 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())
|
||||
}
|
Reference in New Issue
Block a user