1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! # openai
//!
//! The cordyceps crate is a **fast** and **reliable**  API for OpenAi's various AI models
//!
//! ## Supported Features
//!
//! - [`Chat Completion`](crate::chat)
//!
//! ## Planned Features
//! - [`Blocking API`](https://tenor.com/1lq0.gif)
//! - [`Image Generation`](https://platform.openai.com/docs/guides/images/introduction)
//! - [`Image Editing`](https://platform.openai.com/docs/guides/images/introduction)
//! - [`Image Variation`](https://platform.openai.com/docs/guides/images/introduction)
//! - [`Audio Transcription`](https://platform.openai.com/docs/guides/speech-to-text)
//! - [`Audio Translation`](https://platform.openai.com/docs/guides/speech-to-text)
//! - [`Text Moderation`](https://platform.openai.com/docs/guides/moderation)
//!
//! ## Example
//!
//! A small example of how to use the openai crate
//! ```
//! use cordyceps_api::client::{ChatClient, Error, StreamExt};
//! use cordyceps_api::chat::{Payload, Response};
//! use tokio::io::AsyncWriteExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//!     let mut stdout = tokio::io::stdout();
//!     let api_key = std::env::var("OPENAI_API_KEY")?;
//!     let payload = Payload::builder()
//!         .system_message("Implement the Rust programming language into your responses")
//!         .user_message("Tell me a joke")
//!         .build()?;
//!
//!     let client = ChatClient::new(&api_key);
//!     let mut response = client.send(&payload).await?;
//!
//!     while let Some(chunk) = response.next().await {
//!         let body = chunk.unwrap();
//!         match serde_jsonrc::from_slice::<Response>(&body) {
//!             Ok(r) => {
//!                 let text = r.text(0).unwrap();
//!                 stdout.write_all(text.as_bytes()).await.unwrap();
//!                 stdout.flush().await.unwrap();
//!             },
//!             Err(_) => continue,
//!         };
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Re-exports
//! `pub use futures_util::stream::StreamExt;`
//! `pub use futures_util::stream::Stream;`
//! `pub use reqwest::Result as ReqwestResult;`

#[cfg(not(feature = "blocking"))]
pub mod client;

#[cfg(feature = "chat")]
pub mod chat;

#[cfg(feature = "tests")]
#[cfg(test)]
mod tests {
    use crate::chat::{Payload, Response};
    use crate::client::{ChatClient, StreamExt};
    use tokio::io::AsyncWriteExt;

    #[tokio::test]
    async fn example() {
        let mut stdout = tokio::io::stdout();
        let api_key = std::env::var("OPENAI_API_KEY").unwrap();
        let payload = Payload::builder()
            .system_message("Implement the Rust programming language into your responses")
            .user_message("Tell me a joke")
            .build()
            .unwrap();

        let client = ChatClient::new(&api_key);
        let mut response = client.send(&payload).await.unwrap();

        while let Some(chunk) = response.next().await {
            let body = chunk.unwrap();
            match serde_jsonrc::from_slice::<Response>(&body) {
                Ok(r) => {
                    let text = r.text(0).unwrap();
                    stdout.write_all(text.as_bytes()).await.unwrap();
                    stdout.flush().await.unwrap();
                }
                Err(_) => continue,
            };
        }
    }
}