Packages

An LLM interaction library that makes you feel like a star

Current section

Files

Jump to
starlet src examples@openai_list_models.erl
Raw

src/examples@openai_list_models.erl

-module(examples@openai_list_models).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/openai_list_models.gleam").
-export([main/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/examples/openai_list_models.gleam", 18).
?DOC(false).
-spec run_example(binary()) -> nil.
run_example(Api_key) ->
Result = begin
gleam@result:'try'(
starlet@openai:list_models(Api_key),
fun(Models) ->
gleam_stdlib:println(<<"Available models:"/utf8>>),
gleam_stdlib:println(<<""/utf8>>),
gleam@list:each(
Models,
fun(Model) ->
gleam_stdlib:println(
<<<<<<<<" "/utf8,
(erlang:element(2, Model))/binary>>/binary,
" (owned by: "/utf8>>/binary,
(erlang:element(3, Model))/binary>>/binary,
")"/utf8>>
)
end
),
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(
<<<<"Total: "/utf8,
(erlang:integer_to_binary(erlang:length(Models)))/binary>>/binary,
" models"/utf8>>
),
{ok, nil}
end
)
end,
case Result of
{ok, _} ->
nil;
{error, Err} ->
gleam_stdlib:println(
<<"Error: "/utf8, (examples@utils:error_to_string(Err))/binary>>
)
end.
-file("src/examples/openai_list_models.gleam", 9).
?DOC(false).
-spec main() -> nil.
main() ->
Api_key = begin
_pipe = envoy_ffi:get(<<"OPENAI_API_KEY"/utf8>>),
gleam@result:unwrap(_pipe, <<""/utf8>>)
end,
case Api_key of
<<""/utf8>> ->
gleam_stdlib:println(
<<"Error: OPENAI_API_KEY environment variable not set"/utf8>>
);
_ ->
run_example(Api_key)
end.