Skip to content

Commit

Permalink
user agent header. a few tests covering Ollama.init/1
Browse files Browse the repository at this point in the history
  • Loading branch information
lebrunel committed Feb 21, 2024
1 parent 3b54379 commit 98090ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/ollama.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Ollama do
@version Keyword.fetch!(Mix.Project.config(), :version)
@moduledoc """
![Ollama-ex](https://raw.githubusercontent.com/lebrunel/ollama-ex/main/media/poster.webp)
Expand All @@ -21,7 +22,7 @@ defmodule Ollama do
```elixir
def deps do
[
{:ollama, "#{Keyword.fetch!(Mix.Project.config(), :version)}"}
{:ollama, "#{@version}"}
]
end
```
Expand Down Expand Up @@ -201,6 +202,9 @@ defmodule Ollama do

@default_req_opts [
base_url: "http://localhost:11434/api",
headers: [
{"user-agent", "ollama-ex/#{@version}"}
],
receive_timeout: 60_000,
]

Expand Down Expand Up @@ -240,9 +244,11 @@ defmodule Ollama do

@spec init_req(keyword()) :: Req.Request.t()
defp init_req(opts) do
{headers, opts} = Keyword.pop(opts, :headers, [])
@default_req_opts
|> Keyword.merge(opts)
|> Req.new()
|> Req.update(headers: headers)
end


Expand Down
33 changes: 33 additions & 0 deletions test/ollama_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ defmodule OllamaTest do
{:ok, client: Ollama.init("http://localhost:4000")}
end

describe "init/2" do
test "default client" do
client = Ollama.init()
assert "http://localhost:11434/api" = client.req.options.base_url
assert %{"user-agent" => _val} = client.req.headers
end

test "client with custom base url" do
client = Ollama.init("https://ollama.my.site/api")
assert "https://ollama.my.site/api" = client.req.options.base_url
end

test "client with custom req opts" do
client = Ollama.init(receive_timeout: :infinity)
assert "http://localhost:11434/api" = client.req.options.base_url
assert :infinity = client.req.options.receive_timeout
end

test "client with custom req struct" do
client = Ollama.init(Req.new(base_url: "https://ollama.my.site/api"))
assert "https://ollama.my.site/api" = client.req.options.base_url
end

test "client with merged headers" do
client = Ollama.init(headers: [
{"User-Agent", "testing"},
{"X-Test", "testing"},
])
assert "http://localhost:11434/api" = client.req.options.base_url
assert %{"user-agent" => ["testing"], "x-test" => ["testing"]} = client.req.headers
end
end

describe "chat2" do
test "generates a response for a given prompt", %{client: client} do
assert {:ok, res} = Ollama.chat(client, [
Expand Down

0 comments on commit 98090ea

Please sign in to comment.