#interface #sdk #api-key #api-client #chat #key-set #cohere

cohere-rust

This crate provides a simplified interface with the Cohere API in Rust

5 releases

0.6.0 Sep 8, 2024
0.5.3 Nov 24, 2023
0.5.2 Nov 19, 2023
0.5.1 Nov 14, 2023
0.5.0 Nov 10, 2023

#1172 in Web programming

Download history 93/week @ 2024-07-17 56/week @ 2024-07-24 32/week @ 2024-07-31 10/week @ 2024-08-07 31/week @ 2024-08-14 20/week @ 2024-08-21 21/week @ 2024-08-28 262/week @ 2024-09-04 138/week @ 2024-09-11 345/week @ 2024-09-18 200/week @ 2024-09-25 51/week @ 2024-10-02 59/week @ 2024-10-09 85/week @ 2024-10-16 53/week @ 2024-10-23 130/week @ 2024-10-30

345 downloads per month
Used in howcani

MIT license

32KB
542 lines

Cohere Rust SDK

This crate provides a simplified interface with the Cohere API in Rust.

Documentation

See the Cohere API's documentation.

Also see some code examples for the SDK here.

Usage

To use this crate, you must first obtain a Cohere API key. Once you have an API key you can either set it as the COHERE_API_KEY environment variable or pass it directly when constructing the client.

Additionally, this crate relies on the tokio async-runtime to make all the API operations non-blocking.

This is a basic example of the creating the client and using the chat endpoint.

use cohere_rust::api::chat::ChatRequest;
use cohere_rust::api::GenerateModel;
use cohere_rust::Cohere;

#[tokio::main]
async fn main() {
    // automatically reads API key from `COHERE_API_KEY`
    let co = Cohere::default();

    let request = ChatRequest {
        message: "Tell me a story about a magical land.",
        model: Some(GenerateModel::CommandR),
        ..Default::default()
    };

    match co.chat(&request).await {
        Ok(mut rx) => {
            while let Some(message) = rx.recv().await {
                match message {
                    Ok(message) => println!("Chat response: {:#?}", message),
                    Err(e) => println!("Chat error! {:#?}", e),
                }
            }
        }
        Err(e) => {
            println!("Chat failed! {}", e)
        }
    }
}

Example usage of other endpoints can be found here.

Versioning

This SDK supports the latest API version. For more information, please refer to the Versioning Docs.

Endpoints

For a full breakdown of endpoints and arguments, please consult the Cohere Docs.

Cohere Endpoint Function
/generate co.generate()
/chat co.chat()
/embed co.embed()
/rerank co.rerank()
/classify co.classify()
/tokenize co.tokenize()
/detokenize co.detokenize()
/check-api-key co.check_api_key()

Responses

All of the endpoint functions will return a Cohere object corresponding to the endpoint (e.g. for generate, it would be GenerateResponse). The names of these fields and a detailed breakdown of the response body can be found in the Cohere Docs.

Errors

Unsuccessful API calls from the SDK will return an error. Please see the documentation's page on errors for more information about what the errors mean.

Dependencies

~7–18MB
~236K SLoC