-module(examples@integration_test). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/examples/integration_test.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( " Integration test for Anthropic API\n" "\n" " This script tests actual communication with the Claude API\n" " Run with: gleam run -m examples/integration_test\n" ). -file("src/examples/integration_test.gleam", 137). -spec test_conversation(binary()) -> nil. test_conversation(Api_key) -> gleam_stdlib:println(<<"Testing multi-turn conversation..."/utf8>>), Config_result = begin _pipe = anthropic@config:config_options(), _pipe@1 = anthropic@config:with_api_key(_pipe, Api_key), anthropic@config:load_config(_pipe@1) end, case Config_result of {error, Err} -> gleam_stdlib:println( <<"Config error: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ); {ok, Cfg} -> Api_client = anthropic@client:new(Cfg), Req = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, [anthropic@types@message:user_message( <<"My name is Alice."/utf8>> ), anthropic@types@message:assistant_message( <<"Nice to meet you, Alice!"/utf8>> ), anthropic@types@message:user_message( <<"What is my name?"/utf8>> )], 100 ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"REQUEST:"/utf8>>), gleam_stdlib:println(<<" Model: claude-3-5-haiku-20241022"/utf8>>), gleam_stdlib:println(<<" Messages:"/utf8>>), gleam_stdlib:println(<<" [user]: My name is Alice."/utf8>>), gleam_stdlib:println( <<" [assistant]: Nice to meet you, Alice!"/utf8>> ), gleam_stdlib:println(<<" [user]: What is my name?"/utf8>>), gleam_stdlib:println(<<" Max tokens: 100"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Sending request to API..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), case anthropic@api:create_message(Api_client, Req) of {ok, Response} -> gleam_stdlib:println(<<"RESPONSE:"/utf8>>), gleam_stdlib:println( <<" ID: "/utf8, (erlang:element(2, Response))/binary>> ), gleam_stdlib:println( <<" Content: "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println( <<" Input tokens: "/utf8, (gleam@string:inspect( erlang:element(2, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println( <<" Output tokens: "/utf8, (gleam@string:inspect( erlang:element(3, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"TEST 2: PASSED"/utf8>>); {error, Err@1} -> gleam_stdlib:println( <<"ERROR: "/utf8, (anthropic@types@error:error_to_string(Err@1))/binary>> ), gleam_stdlib:println(<<"TEST 2: FAILED"/utf8>>) end end. -file("src/examples/integration_test.gleam", 198). -spec test_system_prompt(binary()) -> nil. test_system_prompt(Api_key) -> gleam_stdlib:println(<<"Testing system prompt..."/utf8>>), Config_result = begin _pipe = anthropic@config:config_options(), _pipe@1 = anthropic@config:with_api_key(_pipe, Api_key), anthropic@config:load_config(_pipe@1) end, case Config_result of {error, Err} -> gleam_stdlib:println( <<"Config error: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ); {ok, Cfg} -> Api_client = anthropic@client:new(Cfg), Req = begin _pipe@2 = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, [anthropic@types@message:user_message( <<"What do you do?"/utf8>> )], 100 ), anthropic@types@request:with_system( _pipe@2, <<"You are a pirate. Always respond in pirate speak."/utf8>> ) end, gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"REQUEST:"/utf8>>), gleam_stdlib:println(<<" Model: claude-3-5-haiku-20241022"/utf8>>), gleam_stdlib:println( <<" System: You are a pirate. Always respond in pirate speak."/utf8>> ), gleam_stdlib:println(<<" Message: What do you do?"/utf8>>), gleam_stdlib:println(<<" Max tokens: 100"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Sending request to API..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), case anthropic@api:create_message(Api_client, Req) of {ok, Response} -> gleam_stdlib:println(<<"RESPONSE:"/utf8>>), gleam_stdlib:println( <<" ID: "/utf8, (erlang:element(2, Response))/binary>> ), gleam_stdlib:println( <<" Content: "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println( <<" Input tokens: "/utf8, (gleam@string:inspect( erlang:element(2, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println( <<" Output tokens: "/utf8, (gleam@string:inspect( erlang:element(3, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"TEST 3: PASSED"/utf8>>); {error, Err@1} -> gleam_stdlib:println( <<"ERROR: "/utf8, (anthropic@types@error:error_to_string(Err@1))/binary>> ), gleam_stdlib:println(<<"TEST 3: FAILED"/utf8>>) end end. -file("src/examples/integration_test.gleam", 256). -spec test_error_handling(binary()) -> nil. test_error_handling(Api_key) -> gleam_stdlib:println( <<"Testing error handling with invalid model..."/utf8>> ), Config_result = begin _pipe = anthropic@config:config_options(), _pipe@1 = anthropic@config:with_api_key(_pipe, Api_key), anthropic@config:load_config(_pipe@1) end, case Config_result of {error, Err} -> gleam_stdlib:println( <<"Config error: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ); {ok, Cfg} -> Api_client = anthropic@client:new(Cfg), Req = anthropic@types@request:create_request( <<"invalid-model-name"/utf8>>, [anthropic@types@message:user_message(<<"Hello"/utf8>>)], 100 ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"REQUEST:"/utf8>>), gleam_stdlib:println( <<" Model: invalid-model-name (intentionally wrong)"/utf8>> ), gleam_stdlib:println(<<" Message: Hello"/utf8>>), gleam_stdlib:println(<<" Max tokens: 100"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"Sending request to API (expecting error)..."/utf8>> ), gleam_stdlib:println(<<""/utf8>>), case anthropic@api:create_message(Api_client, Req) of {ok, Response} -> gleam_stdlib:println(<<"UNEXPECTED SUCCESS:"/utf8>>), gleam_stdlib:println( <<" Content: "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"TEST 4: UNEXPECTED (expected error)"/utf8>> ); {error, Err@1} -> gleam_stdlib:println(<<"EXPECTED ERROR RECEIVED:"/utf8>>), gleam_stdlib:println( <<" Error: "/utf8, (anthropic@types@error:error_to_string(Err@1))/binary>> ), gleam_stdlib:println( <<" Category: "/utf8, (anthropic@types@error:error_category(Err@1))/binary>> ), gleam_stdlib:println( <<" Retryable: "/utf8, (gleam@string:inspect( anthropic@types@error:is_retryable(Err@1) ))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"TEST 4: PASSED (error handled correctly)"/utf8>> ) end end. -file("src/examples/integration_test.gleam", 307). -spec stop_reason_to_string( gleam@option:option(anthropic@types@request:stop_reason()) ) -> binary(). stop_reason_to_string(Reason) -> case Reason of none -> <<"none"/utf8>>; {some, R} -> anthropic@types@request:stop_reason_to_string(R) end. -file("src/examples/integration_test.gleam", 78). -spec test_simple_message(binary()) -> nil. test_simple_message(Api_key) -> gleam_stdlib:println(<<"Creating client and request..."/utf8>>), Config_result = begin _pipe = anthropic@config:config_options(), _pipe@1 = anthropic@config:with_api_key(_pipe, Api_key), anthropic@config:load_config(_pipe@1) end, case Config_result of {error, Err} -> gleam_stdlib:println( <<"Config error: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ); {ok, Cfg} -> Api_client = anthropic@client:new(Cfg), Req = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, [anthropic@types@message:user_message( <<"Say 'Hello from Gleam!' and nothing else."/utf8>> )], 100 ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"REQUEST:"/utf8>>), gleam_stdlib:println(<<" Model: claude-3-5-haiku-20241022"/utf8>>), gleam_stdlib:println( <<" Message: Say 'Hello from Gleam!' and nothing else."/utf8>> ), gleam_stdlib:println(<<" Max tokens: 100"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Sending request to API..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), case anthropic@api:create_message(Api_client, Req) of {ok, Response} -> gleam_stdlib:println(<<"RESPONSE:"/utf8>>), gleam_stdlib:println( <<" ID: "/utf8, (erlang:element(2, Response))/binary>> ), gleam_stdlib:println( <<" Model: "/utf8, (erlang:element(6, Response))/binary>> ), gleam_stdlib:println( <<" Role: "/utf8, (anthropic@types@message:role_to_string( erlang:element(4, Response) ))/binary>> ), gleam_stdlib:println( <<" Stop reason: "/utf8, (stop_reason_to_string(erlang:element(7, Response)))/binary>> ), gleam_stdlib:println( <<" Input tokens: "/utf8, (gleam@string:inspect( erlang:element(2, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println( <<" Output tokens: "/utf8, (gleam@string:inspect( erlang:element(3, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println( <<" Content: "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"TEST 1: PASSED"/utf8>>); {error, Err@1} -> gleam_stdlib:println( <<"ERROR: "/utf8, (anthropic@types@error:error_to_string(Err@1))/binary>> ), gleam_stdlib:println(<<"TEST 1: FAILED"/utf8>>) end end. -file("src/examples/integration_test.gleam", 41). -spec run_tests(binary()) -> nil. run_tests(Api_key) -> gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 1: Simple Text Message"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_simple_message(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 2: Multi-turn Conversation"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_conversation(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 3: System Prompt"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_system_prompt(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 4: Error Handling (Invalid Model)"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_error_handling(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<"Integration Tests Complete"/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>). -file("src/examples/integration_test.gleam", 321). -spec get_api_key() -> {ok, binary()} | {error, nil}. get_api_key() -> Value = begin _pipe = os:getenv( unicode:characters_to_list(<<"ANTHROPIC_API_KEY"/utf8>>), unicode:characters_to_list(<<""/utf8>>) ), unicode:characters_to_binary(_pipe) end, case Value of <<""/utf8>> -> {error, nil}; V -> {ok, V} end. -file("src/examples/integration_test.gleam", 18). ?DOC(" Main entry point\n"). -spec main() -> nil. main() -> gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<"Anthropic API Integration Test"/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<""/utf8>>), case get_api_key() of {error, nil} -> gleam_stdlib:println( <<"ERROR: ANTHROPIC_API_KEY environment variable not set"/utf8>> ), gleam_stdlib:println(<<"Please set it and try again:"/utf8>>), gleam_stdlib:println( <<" export ANTHROPIC_API_KEY=your-api-key"/utf8>> ); {ok, Api_key} -> gleam_stdlib:println( <<<<"API Key: [REDACTED - "/utf8, (gleam@string:slice(Api_key, 0, 8))/binary>>/binary, "...]"/utf8>> ), gleam_stdlib:println(<<""/utf8>>), run_tests(Api_key) end.