Packages

An LLM interaction library that makes you feel like a star

Current section

Files

Jump to
starlet src examples@ollama_json_output.erl
Raw

src/examples@ollama_json_output.erl

-module(examples@ollama_json_output).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/ollama_json_output.gleam").
-export([main/0]).
-export_type([person/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).
-type person() :: {person, binary(), integer(), binary()}.
-file("src/examples/ollama_json_output.gleam", 16).
?DOC(false).
-spec person_decoder() -> gleam@dynamic@decode:decoder(person()).
person_decoder() ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"age"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Age) ->
gleam@dynamic@decode:field(
<<"city"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(City) ->
gleam@dynamic@decode:success(
{person, Name, Age, City}
)
end
)
end
)
end
).
-file("src/examples/ollama_json_output.gleam", 23).
?DOC(false).
-spec main() -> nil.
main() ->
Client = starlet@ollama:new(<<"http://localhost:11434"/utf8>>),
Person_schema = begin
_pipe = jscheam@schema:object(
[jscheam@schema:prop(<<"name"/utf8>>, jscheam@schema:string()),
jscheam@schema:prop(<<"age"/utf8>>, jscheam@schema:integer()),
jscheam@schema:prop(<<"city"/utf8>>, jscheam@schema:string())]
),
jscheam@schema:disallow_additional_props(_pipe)
end,
Result = begin
Msg = <<"Extract the person info: John Smith is 30 years old and lives in Paris."/utf8>>,
Chat = begin
_pipe@1 = starlet:chat(Client, <<"qwen3:0.6b"/utf8>>),
_pipe@2 = starlet:system(
_pipe@1,
<<"You are a helpful assistant that extracts structured data."/utf8>>
),
_pipe@3 = starlet:with_json_output(_pipe@2, Person_schema),
starlet:user(_pipe@3, Msg)
end,
gleam_stdlib:println(<<"User: "/utf8, Msg/binary>>),
gleam_stdlib:println(<<""/utf8>>),
gleam@result:'try'(
starlet:send(Chat),
fun(_use0) ->
{_, Turn} = _use0,
Json_string = starlet:json(Turn),
gleam_stdlib:println(<<"Raw JSON: "/utf8, Json_string/binary>>),
gleam_stdlib:println(<<""/utf8>>),
case gleam@json:parse(Json_string, person_decoder()) of
{ok, Person} ->
gleam_stdlib:println(<<"Parsed person:"/utf8>>),
gleam_stdlib:println(
<<" Name: "/utf8,
(erlang:element(2, Person))/binary>>
),
gleam_stdlib:println(
<<" Age: "/utf8,
(erlang:integer_to_binary(
erlang:element(3, Person)
))/binary>>
),
gleam_stdlib:println(
<<" City: "/utf8,
(erlang:element(4, Person))/binary>>
),
{ok, nil};
{error, Err} ->
gleam_stdlib:println(
<<"Failed to parse JSON: "/utf8,
(gleam@string:inspect(Err))/binary>>
),
{ok, nil}
end
end
)
end,
case Result of
{ok, _} ->
nil;
{error, Err@1} ->
gleam_stdlib:println(
<<"Error: "/utf8,
(examples@utils:error_to_string(Err@1))/binary>>
)
end.