Ollama Translator
An Erlang library for translating text using the Ollama API.
Features
- 🌐 Translate text to any target language
- ⚙️ Configurable via environment variables or runtime configuration
- 🔧 Support for custom Ollama endpoints and models
- 📝 Customizable prompt templates
- 🎯 Simple and focused API
Installation
Add to your rebar.config:
{deps, [
{ollama_translator, "0.1.0"}
]}.Quick Start
% Start your Erlang shell
$ rebar3 shell
% Simple translation
1> ollama_translator:translate("French", "Hello world").
{ok, <<"Bonjour le monde">>}
2> ollama_translator:translate("Spanish", "How are you?").
{ok, <<"¿Cómo estás?">>}
3> ollama_translator:translate(<<"German">>, "Good morning").
{ok, <<"Guten Morgen">>}
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="Translate the following text to ~s. Only return the translation, no explanations:\n\n~s" # DefaultRuntime Configuration
% Custom configuration
Config = #{
endpoint => "http://my-ollama-server:11434/api/generate",
model => <<"llama2">>,
prompt_template => "Traduis ce texte en ~s :\n\n~s"
},
% Use custom config
ollama_translator:translate("Italian", "Hello world", Config).
% Modify default config partially
BaseConfig = ollama_translator:get_env_config(),
MyConfig = BaseConfig#{model => <<"codellama">>},
ollama_translator:translate("Portuguese", "Good bye", MyConfig).API Reference
Main Functions
translate/2,3
translate(TargetLanguage, Text) -> {ok, Translation} | {error, Reason}.
translate(TargetLanguage, Text, Config) -> {ok, Translation} | {error, Reason}.Translates text to the specified target language.
TargetLanguagecan be a string or binary (e.g.,"French",<<"Spanish">>)Textis the text to translate as a string- Returns the translation as a binary
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 translation results to stdout.
Configuration Map
Config = #{
endpoint => "http://localhost:11434/api/generate", % Ollama API endpoint
model => <<"phi3">>, % Model name as binary
prompt_template => "Translate text to ~s:\n\n~s" % Prompt with 2 ~s placeholders
}.Note: The prompt template must contain exactly 2 ~s placeholders:
-
First
~sfor the target language -
Second
~sfor the text to translate
Prerequisites
- Erlang/OTP 27+
- Running Ollama instance
inetsapplication for HTTP requests
Dependencies
This library requires:
inets(part of OTP) for HTTP client
Add to your rebar.config:
{deps, [
ollama_translator
]}.Examples
Basic Usage
% Various languages
ollama_translator:translate("French", "Hello").
ollama_translator:translate("Spanish", "Good morning").
ollama_translator:translate("German", "How are you?").
ollama_translator:translate("Italian", "Thank you very much").
ollama_translator:translate("Portuguese", "See you later").
ollama_translator:translate("Russian", "Good night").
ollama_translator:translate("Chinese", "Welcome").
ollama_translator:translate("Japanese", "Excuse me").Advanced Usage
% Custom model for better translations
Config = #{model => <<"llama2">>},
ollama_translator:translate("French", "The weather is beautiful today", Config).
% Custom prompt for formal translation
FormalConfig = #{
prompt_template => "Provide a formal translation of this text to ~s:\n\n~s"
},
ollama_translator:translate("Japanese", "Could you help me please?", FormalConfig).
% Technical translation
TechConfig = #{
model => <<"codellama">>,
prompt_template => "Translate this technical text to ~s, keeping technical terms:\n\n~s"
},
ollama_translator:translate("German", "Database connection failed", TechConfig).Error Handling
case ollama_translator:translate("French", "Hello world") of
{ok, Translation} ->
io:format("Translation: ~s~n", [Translation]);
{error, {ollama_error, StatusCode, Body}} ->
io:format("Ollama API error ~p: ~s~n", [StatusCode, Body]);
{error, {request_failed, Reason}} ->
io:format("Request failed: ~p~n", [Reason]);
{error, Reason} ->
io:format("Other error: ~p~n", [Reason])
end.Batch Translation
% Translate multiple texts
Texts = ["Hello", "Good morning", "Thank you", "Goodbye"],
Translations = [ollama_translator:translate("Spanish", Text) || Text <- Texts],
[ollama_translator:print_result(Result) || Result <- Translations].Language Examples
The library supports any language that your Ollama model can handle. Common examples:
- European:
"French","Spanish","German","Italian","Portuguese","Dutch","Russian" - Asian:
"Chinese","Japanese","Korean","Thai","Vietnamese" - Other:
"Arabic","Hebrew","Hindi","Turkish","Polish"
You can also be more specific: "Mexican Spanish", "Brazilian Portuguese", "Simplified Chinese", etc.
Development
# Clone the repository
git clone https://github.com/roquess/ollama_translator_erlang.git
cd ollama_translator
# Install dependencies and compile
rebar3 compile
# Run tests (if available)
rebar3 eunit
# Start shell for testing
rebar3 shellIntegration with Other Libraries
Works well with other text processing libraries:
% With ollama_summarizer for translate summaries
{ok, Summary} = ollama_summarizer:summarize_url("https://example.com"),
{ok, Translation} = ollama_translator:translate("French", binary_to_list(Summary)).Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Changelog
0.1.0
- Initial release
- Basic text translation
- Environment variable configuration
- Custom configuration support
- Support for string and binary language input