|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/ollama/ollama/api" |
| 13 | + "github.com/ollama/ollama/openai" |
| 14 | +) |
| 15 | + |
| 16 | +func TestEmbeddingsMiddleware_EncodingFormats(t *testing.T) { |
| 17 | + testCases := []struct { |
| 18 | + name string |
| 19 | + encodingFormat string |
| 20 | + expectType string // "array" or "string" |
| 21 | + verifyBase64 bool |
| 22 | + }{ |
| 23 | + {"float format", "float", "array", false}, |
| 24 | + {"base64 format", "base64", "string", true}, |
| 25 | + {"default format", "", "array", false}, |
| 26 | + } |
| 27 | + |
| 28 | + gin.SetMode(gin.TestMode) |
| 29 | + |
| 30 | + endpoint := func(c *gin.Context) { |
| 31 | + resp := api.EmbedResponse{ |
| 32 | + Embeddings: [][]float32{{0.1, -0.2, 0.3}}, |
| 33 | + PromptEvalCount: 5, |
| 34 | + } |
| 35 | + c.JSON(http.StatusOK, resp) |
| 36 | + } |
| 37 | + |
| 38 | + router := gin.New() |
| 39 | + router.Use(EmbeddingsMiddleware()) |
| 40 | + router.Handle(http.MethodPost, "/api/embed", endpoint) |
| 41 | + |
| 42 | + for _, tc := range testCases { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + body := `{"input": "test", "model": "test-model"` |
| 45 | + if tc.encodingFormat != "" { |
| 46 | + body += `, "encoding_format": "` + tc.encodingFormat + `"` |
| 47 | + } |
| 48 | + body += `}` |
| 49 | + |
| 50 | + req, _ := http.NewRequest(http.MethodPost, "/api/embed", strings.NewReader(body)) |
| 51 | + req.Header.Set("Content-Type", "application/json") |
| 52 | + |
| 53 | + resp := httptest.NewRecorder() |
| 54 | + router.ServeHTTP(resp, req) |
| 55 | + |
| 56 | + if resp.Code != http.StatusOK { |
| 57 | + t.Fatalf("expected status 200, got %d", resp.Code) |
| 58 | + } |
| 59 | + |
| 60 | + var result openai.EmbeddingList |
| 61 | + if err := json.Unmarshal(resp.Body.Bytes(), &result); err != nil { |
| 62 | + t.Fatalf("failed to unmarshal response: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + if len(result.Data) != 1 { |
| 66 | + t.Fatalf("expected 1 embedding, got %d", len(result.Data)) |
| 67 | + } |
| 68 | + |
| 69 | + switch tc.expectType { |
| 70 | + case "array": |
| 71 | + if _, ok := result.Data[0].Embedding.([]interface{}); !ok { |
| 72 | + t.Errorf("expected array, got %T", result.Data[0].Embedding) |
| 73 | + } |
| 74 | + case "string": |
| 75 | + embStr, ok := result.Data[0].Embedding.(string) |
| 76 | + if !ok { |
| 77 | + t.Errorf("expected string, got %T", result.Data[0].Embedding) |
| 78 | + } else if tc.verifyBase64 { |
| 79 | + decoded, err := base64.StdEncoding.DecodeString(embStr) |
| 80 | + if err != nil { |
| 81 | + t.Errorf("invalid base64: %v", err) |
| 82 | + } else if len(decoded) != 12 { |
| 83 | + t.Errorf("expected 12 bytes, got %d", len(decoded)) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestEmbeddingsMiddleware_BatchWithBase64(t *testing.T) { |
| 92 | + gin.SetMode(gin.TestMode) |
| 93 | + |
| 94 | + endpoint := func(c *gin.Context) { |
| 95 | + resp := api.EmbedResponse{ |
| 96 | + Embeddings: [][]float32{ |
| 97 | + {0.1, 0.2}, |
| 98 | + {0.3, 0.4}, |
| 99 | + {0.5, 0.6}, |
| 100 | + }, |
| 101 | + PromptEvalCount: 10, |
| 102 | + } |
| 103 | + c.JSON(http.StatusOK, resp) |
| 104 | + } |
| 105 | + |
| 106 | + router := gin.New() |
| 107 | + router.Use(EmbeddingsMiddleware()) |
| 108 | + router.Handle(http.MethodPost, "/api/embed", endpoint) |
| 109 | + |
| 110 | + body := `{ |
| 111 | + "input": ["hello", "world", "test"], |
| 112 | + "model": "test-model", |
| 113 | + "encoding_format": "base64" |
| 114 | + }` |
| 115 | + |
| 116 | + req, _ := http.NewRequest(http.MethodPost, "/api/embed", strings.NewReader(body)) |
| 117 | + req.Header.Set("Content-Type", "application/json") |
| 118 | + |
| 119 | + resp := httptest.NewRecorder() |
| 120 | + router.ServeHTTP(resp, req) |
| 121 | + |
| 122 | + if resp.Code != http.StatusOK { |
| 123 | + t.Fatalf("expected status 200, got %d", resp.Code) |
| 124 | + } |
| 125 | + |
| 126 | + var result openai.EmbeddingList |
| 127 | + if err := json.Unmarshal(resp.Body.Bytes(), &result); err != nil { |
| 128 | + t.Fatalf("failed to unmarshal response: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + if len(result.Data) != 3 { |
| 132 | + t.Fatalf("expected 3 embeddings, got %d", len(result.Data)) |
| 133 | + } |
| 134 | + |
| 135 | + // All should be base64 strings |
| 136 | + for i := range 3 { |
| 137 | + embeddingStr, ok := result.Data[i].Embedding.(string) |
| 138 | + if !ok { |
| 139 | + t.Errorf("embedding %d: expected string, got %T", i, result.Data[i].Embedding) |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + // Verify it's valid base64 |
| 144 | + if _, err := base64.StdEncoding.DecodeString(embeddingStr); err != nil { |
| 145 | + t.Errorf("embedding %d: invalid base64: %v", i, err) |
| 146 | + } |
| 147 | + |
| 148 | + // Check index |
| 149 | + if result.Data[i].Index != i { |
| 150 | + t.Errorf("embedding %d: expected index %d, got %d", i, i, result.Data[i].Index) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestEmbeddingsMiddleware_InvalidEncodingFormat(t *testing.T) { |
| 156 | + gin.SetMode(gin.TestMode) |
| 157 | + |
| 158 | + endpoint := func(c *gin.Context) { |
| 159 | + c.Status(http.StatusOK) |
| 160 | + } |
| 161 | + |
| 162 | + router := gin.New() |
| 163 | + router.Use(EmbeddingsMiddleware()) |
| 164 | + router.Handle(http.MethodPost, "/api/embed", endpoint) |
| 165 | + |
| 166 | + testCases := []struct { |
| 167 | + name string |
| 168 | + encodingFormat string |
| 169 | + shouldFail bool |
| 170 | + }{ |
| 171 | + {"valid: float", "float", false}, |
| 172 | + {"valid: base64", "base64", false}, |
| 173 | + {"valid: FLOAT (uppercase)", "FLOAT", false}, |
| 174 | + {"valid: BASE64 (uppercase)", "BASE64", false}, |
| 175 | + {"valid: Float (mixed)", "Float", false}, |
| 176 | + {"valid: Base64 (mixed)", "Base64", false}, |
| 177 | + {"invalid: json", "json", true}, |
| 178 | + {"invalid: hex", "hex", true}, |
| 179 | + {"invalid: invalid_format", "invalid_format", true}, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tc := range testCases { |
| 183 | + t.Run(tc.name, func(t *testing.T) { |
| 184 | + body := `{ |
| 185 | + "input": "test", |
| 186 | + "model": "test-model", |
| 187 | + "encoding_format": "` + tc.encodingFormat + `" |
| 188 | + }` |
| 189 | + |
| 190 | + req, _ := http.NewRequest(http.MethodPost, "/api/embed", strings.NewReader(body)) |
| 191 | + req.Header.Set("Content-Type", "application/json") |
| 192 | + |
| 193 | + resp := httptest.NewRecorder() |
| 194 | + router.ServeHTTP(resp, req) |
| 195 | + |
| 196 | + if tc.shouldFail { |
| 197 | + if resp.Code != http.StatusBadRequest { |
| 198 | + t.Errorf("expected status 400, got %d", resp.Code) |
| 199 | + } |
| 200 | + |
| 201 | + var errResp openai.ErrorResponse |
| 202 | + if err := json.Unmarshal(resp.Body.Bytes(), &errResp); err != nil { |
| 203 | + t.Fatalf("failed to unmarshal error response: %v", err) |
| 204 | + } |
| 205 | + |
| 206 | + if errResp.Error.Type != "invalid_request_error" { |
| 207 | + t.Errorf("expected error type 'invalid_request_error', got %q", errResp.Error.Type) |
| 208 | + } |
| 209 | + |
| 210 | + if !strings.Contains(errResp.Error.Message, "encoding_format") { |
| 211 | + t.Errorf("expected error message to mention encoding_format, got %q", errResp.Error.Message) |
| 212 | + } |
| 213 | + } else { |
| 214 | + if resp.Code != http.StatusOK { |
| 215 | + t.Errorf("expected status 200, got %d: %s", resp.Code, resp.Body.String()) |
| 216 | + } |
| 217 | + } |
| 218 | + }) |
| 219 | + } |
| 220 | +} |
0 commit comments