-
Notifications
You must be signed in to change notification settings - Fork 0
/
gloq.gleam
89 lines (78 loc) · 2.3 KB
/
gloq.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gleam/hackney
import gleam/http
import gleam/http/request
import gleam/json
pub type GroqRequestBuilder {
GroqRequestBuilder(key: String, user: String, context: String, model: String)
}
/// 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)
}
/// 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,
) -> 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(builder.user)),
#("content", json.string(builder.context)),
]),
],
of: fn(x) { x },
),
),
#("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 " <> 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(_) -> "Error, Request Failed"
}
}