2 releases

0.1.1 Mar 8, 2023
0.1.0 Mar 8, 2023

#75 in #chatgpt

MIT license

19KB
395 lines

cordyceps

https://docs.rs/cordyceps_api/0.1.1/cordyceps_api/


lib.rs:

openai

The cordyceps crate is a fast and reliable API for OpenAi's various AI models

Supported Features

Planned Features

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;

Dependencies

~6–19MB
~270K SLoC