diff --git a/gleam.toml b/gleam.toml index 8e1f096..9b6edf5 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ 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. @@ -7,6 +7,7 @@ version = "1.0.1" 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 diff --git a/src/gloq.gleam b/src/gloq.gleam index 4c9b74a..04428e6 100644 --- a/src/gloq.gleam +++ b/src/gloq.gleam @@ -1,46 +1,59 @@ -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([ #( @@ -48,14 +61,14 @@ pub fn groq_request( 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 = @@ -63,18 +76,14 @@ pub fn groq_request( |> 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" } }