Claude Handler

Hex.pmHex DocsLicense

A simple and flexible Erlang library for interacting with the Claude API. This library provides a clean interface to generate text, perform chat completions, and manage configurations for Claude models.

Features

Prerequisites

Quick Start

Basic Text Generation

% Start your Erlang shell
1> application:start(inets).
ok
2> {ok, Response} = claude_handler:generate("Explain quantum computing in simple terms").
{ok, <<"Quantum computing is a revolutionary computing paradigm...">>}
3> claude_handler:print_result({ok, Response}).
Quantum computing is a revolutionary computing paradigm...
ok

Chat Completion

1> Messages = [
    #{role => <<"user">>, content => <<"Hello! How are you today?">>}
].
2> {ok, Response} = claude_handler:chat(Messages).
{ok, <<"Hello! I&#39;m doing well, thank you for asking...">>}

API Reference

Core Functions

generate/1,2

Generate text from a simple prompt.

-spec generate(string() | binary()) -> claude_result().
-spec generate(string() | binary(), config()) -> claude_result().

Examples:

claude_handler:generate("What is the meaning of life?").
claude_handler:generate("Explain AI", #{model => <<"phi3">>, temperature => 0.8}).

chat/1,2

Perform chat completion using the messages format.

-spec chat(messages()) -> claude_result().
-spec chat(messages(), config()) -> claude_result().

Examples:

Messages = [
    #{role => <<"system">>, content => <<"You are a helpful assistant">>},
    #{role => <<"user">>, content => <<"Hello!">>}
],
claude_handler:chat(Messages).

generate_with_context/2,3

Generate text with additional context.

-spec generate_with_context(string() | binary(), string() | binary()) -> claude_result().
-spec generate_with_context(string() | binary(), string() | binary(), config()) -> claude_result().

Examples:

Context = "You are a expert in mathematics",
Prompt = "Explain calculus",
claude_handler:generate_with_context(Context, Prompt).

Configuration Functions

default_config/0

Get the default hardcoded configuration.

Config = claude_handler:default_config().

get_env_config/0

Get configuration from environment variables with fallback to defaults.

Config = claude_handler:get_env_config().

Utility Functions

print_result/1

Print the result of an operation to stdout.

Result = claude_handler:generate("Hello world"),
claude_handler:print_result(Result).

format_prompt/2

Format a prompt template with arguments.

Prompt = claude_handler:format_prompt("Translate &#39;~s&#39; to ~s", ["hello", "French"]).

Examples

Building a Simple Chatbot

-module(simple_chatbot).
-export([start/0, chat_loop/1]).

start() ->
    application:start(inets),
    InitialMessages = [
        #{role => <<"system">>, content => <<"You are a friendly chatbot">>}
    ],
    chat_loop(InitialMessages).

chat_loop(Messages) ->
    io:format("You: "),
    case io:get_line("") of
        eof -> ok;
        Line ->
            UserMessage = #{role => <<"user">>, content => list_to_binary(string:trim(Line))},
            NewMessages = Messages ++ [UserMessage],
            
            case claude_handler:chat(NewMessages) of
                {ok, Response} ->
                    io:format("Bot: ~s~n", [Response]),
                    AssistantMessage = #{role => <<"assistant">>, content => Response},
                    chat_loop(NewMessages ++ [AssistantMessage]);
                {error, Reason} ->
                    io:format("Error: ~p~n", [Reason]),
                    chat_loop(Messages)
            end
    end.

Text Summarization

-module(text_summarizer).
-export([summarize/1]).

summarize(Text) ->
    Context = "You are an expert at summarizing text. Provide a concise summary.",
    Prompt = "Summarize the following text:\n\n" ++ Text,
    
    Config = #{
        model => <<"llama2">>,
        temperature => 0.3,
        max_tokens => 200
    },
    
    claude_handler:generate_with_context(Context, Prompt, Config).

Code Generation

-module(code_generator).
-export([generate_function/2]).

generate_function(Language, Description) ->
    Prompt = io_lib:format("Write a ~s function that ~s. Include comments and proper formatting.", 
                          [Language, Description]),
    
    Config = #{
        model => <<"codellama">>,
        temperature => 0.2,
        max_tokens => 500
    },
    
    claude_handler:generate(Prompt, Config).

Development

Building

rebar3 compile

Running Tests

rebar3 eunit

Type Checking

rebar3 dialyzer

Code Analysis

rebar3 xref

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments