Skip to content

Commit

Permalink
Ollama.init/1 accepts keyword opts. Recieve timeout defaults to 1min. F…
Browse files Browse the repository at this point in the history
…ixes #3.
  • Loading branch information
lebrunel committed Feb 16, 2024
1 parent 2ee1673 commit de2686d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
37 changes: 31 additions & 6 deletions lib/ollama.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,52 @@ defmodule Ollama do
@typep req_response() :: {:ok, Req.Response.t()} | {:error, term()} | Task.t()


@default_req_opts [
base_url: "http://localhost:11434/api",
receive_timeout: 60_000,
]

@doc """
Creates a new API client with the provided URL. If no URL is given, it
defaults to `"http://localhost:11434/api"`.
Creates a new Ollama API client. Accepts either a base URL for the Ollama API,
a keyword list of options passed to `Req.new/1`, or an existing `t:Req.Request.t/0`
struct.
If no arguments are given, the client is initiated with the default options:
```elixir
@default_req_opts [
base_url: "http://localhost:11434/api",
receive_timeout: 60_000,
]
```
## Examples
iex> client = Ollama.init("https://ollama.service.ai:11434/api")
%Ollama{}
"""
@spec init(Req.url() | Req.Request.t()) :: client()
def init(url \\ "http://localhost:11434/api")
@spec init(Req.url() | keyword() | Req.Request.t()) :: client()
def init(opts \\ [])

def init(url) when is_binary(url),
do: struct(__MODULE__, req: Req.new(base_url: url))
do: struct(__MODULE__, req: init_req(base_url: url))

def init(%URI{} = url),
do: struct(__MODULE__, req: Req.new(base_url: url))
do: struct(__MODULE__, req: init_req(base_url: url))

def init(opts) when is_list(opts),
do: struct(__MODULE__, req: init_req(opts))

def init(%Req.Request{} = req),
do: struct(__MODULE__, req: req)

@spec init_req(keyword()) :: Req.Request.t()
defp init_req(opts) do
@default_req_opts
|> Keyword.merge(opts)
|> Req.new()
end


schema :chat, [
model: [
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Ollama.MixProject do
name: "Ollama",
description: "A nifty little library for working with Ollama in Elixir.",
source_url: "https://github.com/lebrunel/ollama-ex",
version: "0.4.0",
version: "0.4.1",
elixir: "~> 1.13",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
2 changes: 1 addition & 1 deletion test/support/stream_catcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Ollama.StreamCatcher do

@impl true
def handle_info({_from, {:data, data}}, state) do
state = case String.valid?(data) do
state = case is_binary(data) do
false ->
[data | state]
true ->
Expand Down

0 comments on commit de2686d

Please sign in to comment.