pub struct OpenAIAssistant { /* private fields */ }
assistants::OpenAIAssistant
struct for latest functionality including Assistants API v2.Expand description
This is a DEPRECATED implementation of OpenAI Assistants API that will not be maintained going forward (after May 2024).
For current implementation, including support for Assistants API v2 and GPT-4o, refer to assistants
module.
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?
26async fn main() -> Result<()> {
27 env_logger::init();
28 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
29 // Read concert file
30 let path = Path::new("metallica.pdf");
31 let bytes = std::fs::read(path)?;
32 let file_name = path
33 .file_name()
34 .and_then(OsStr::to_str)
35 .map(|s| s.to_string())
36 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
37
38 let openai_file = OpenAIFile::new(&file_name, bytes, &api_key, true).await?;
39
40 let bands_genres = vec![
41 ("Metallica", "Metal"),
42 ("The Beatles", "Rock"),
43 ("Daft Punk", "Electronic"),
44 ("Miles Davis", "Jazz"),
45 ("Johnny Cash", "Country"),
46 ];
47
48 // Extract concert information using Assistant API
49 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4o, &api_key, true)
50 .await?
51 // Constructor defaults to V1
52 .version(OpenAIAssistantVersion::V2)
53 .set_context(
54 "bands_genres",
55 &bands_genres
56 )
57 .await?
58 .get_answer::<ConcertInfo>(
59 "Extract the information requested in the response type from the attached concert information.
60 The response should include the genre of the music the 'band' represents.
61 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
62 &[openai_file.id.clone()],
63 )
64 .await?;
65
66 println!("Concert Info: {:?}", concert_info);
67
68 //Remove the file from OpenAI
69 openai_file.delete_file().await?;
70
71 Ok(())
72}
Sourcepub fn version(self, version: OpenAIAssistantVersion) -> Self
pub fn version(self, version: OpenAIAssistantVersion) -> Self
This method can be used to set the version of Assistants API Beta Current default is V1
Examples found in repository?
26async fn main() -> Result<()> {
27 env_logger::init();
28 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
29 // Read concert file
30 let path = Path::new("metallica.pdf");
31 let bytes = std::fs::read(path)?;
32 let file_name = path
33 .file_name()
34 .and_then(OsStr::to_str)
35 .map(|s| s.to_string())
36 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
37
38 let openai_file = OpenAIFile::new(&file_name, bytes, &api_key, true).await?;
39
40 let bands_genres = vec![
41 ("Metallica", "Metal"),
42 ("The Beatles", "Rock"),
43 ("Daft Punk", "Electronic"),
44 ("Miles Davis", "Jazz"),
45 ("Johnny Cash", "Country"),
46 ];
47
48 // Extract concert information using Assistant API
49 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4o, &api_key, true)
50 .await?
51 // Constructor defaults to V1
52 .version(OpenAIAssistantVersion::V2)
53 .set_context(
54 "bands_genres",
55 &bands_genres
56 )
57 .await?
58 .get_answer::<ConcertInfo>(
59 "Extract the information requested in the response type from the attached concert information.
60 The response should include the genre of the music the 'band' represents.
61 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
62 &[openai_file.id.clone()],
63 )
64 .await?;
65
66 println!("Concert Info: {:?}", concert_info);
67
68 //Remove the file from OpenAI
69 openai_file.delete_file().await?;
70
71 Ok(())
72}
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?
26async fn main() -> Result<()> {
27 env_logger::init();
28 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
29 // Read concert file
30 let path = Path::new("metallica.pdf");
31 let bytes = std::fs::read(path)?;
32 let file_name = path
33 .file_name()
34 .and_then(OsStr::to_str)
35 .map(|s| s.to_string())
36 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
37
38 let openai_file = OpenAIFile::new(&file_name, bytes, &api_key, true).await?;
39
40 let bands_genres = vec![
41 ("Metallica", "Metal"),
42 ("The Beatles", "Rock"),
43 ("Daft Punk", "Electronic"),
44 ("Miles Davis", "Jazz"),
45 ("Johnny Cash", "Country"),
46 ];
47
48 // Extract concert information using Assistant API
49 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4o, &api_key, true)
50 .await?
51 // Constructor defaults to V1
52 .version(OpenAIAssistantVersion::V2)
53 .set_context(
54 "bands_genres",
55 &bands_genres
56 )
57 .await?
58 .get_answer::<ConcertInfo>(
59 "Extract the information requested in the response type from the attached concert information.
60 The response should include the genre of the music the 'band' represents.
61 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
62 &[openai_file.id.clone()],
63 )
64 .await?;
65
66 println!("Concert Info: {:?}", concert_info);
67
68 //Remove the file from OpenAI
69 openai_file.delete_file().await?;
70
71 Ok(())
72}
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?
26async fn main() -> Result<()> {
27 env_logger::init();
28 let api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
29 // Read concert file
30 let path = Path::new("metallica.pdf");
31 let bytes = std::fs::read(path)?;
32 let file_name = path
33 .file_name()
34 .and_then(OsStr::to_str)
35 .map(|s| s.to_string())
36 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
37
38 let openai_file = OpenAIFile::new(&file_name, bytes, &api_key, true).await?;
39
40 let bands_genres = vec![
41 ("Metallica", "Metal"),
42 ("The Beatles", "Rock"),
43 ("Daft Punk", "Electronic"),
44 ("Miles Davis", "Jazz"),
45 ("Johnny Cash", "Country"),
46 ];
47
48 // Extract concert information using Assistant API
49 let concert_info = OpenAIAssistant::new(OpenAIModels::Gpt4o, &api_key, true)
50 .await?
51 // Constructor defaults to V1
52 .version(OpenAIAssistantVersion::V2)
53 .set_context(
54 "bands_genres",
55 &bands_genres
56 )
57 .await?
58 .get_answer::<ConcertInfo>(
59 "Extract the information requested in the response type from the attached concert information.
60 The response should include the genre of the music the 'band' represents.
61 The mapping of bands to genres was provided in 'bands_genres' list in a previous message.",
62 &[openai_file.id.clone()],
63 )
64 .await?;
65
66 println!("Concert Info: {:?}", concert_info);
67
68 //Remove the file from OpenAI
69 openai_file.delete_file().await?;
70
71 Ok(())
72}
Trait Implementationsยง
Sourceยงimpl Clone for OpenAIAssistant
impl Clone for OpenAIAssistant
Sourceยงfn clone(&self) -> OpenAIAssistant
fn clone(&self) -> OpenAIAssistant
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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>,
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
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more