pub struct OpenAIAssistant { /* private fields */ }
Expand description
The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries. The Assistants API currently supports three types of tools: Code Interpreter, Retrieval, and Function calling. In the future, we plan to release more OpenAI-built tools, and allow you to provide your own tools on our platform.
Implementations§
Source§impl OpenAIAssistant
impl OpenAIAssistant
Sourcepub async fn new(
model: OpenAIModels,
open_ai_key: &str,
debug: bool,
) -> Result<Self>
pub async fn new( model: OpenAIModels, open_ai_key: &str, debug: bool, ) -> Result<Self>
Examples found in repository?
examples/use_openai_assistant.rs (line 42)
24async fn main() -> Result<()> {
25 env_logger::init();
26 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
27 // Read invoice file
28 let path = Path::new("examples/metallica.pdf");
29 let bytes = std::fs::read(path)?;
30
31 let openai_file = OpenAIFile::new(bytes, &api_key, true).await?;
32
33 let bands_genres = vec![
34 ("Metallica", "Metal"),
35 ("The Beatles", "Rock"),
36 ("Daft Punk", "Electronic"),
37 ("Miles Davis", "Jazz"),
38 ("Johnny Cash", "Country"),
39 ];
40
41 // Extract concert information using Assistant API
42 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4Turbo, &api_key, true)
43 .await?
44 .set_context(
45 "bands_genres",
46 &bands_genres
47 )
48 .await?
49 .get_answer::<ConcertInfo>(
50 "Extract the information requested in the response type from the attached concert information.
51 The response should include the genre of the music the 'band' represents.
52 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
53 &[openai_file.id],
54 )
55 .await?;
56
57 println!("Concert Info: {:?}", concert_info);
58 Ok(())
59}
Sourcepub async fn get_answer<T: JsonSchema + DeserializeOwned>(
self,
message: &str,
file_ids: &[String],
) -> Result<T>
pub async fn get_answer<T: JsonSchema + DeserializeOwned>( self, message: &str, file_ids: &[String], ) -> Result<T>
Examples found in repository?
examples/use_openai_assistant.rs (lines 49-54)
24async fn main() -> Result<()> {
25 env_logger::init();
26 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
27 // Read invoice file
28 let path = Path::new("examples/metallica.pdf");
29 let bytes = std::fs::read(path)?;
30
31 let openai_file = OpenAIFile::new(bytes, &api_key, true).await?;
32
33 let bands_genres = vec![
34 ("Metallica", "Metal"),
35 ("The Beatles", "Rock"),
36 ("Daft Punk", "Electronic"),
37 ("Miles Davis", "Jazz"),
38 ("Johnny Cash", "Country"),
39 ];
40
41 // Extract concert information using Assistant API
42 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4Turbo, &api_key, true)
43 .await?
44 .set_context(
45 "bands_genres",
46 &bands_genres
47 )
48 .await?
49 .get_answer::<ConcertInfo>(
50 "Extract the information requested in the response type from the attached concert information.
51 The response should include the genre of the music the 'band' represents.
52 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
53 &[openai_file.id],
54 )
55 .await?;
56
57 println!("Concert Info: {:?}", concert_info);
58 Ok(())
59}
Sourcepub async fn set_context<T: Serialize>(
self,
dataset_name: &str,
data: &T,
) -> Result<Self>
pub async fn set_context<T: Serialize>( self, dataset_name: &str, data: &T, ) -> Result<Self>
This method can be used to provide data that will be used as context for the prompt. Using this function you can provide multiple sets of context data by calling it multiple times. New values will be as messages to the thread It accepts any struct that implements the Serialize trait.
Examples found in repository?
examples/use_openai_assistant.rs (lines 44-47)
24async fn main() -> Result<()> {
25 env_logger::init();
26 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
27 // Read invoice file
28 let path = Path::new("examples/metallica.pdf");
29 let bytes = std::fs::read(path)?;
30
31 let openai_file = OpenAIFile::new(bytes, &api_key, true).await?;
32
33 let bands_genres = vec![
34 ("Metallica", "Metal"),
35 ("The Beatles", "Rock"),
36 ("Daft Punk", "Electronic"),
37 ("Miles Davis", "Jazz"),
38 ("Johnny Cash", "Country"),
39 ];
40
41 // Extract concert information using Assistant API
42 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4Turbo, &api_key, true)
43 .await?
44 .set_context(
45 "bands_genres",
46 &bands_genres
47 )
48 .await?
49 .get_answer::<ConcertInfo>(
50 "Extract the information requested in the response type from the attached concert information.
51 The response should include the genre of the music the 'band' represents.
52 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
53 &[openai_file.id],
54 )
55 .await?;
56
57 println!("Concert Info: {:?}", concert_info);
58 Ok(())
59}
Trait Implementations§
Source§impl Clone for OpenAIAssistant
impl Clone for OpenAIAssistant
Source§fn clone(&self) -> OpenAIAssistant
fn clone(&self) -> OpenAIAssistant
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for OpenAIAssistant
impl Debug for OpenAIAssistant
Source§impl<'de> Deserialize<'de> for OpenAIAssistant
impl<'de> Deserialize<'de> for OpenAIAssistant
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for OpenAIAssistant
impl RefUnwindSafe for OpenAIAssistant
impl Send for OpenAIAssistant
impl Sync for OpenAIAssistant
impl Unpin for OpenAIAssistant
impl UnwindSafe for OpenAIAssistant
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more