Current section

Files

Jump to
gloq src gloq.erl
Raw

src/gloq.erl

-module(gloq).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([groq_request/4]).
-spec read_env(binary()) -> binary().
read_env(Key_name) ->
_pipe = dot_env:new(),
_pipe@1 = dot_env:set_path(_pipe, <<".env"/utf8>>),
_pipe@2 = dot_env:set_debug(_pipe@1, true),
dot_env:load(_pipe@2),
case dot_env_ffi:get_env(Key_name) of
{ok, Key} ->
Key;
{error, E} ->
<<"Key Not Found"/utf8, E/binary>>
end.
-spec groq_request(binary(), binary(), binary(), binary()) -> binary().
groq_request(Key, User, Context, Model) ->
Body = gleam@json:object(
[{<<"messages"/utf8>>,
gleam@json:array(
[gleam@json:object(
[{<<"role"/utf8>>, gleam@json:string(User)},
{<<"content"/utf8>>, gleam@json:string(Context)}]
)],
fun(X) -> X end
)},
{<<"model"/utf8>>, gleam@json:string(Model)}]
),
Req = begin
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_host(_pipe@1, <<"api.groq.com"/utf8>>),
_pipe@3 = gleam@http@request:set_path(
_pipe@2,
<<"/openai/v1/chat/completions"/utf8>>
),
_pipe@4 = gleam@http@request:set_header(
_pipe@3,
<<"Authorization"/utf8>>,
<<"Bearer "/utf8, Key/binary>>
),
_pipe@5 = gleam@http@request:set_header(
_pipe@4,
<<"Content-Type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@5, gleam@json:to_string(Body))
end,
Res = gleam@hackney:send(Req),
case Res of
{ok, R} ->
erlang:element(4, R);
{error, _} ->
<<"Error, Request Failed"/utf8>>
end.