Packages

An LLM interaction library that makes you feel like a star

Current section

Files

Jump to
starlet src examples@anthropic_chat.erl
Raw

src/examples@anthropic_chat.erl

-module(examples@anthropic_chat).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/anthropic_chat.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/anthropic_chat.gleam", 17).
?DOC(false).
-spec run_example(binary()) -> nil.
run_example(Api_key) ->
Client = starlet@anthropic:new(Api_key),
Result = begin
Msg1 = <<"What is the capital of France?"/utf8>>,
Msg2 = <<"What is its population?"/utf8>>,
Chat = begin
_pipe = starlet:chat(Client, <<"claude-haiku-4-5-20251001"/utf8>>),
_pipe@1 = starlet:system(
_pipe,
<<"You are a helpful assistant. Be concise."/utf8>>
),
starlet:user(_pipe@1, Msg1)
end,
gleam@result:'try'(
starlet:send(Chat),
fun(_use0) ->
{Chat@1, Turn} = _use0,
gleam_stdlib:println(<<"User: "/utf8, Msg1/binary>>),
gleam_stdlib:println(
<<"Claude: "/utf8, (starlet:text(Turn))/binary>>
),
gleam_stdlib:println(<<""/utf8>>),
Chat@2 = starlet:user(Chat@1, Msg2),
gleam@result:'try'(
starlet:send(Chat@2),
fun(_use0@1) ->
{_, Turn@1} = _use0@1,
gleam_stdlib:println(<<"User: "/utf8, Msg2/binary>>),
gleam_stdlib:println(
<<"Claude: "/utf8, (starlet:text(Turn@1))/binary>>
),
{ok, nil}
end
)
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/anthropic_chat.gleam", 8).
?DOC(false).
-spec main() -> nil.
main() ->
Api_key = begin
_pipe = envoy_ffi:get(<<"ANTHROPIC_API_KEY"/utf8>>),
gleam@result:unwrap(_pipe, <<""/utf8>>)
end,
case Api_key of
<<""/utf8>> ->
gleam_stdlib:println(
<<"Error: ANTHROPIC_API_KEY environment variable not set"/utf8>>
);
_ ->
run_example(Api_key)
end.