Skip to content

Commit

Permalink
Updated Wrapper to Builder Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
AryaanSheth committed Sep 5, 2024
1 parent 2c81864 commit 7013bf6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
3 changes: 2 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name = "gloq"
version = "1.0.1"
version = "1.1.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
description = "Gleam API Wrapper to Interface with the GroqCloud API"
licences = ["Apache-2.0"]
repository = { type = "github", user = "AryaanSheth", repo = "gloq" }
gleam = ">= 0.32.0"
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
Expand Down
93 changes: 51 additions & 42 deletions src/gloq.gleam
Original file line number Diff line number Diff line change
@@ -1,80 +1,89 @@
import dot_env as dot
import dot_env/env
import gleam/hackney
import gleam/http
import gleam/http/request
import gleam/json

// dev use
// loads the .env file and returns the value of the GROQ_API_KEY
fn read_env(key_name) -> String {
dot.new()
|> dot.set_path(".env")
|> dot.set_debug(True)
|> dot.load()
pub type GroqRequestBuilder {
GroqRequestBuilder(key: String, user: String, context: String, model: String)
}

case env.get_string(key_name) {
Ok(key) -> key
Error(e) -> "Key Not Found" <> e
}
/// Creates a new GroqRequestBuilder with default model and user values.
pub fn default_groq_request() -> GroqRequestBuilder {
GroqRequestBuilder(
key: "",
user: "user",
context: "",
model: "llama3-8b-8192",
)
}

/// Create a new GroqRequestBuilder with no default values.
pub fn new_groq_request() -> GroqRequestBuilder {
GroqRequestBuilder(key: "", user: "", context: "", model: "")
}

/// Enum of all models available for the GroqRequestBuilder.
/// Sets the API key for the GroqRequestBuilder.
pub fn with_key(builder: GroqRequestBuilder, key: String) -> GroqRequestBuilder {
GroqRequestBuilder(..builder, key: key)
}

/// Makes a request to the GroqCloud API for chat completions.
///
/// This function sends a POST request to the Groq API endpoint for chat completions.
/// It constructs the request body with the provided user message and context,
/// sets the necessary headers including the API key, and handles the response.
///
/// Parameters:
/// - key: String - The API key for authentication with Groq.
/// - user: String - The role of the message sender (e.g., "user" or "system").
/// - context: String - The content of the message or prompt.
/// - model: String - The name of the Groq model to use for the completion.
///
/// Returns:
/// - String - The response body from the API if successful, or an error message if the request fails.
///
/// Note: This function uses the hackney HTTP client for making the request.
pub fn groq_request(
key: String,
/// Sets the user role for the GroqRequestBuilder.
pub fn with_user(
builder: GroqRequestBuilder,
user: String,
) -> GroqRequestBuilder {
GroqRequestBuilder(..builder, user: user)
}

/// Sets the context/prompt for the GroqRequestBuilder.
pub fn with_context(
builder: GroqRequestBuilder,
context: String,
) -> GroqRequestBuilder {
GroqRequestBuilder(..builder, context: context)
}

/// Sets the model for the GroqRequestBuilder.
pub fn with_model(
builder: GroqRequestBuilder,
model: String,
) -> String {
) -> GroqRequestBuilder {
GroqRequestBuilder(..builder, model: model)
}

/// Sends the request to the GroqCloud API for chat completions.
pub fn send(builder: GroqRequestBuilder) -> String {
let body =
json.object([
#(
"messages",
json.array(
[
json.object([
#("role", json.string(user)),
#("content", json.string(context)),
#("role", json.string(builder.user)),
#("content", json.string(builder.context)),
]),
],
of: fn(x) { x },
),
),
#("model", json.string(model)),
#("model", json.string(builder.model)),
])

let req =
request.new()
|> request.set_method(http.Post)
|> request.set_host("api.groq.com")
|> request.set_path("/openai/v1/chat/completions")
|> request.set_header("Authorization", "Bearer " <> key)
|> request.set_header("Authorization", "Bearer " <> builder.key)
|> request.set_header("Content-Type", "application/json")
|> request.set_body(json.to_string(body))

let res = hackney.send(req)

case res {
Ok(r) -> {
r.body
}
Error(_e) -> {
"Error, Request Failed"
}
Ok(r) -> r.body
Error(_) -> "Error, Request Failed"
}
}

0 comments on commit 7013bf6

Please sign in to comment.