Ollama Summarizer

An Erlang library for summarizing web content and HTML using the Ollama API.

Hex.pmHex DocsLicense

Features

Installation

Add to your rebar.config:

{deps, [
    {ollama_summarizer, "0.1.0"}}
]}.

Or with hex (once published):

{deps, [ollama_summarizer]}.

Quick Start

% Start your Erlang shell
$ rebar3 shell

% Summarize a webpage
1> ollama_summarizer:summarize_url("https://example.com").
{ok, <<"This is a summary of the webpage content...">>}

% Summarize HTML directly
2> Html = "<html><body><h1>Title</h1><p>Some content here</p></body></html>",
2> ollama_summarizer:summarize_html(Html).
{ok, <<"Summary of the HTML content...">>}

% Print the result nicely
3> Result = ollama_summarizer:summarize_url("https://news.ycombinator.com"),
3> ollama_summarizer:print_result(Result).

Configuration

Environment Variables

Set these environment variables to override defaults:

export OLLAMA_ENDPOINT="http://localhost:11434/api/generate"  # Default
export OLLAMA_MODEL="phi3"                                   # Default  
export OLLAMA_PROMPT="Résume ce contenu en français en quelques phrases claires et concises :\n\n~s"  # Default

Runtime Configuration

% Custom configuration
Config = #{
    endpoint => "http://my-ollama-server:11434/api/generate",
    model => <<"llama2">>,
    prompt_template => "Summarize this content in English:\n\n~s"
},

% Use custom config
ollama_summarizer:summarize_url("https://example.com", Config).

% Modify default config partially
BaseConfig = ollama_summarizer:get_env_config(),
MyConfig = BaseConfig#{model => <<"codellama">>},
ollama_summarizer:summarize_html(Html, MyConfig).

API Reference

Main Functions

summarize_url/1,2

summarize_url(Url) -> {ok, Summary} | {error, Reason}.
summarize_url(Url, Config) -> {ok, Summary} | {error, Reason}.

Fetches and summarizes content from a URL.

summarize_html/1,2

summarize_html(Html) -> {ok, Summary} | {error, Reason}.
summarize_html(Html, Config) -> {ok, Summary} | {error, Reason}.

Summarizes HTML content directly.

summarize_with_ollama/1,2

summarize_with_ollama(Text) -> {ok, Summary} | {error, Reason}.
summarize_with_ollama(Text, Config) -> {ok, Summary} | {error, Reason}.

Summarizes plain text content.

Configuration Functions

default_config/0

default_config() -> Config.

Returns hardcoded default configuration.

get_env_config/0

get_env_config() -> Config.

Returns configuration with environment variable overrides.

Utility Functions

print_result/1

print_result(Result) -> ok | error.

Pretty prints summarization results to stdout.

Configuration Map

Config = #{
    endpoint => "http://localhost:11434/api/generate",  % Ollama API endpoint
    model => <<"phi3">>,                               % Model name as binary
    prompt_template => "Summarize this:\n\n~s"         % Prompt with ~s placeholder
}.

Prerequisites

Dependencies

This library requires:

Add to your rebar.config:

{deps, [
    ollama_summarizer
]}.

Examples

Basic Usage

% Summarize Hacker News
Result = ollama_summarizer:summarize_url("https://news.ycombinator.com"),
ollama_summarizer:print_result(Result).

% Summarize with different model
Config = #{model => <<"llama2">>},
ollama_summarizer:summarize_url("https://example.com", Config).

Advanced Usage

% Custom prompt for technical content
TechConfig = #{
    model => <<"codellama">>,
    prompt_template => "Provide a technical summary of this code/content:\n\n~s"
},

% Summarize GitHub README
ollama_summarizer:summarize_url("https://raw.githubusercontent.com/user/repo/main/README.md", TechConfig).

Error Handling

case ollama_summarizer:summarize_url("https://invalid-url.com") of
    {ok, Summary} ->
        io:format("Summary: ~s~n", [Summary]);
    {error, {fetch_failed, Reason}} ->
        io:format("Failed to fetch URL: ~p~n", [Reason]);
    {error, {ollama_error, StatusCode, Body}} ->
        io:format("Ollama API error ~p: ~s~n", [StatusCode, Body]);
    {error, Reason} ->
        io:format("Other error: ~p~n", [Reason])
end.

Development

# Clone the repository
git clone https://github.com/roquess/ollama_summarizer.git
cd ollama_summarizer

# Compile
rebar3 compile

# Run tests (if available)
rebar3 eunit

# Start shell for testing
rebar3 shell

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

Changelog

0.1.0