Packages
llm_composer
0.5.3
0.20.2
0.20.1
0.20.0
0.19.6
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.2
0.18.1
0.18.0
0.17.1
0.17.0
0.16.2
0.16.1
0.16.0
0.15.0
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.3
0.12.2
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.8.0
0.7.0
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
LlmComposer is an Elixir library that facilitates chat interactions with language models, providing tools to handle user messages, generate responses, and execute functions automatically based on model outputs.
Current section
Files
Jump to
Current section
Files
lib/llm_composer/models/ollama.ex
defmodule LlmComposer.Models.Ollama do
@moduledoc """
Model implementation for Ollama
Basically it calls the Ollama server api for getting the chat responses.
"""
@behaviour LlmComposer.Model
use Tesla
alias LlmComposer.LlmResponse
alias LlmComposer.Models.Utils
@uri Application.compile_env(:llm_composer, :ollama_uri, "http://localhost:11434")
plug(Tesla.Middleware.BaseUrl, @uri)
plug(Tesla.Middleware.JSON)
plug(Tesla.Middleware.Retry,
delay: :timer.seconds(1),
max_delay: :timer.seconds(10),
max_retries: 5,
should_retry: fn
{:ok, %{status: status}} when status in [429, 500, 503] -> true
{:error, :closed} -> true
_other -> false
end
)
@impl LlmComposer.Model
def model_id, do: :ollama
@impl LlmComposer.Model
@doc """
Reference: https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion
"""
def run(messages, system_message, opts) do
model = Keyword.get(opts, :model)
if model do
messages
|> build_request(system_message, model, opts)
|> then(&post("/api/chat", &1))
|> handle_response()
|> LlmResponse.new(model_id())
else
{:error, :model_not_provided}
end
end
defp build_request(messages, system_message, model, opts) do
base_request = %{
model: model,
stream: false,
# tools: get_tools(Keyword.get(opts, :functions)),
messages: Utils.map_messages([system_message | messages])
}
req_params = Keyword.get(opts, :request_params, %{})
base_request
|> Map.merge(req_params)
|> Utils.cleanup_body()
end
@spec handle_response(Tesla.Env.result()) :: {:ok, map()} | {:error, term}
defp handle_response({:ok, %Tesla.Env{status: status, body: body}}) when status in [200] do
{:ok, %{response: body, actions: []}}
end
defp handle_response({:ok, resp}) do
{:error, resp}
end
end