Packages

An LLM interaction library that makes you feel like a star

Current section

Files

Jump to
starlet src examples@anthropic_thinking.erl
Raw

src/examples@anthropic_thinking.erl

-module(examples@anthropic_thinking).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/anthropic_thinking.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_thinking.gleam", 18).
?DOC(false).
-spec run_example(binary()) -> nil.
run_example(Api_key) ->
Client = starlet@anthropic:new(Api_key),
Result = begin
Msg = <<"What is the sum of all prime numbers between 1 and 20? Think through this step by step."/utf8>>,
Chat@1 = case begin
_pipe = starlet:chat(Client, <<"claude-haiku-4-5-20251001"/utf8>>),
starlet@anthropic:with_thinking(_pipe, 16384)
end of
{ok, Chat} -> Chat;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"examples/anthropic_thinking"/utf8>>,
function => <<"run_example"/utf8>>,
line => 25,
value => _assert_fail,
start => 561,
'end' => 679,
pattern_start => 572,
pattern_end => 580})
end,
Chat@2 = begin
_pipe@1 = Chat@1,
_pipe@2 = starlet:max_tokens(_pipe@1, 32000),
starlet:user(_pipe@2, Msg)
end,
gleam_stdlib:println(<<"User: "/utf8, Msg/binary>>),
gleam_stdlib:println(<<""/utf8>>),
gleam@result:'try'(
starlet:send(Chat@2),
fun(_use0) ->
{_, Turn} = _use0,
case starlet@anthropic:thinking(Turn) of
{some, Thinking} ->
gleam_stdlib:println(
<<"=== Claude's Thinking ==="/utf8>>
),
gleam_stdlib:println(Thinking),
gleam_stdlib:println(<<""/utf8>>);
none ->
gleam_stdlib:println(<<"(No thinking content)"/utf8>>)
end,
gleam_stdlib:println(<<"=== Claude's Response ==="/utf8>>),
gleam_stdlib:println(starlet:text(Turn)),
{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/anthropic_thinking.gleam", 9).
?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.