2023-12-15 00:47:40 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-12-15 19:59:18 +00:00
|
|
|
"bytes"
|
2023-12-15 00:47:40 +00:00
|
|
|
"context"
|
2024-03-29 01:54:01 +00:00
|
|
|
"encoding/binary"
|
2023-12-15 02:18:25 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-12-15 00:47:40 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-12-15 02:18:25 +00:00
|
|
|
"os"
|
2024-01-16 18:34:44 +00:00
|
|
|
"sort"
|
2023-12-15 02:18:25 +00:00
|
|
|
"strings"
|
2023-12-15 00:47:40 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-05-22 05:21:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-15 02:18:25 +00:00
|
|
|
|
2024-03-26 20:04:17 +00:00
|
|
|
"github.com/ollama/ollama/api"
|
2024-06-13 19:52:03 +00:00
|
|
|
"github.com/ollama/ollama/envconfig"
|
2024-06-19 21:19:02 +00:00
|
|
|
"github.com/ollama/ollama/llm"
|
2024-05-20 18:26:45 +00:00
|
|
|
"github.com/ollama/ollama/parser"
|
2024-05-08 21:36:08 +00:00
|
|
|
"github.com/ollama/ollama/types/model"
|
2024-03-26 20:04:17 +00:00
|
|
|
"github.com/ollama/ollama/version"
|
2023-12-15 00:47:40 +00:00
|
|
|
)
|
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
func createTestFile(t *testing.T, name string) string {
|
|
|
|
t.Helper()
|
2023-12-15 19:59:18 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), name)
|
2024-05-30 01:22:03 +00:00
|
|
|
require.NoError(t, err)
|
2024-05-13 22:27:51 +00:00
|
|
|
defer f.Close()
|
2024-03-29 01:54:01 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
err = binary.Write(f, binary.LittleEndian, []byte("GGUF"))
|
2024-05-30 01:22:03 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-18 18:41:02 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
err = binary.Write(f, binary.LittleEndian, uint32(3))
|
2024-05-30 01:22:03 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-29 01:54:01 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
err = binary.Write(f, binary.LittleEndian, uint64(0))
|
2024-05-30 01:22:03 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-29 01:54:01 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
err = binary.Write(f, binary.LittleEndian, uint64(0))
|
2024-05-30 01:22:03 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-29 01:54:01 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
return f.Name()
|
|
|
|
}
|
2023-12-15 19:59:18 +00:00
|
|
|
|
2024-05-13 22:27:51 +00:00
|
|
|
func Test_Routes(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Name string
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Setup func(t *testing.T, req *http.Request)
|
|
|
|
Expected func(t *testing.T, resp *http.Response)
|
2023-12-18 18:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createTestModel := func(t *testing.T, name string) {
|
2024-05-08 21:36:08 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-12-18 18:41:02 +00:00
|
|
|
fname := createTestFile(t, "ollama-model")
|
|
|
|
|
2024-04-30 17:55:19 +00:00
|
|
|
r := strings.NewReader(fmt.Sprintf("FROM %s\nPARAMETER seed 42\nPARAMETER top_p 0.9\nPARAMETER stop foo\nPARAMETER stop bar", fname))
|
2024-05-20 18:26:45 +00:00
|
|
|
modelfile, err := parser.ParseFile(r)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-18 18:41:02 +00:00
|
|
|
fn := func(resp api.ProgressResponse) {
|
|
|
|
t.Logf("Status: %s", resp.Status)
|
|
|
|
}
|
2024-05-08 21:36:08 +00:00
|
|
|
err = CreateModel(context.TODO(), model.ParseName(name), "", "", modelfile, fn)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 19:59:18 +00:00
|
|
|
}
|
2023-12-15 00:47:40 +00:00
|
|
|
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
Name: "Version Handler",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/version",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Equal(t, "application/json; charset=utf-8", contentType)
|
2023-12-15 00:47:40 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-19 23:02:37 +00:00
|
|
|
assert.Equal(t, fmt.Sprintf(`{"version":"%s"}`, version.Version), string(body))
|
2023-12-15 00:47:40 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-15 02:18:25 +00:00
|
|
|
{
|
|
|
|
Name: "Tags Handler (no tags)",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/tags",
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Equal(t, "application/json; charset=utf-8", contentType)
|
2023-12-15 02:18:25 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 02:18:25 +00:00
|
|
|
|
|
|
|
var modelList api.ListResponse
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &modelList)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 02:18:25 +00:00
|
|
|
|
2024-05-14 18:18:10 +00:00
|
|
|
assert.NotNil(t, modelList.Models)
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Empty(t, len(modelList.Models))
|
2023-12-15 02:18:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Tags Handler (yes tags)",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/api/tags",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
2023-12-15 19:59:18 +00:00
|
|
|
createTestModel(t, "test-model")
|
2023-12-15 02:18:25 +00:00
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Equal(t, "application/json; charset=utf-8", contentType)
|
2023-12-15 02:18:25 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 02:18:25 +00:00
|
|
|
|
2024-06-06 17:11:45 +00:00
|
|
|
assert.NotContains(t, string(body), "expires_at")
|
|
|
|
|
2023-12-15 02:18:25 +00:00
|
|
|
var modelList api.ListResponse
|
|
|
|
err = json.Unmarshal(body, &modelList)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 02:18:25 +00:00
|
|
|
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Len(t, modelList.Models, 1)
|
|
|
|
assert.Equal(t, "test-model:latest", modelList.Models[0].Name)
|
2023-12-15 19:59:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Create Model Handler",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Path: "/api/create",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
2024-04-16 22:37:28 +00:00
|
|
|
fname := createTestFile(t, "ollama-model")
|
2023-12-15 19:59:18 +00:00
|
|
|
|
|
|
|
stream := false
|
|
|
|
createReq := api.CreateRequest{
|
|
|
|
Name: "t-bone",
|
2024-04-16 22:37:28 +00:00
|
|
|
Modelfile: fmt.Sprintf("FROM %s", fname),
|
2023-12-15 19:59:18 +00:00
|
|
|
Stream: &stream,
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(createReq)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 19:59:18 +00:00
|
|
|
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(jsonData))
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, "application/json", contentType)
|
|
|
|
_, err := io.ReadAll(resp.Body)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 200, resp.StatusCode)
|
2023-12-15 19:59:18 +00:00
|
|
|
|
|
|
|
model, err := GetModel("t-bone")
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 19:59:18 +00:00
|
|
|
assert.Equal(t, "t-bone:latest", model.ShortName)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Copy Model Handler",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Path: "/api/copy",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
|
|
|
createTestModel(t, "hamshank")
|
|
|
|
copyReq := api.CopyRequest{
|
|
|
|
Source: "hamshank",
|
|
|
|
Destination: "beefsteak",
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(copyReq)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 19:59:18 +00:00
|
|
|
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(jsonData))
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
model, err := GetModel("beefsteak")
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-15 19:59:18 +00:00
|
|
|
assert.Equal(t, "beefsteak:latest", model.ShortName)
|
2023-12-15 02:18:25 +00:00
|
|
|
},
|
|
|
|
},
|
2024-01-16 18:34:44 +00:00
|
|
|
{
|
|
|
|
Name: "Show Model Handler",
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Path: "/api/show",
|
|
|
|
Setup: func(t *testing.T, req *http.Request) {
|
|
|
|
createTestModel(t, "show-model")
|
|
|
|
showReq := api.ShowRequest{Model: "show-model"}
|
|
|
|
jsonData, err := json.Marshal(showReq)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-16 18:34:44 +00:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(jsonData))
|
|
|
|
},
|
|
|
|
Expected: func(t *testing.T, resp *http.Response) {
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
2024-05-22 05:21:04 +00:00
|
|
|
assert.Equal(t, "application/json; charset=utf-8", contentType)
|
2024-01-16 18:34:44 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-16 18:34:44 +00:00
|
|
|
|
|
|
|
var showResp api.ShowResponse
|
|
|
|
err = json.Unmarshal(body, &showResp)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-16 18:34:44 +00:00
|
|
|
|
|
|
|
var params []string
|
|
|
|
paramsSplit := strings.Split(showResp.Parameters, "\n")
|
|
|
|
for _, p := range paramsSplit {
|
|
|
|
params = append(params, strings.Join(strings.Fields(p), " "))
|
|
|
|
}
|
|
|
|
sort.Strings(params)
|
|
|
|
expectedParams := []string{
|
|
|
|
"seed 42",
|
|
|
|
"stop \"bar\"",
|
|
|
|
"stop \"foo\"",
|
|
|
|
"top_p 0.9",
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedParams, params)
|
2024-06-19 21:19:02 +00:00
|
|
|
assert.InDelta(t, 0, showResp.ModelInfo["general.parameter_count"], 1e-9, "Parameter count should be 0")
|
2024-01-16 18:34:44 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-15 00:47:40 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 21:57:15 +00:00
|
|
|
t.Setenv("OLLAMA_MODELS", t.TempDir())
|
2024-06-13 19:52:03 +00:00
|
|
|
envconfig.LoadConfig()
|
2024-05-24 21:57:15 +00:00
|
|
|
|
2024-03-14 17:24:13 +00:00
|
|
|
s := &Server{}
|
2023-12-15 00:47:40 +00:00
|
|
|
router := s.GenerateRoutes()
|
|
|
|
|
|
|
|
httpSrv := httptest.NewServer(router)
|
|
|
|
t.Cleanup(httpSrv.Close)
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2024-04-16 22:37:28 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
u := httpSrv.URL + tc.Path
|
|
|
|
req, err := http.NewRequestWithContext(context.TODO(), tc.Method, u, nil)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2024-04-16 22:37:28 +00:00
|
|
|
|
|
|
|
if tc.Setup != nil {
|
|
|
|
tc.Setup(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := httpSrv.Client().Do(req)
|
2024-05-22 05:21:04 +00:00
|
|
|
require.NoError(t, err)
|
2024-04-16 22:37:28 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if tc.Expected != nil {
|
|
|
|
tc.Expected(t, resp)
|
|
|
|
}
|
|
|
|
})
|
2023-12-15 00:47:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-13 22:27:51 +00:00
|
|
|
|
|
|
|
func TestCase(t *testing.T) {
|
|
|
|
t.Setenv("OLLAMA_MODELS", t.TempDir())
|
2024-06-13 19:52:03 +00:00
|
|
|
envconfig.LoadConfig()
|
2024-05-13 22:27:51 +00:00
|
|
|
|
|
|
|
cases := []string{
|
|
|
|
"mistral",
|
|
|
|
"llama3:latest",
|
|
|
|
"library/phi3:q4_0",
|
|
|
|
"registry.ollama.ai/library/gemma:q5_K_M",
|
|
|
|
// TODO: host:port currently fails on windows (#4107)
|
|
|
|
// "localhost:5000/alice/bob:latest",
|
|
|
|
}
|
|
|
|
|
|
|
|
var s Server
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt, func(t *testing.T) {
|
|
|
|
w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
|
|
|
|
Name: tt,
|
2024-06-07 15:55:46 +00:00
|
|
|
Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
|
2024-05-13 22:27:51 +00:00
|
|
|
Stream: &stream,
|
|
|
|
})
|
|
|
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
t.Fatalf("expected status 200 got %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect, err := json.Marshal(map[string]string{"error": "a model with that name already exists"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("create", func(t *testing.T) {
|
|
|
|
w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
|
|
|
|
Name: strings.ToUpper(tt),
|
2024-06-07 15:55:46 +00:00
|
|
|
Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
|
2024-05-13 22:27:51 +00:00
|
|
|
Stream: &stream,
|
|
|
|
})
|
|
|
|
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
|
|
t.Fatalf("expected status 500 got %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(w.Body.Bytes(), expect) {
|
|
|
|
t.Fatalf("expected error %s got %s", expect, w.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("pull", func(t *testing.T) {
|
|
|
|
w := createRequest(t, s.PullModelHandler, api.PullRequest{
|
|
|
|
Name: strings.ToUpper(tt),
|
|
|
|
Stream: &stream,
|
|
|
|
})
|
|
|
|
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
|
|
t.Fatalf("expected status 500 got %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(w.Body.Bytes(), expect) {
|
|
|
|
t.Fatalf("expected error %s got %s", expect, w.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("copy", func(t *testing.T) {
|
|
|
|
w := createRequest(t, s.CopyModelHandler, api.CopyRequest{
|
|
|
|
Source: tt,
|
|
|
|
Destination: strings.ToUpper(tt),
|
|
|
|
})
|
|
|
|
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
|
|
t.Fatalf("expected status 500 got %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(w.Body.Bytes(), expect) {
|
|
|
|
t.Fatalf("expected error %s got %s", expect, w.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 21:19:02 +00:00
|
|
|
|
|
|
|
func TestShow(t *testing.T) {
|
|
|
|
t.Setenv("OLLAMA_MODELS", t.TempDir())
|
|
|
|
envconfig.LoadConfig()
|
|
|
|
|
|
|
|
var s Server
|
|
|
|
|
|
|
|
createRequest(t, s.CreateModelHandler, api.CreateRequest{
|
|
|
|
Name: "show-model",
|
|
|
|
Modelfile: fmt.Sprintf(
|
|
|
|
"FROM %s\nFROM %s",
|
|
|
|
createBinFile(t, llm.KV{"general.architecture": "test"}, nil),
|
|
|
|
createBinFile(t, llm.KV{"general.architecture": "clip"}, nil),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
w := createRequest(t, s.ShowModelHandler, api.ShowRequest{
|
|
|
|
Name: "show-model",
|
|
|
|
})
|
|
|
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
t.Fatalf("expected status code 200, actual %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp api.ShowResponse
|
|
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ModelInfo["general.architecture"] != "test" {
|
|
|
|
t.Fatal("Expected model architecture to be 'test', but got", resp.ModelInfo["general.architecture"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ProjectorInfo["general.architecture"] != "clip" {
|
|
|
|
t.Fatal("Expected projector architecture to be 'clip', but got", resp.ProjectorInfo["general.architecture"])
|
|
|
|
}
|
|
|
|
}
|