-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c81864
commit 7013bf6
Showing
2 changed files
with
53 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |