Current section
Files
Jump to
Current section
Files
src/gloq.erl
-module(gloq).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([read_env/0, main/0]).
-spec read_env() -> binary().
read_env() ->
_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(<<"GROQ_API_KEY"/utf8>>) of
{ok, Key} ->
Key;
{error, E} ->
<<"Key Not Found"/utf8, E/binary>>
end.
-spec main() -> nil.
main() ->
Api_key = read_env(),
Body = gleam@json:object(
[{<<"messages"/utf8>>,
gleam@json:array(
[gleam@json:object(
[{<<"role"/utf8>>,
gleam@json:string(<<"user"/utf8>>)},
{<<"content"/utf8>>,
gleam@json:string(
<<"Explain the importance of fast language models"/utf8>>
)}]
)],
fun(X) -> X end
)},
{<<"model"/utf8>>, gleam@json:string(<<"llama3-8b-8192"/utf8>>)}]
),
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, Api_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} ->
gleam@io:println(
<<"Response:"/utf8, (erlang:element(4, R))/binary>>
);
{error, _} ->
gleam@io:println(<<"Error, Request Failed"/utf8>>)
end.