Crates.io | groq-api-rs |
lib.rs | groq-api-rs |
version | 0.2.0 |
source | src |
created_at | 2024-06-14 07:31:47.150046 |
updated_at | 2024-06-26 07:05:12.980005 |
description | Client For Groq Cloud LLM APIs |
homepage | https://github.com/ededddy/groq-api-rs |
repository | https://github.com/ededddy/groq-api-rs |
max_upload_size | |
id | 1271744 |
size | 48,181 |
Provides a simple client implementation for the groq cloud API.
You can learn more about the API provided API Documentation
This crate uses [reqwest
], [reqwest_eventsource
], [tokio
], [serde
], [serde_json
], [anyhow
],
[chrono
],[futures
]
1.78.0
cargo add groq-api-rs
Request a completion object from Groq
use groq_api_rs::completion::{client::Groq, message::Message, request::builder};
async fn create_completion() -> anyhow::Result<()> {
let messages = vec![Message::UserMessage {
role: Some("user".to_string()),
content: Some("Explain the importance of fast language models".to_string()),
name: None,
tool_call_id: None,
}];
let request = builder::RequestBuilder::new("mixtral-8x7b-32768".to_string());
let api_key = env!("GROQ_API_KEY");
let mut client = Groq::new(api_key);
client.add_messages(messages);
let res = client.create(request).await;
assert!(res.is_ok());
Ok(())
}
Request a completion chunk object from Groq using stream option implemented with SSE
use groq_api_rs::completion::{client::Groq, message::Message, request::builder};
async fn create_stream_completion() -> anyhow::Result<()> {
let messages = vec![Message::UserMessage {
role: Some("user".to_string()),
content: Some("Explain the importance of fast language models".to_string()),
name: None,
tool_call_id: None,
}];
let request =
builder::RequestBuilder::new("mixtral-8x7b-32768".to_string()).with_stream(true);
let api_key = env!("GROQ_API_KEY");
let mut client = Groq::new(api_key);
client.add_messages(messages);
let res = client.create(request).await;
assert!(res.is_ok());
Ok(())
}
Example that the completion can return Error Object and augmented with HTTP status code.
use groq_api_rs::completion::{client::Groq, message::Message, request::builder};
async fn error_does_return() -> anyhow::Result<()> {
let messages = vec![Message::UserMessage {
role: Some("user".to_string()),
content: Some("Explain the importance of fast language models".to_string()),
name: None,
tool_call_id: None,
}];
let request =
builder::RequestBuilder::new("mixtral-8x7b-32768".to_string()).with_stream(true);
let api_key = "";
let mut client = Groq::new(api_key);
client.add_messages(messages);
let res = client.create(request).await;
assert!(res.is_err());
eprintln!("{}", res.unwrap_err());
Ok(())
}
Feel free to open issues and PRs. I am still learning Rust, the design and coding might not be good.