123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //go:build integration
- package integration
- import (
- "context"
- "testing"
- "time"
- "github.com/ollama/ollama/api"
- )
- func TestAllMiniLMEmbed(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
- defer cancel()
- req := api.EmbedRequest{
- Model: "all-minilm",
- Input: "why is the sky blue?",
- }
- res, err := EmbedTestHelper(ctx, t, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- if len(res.Embeddings) != 1 {
- t.Fatalf("expected 1 embedding, got %d", len(res.Embeddings))
- }
- if len(res.Embeddings[0]) != 384 {
- t.Fatalf("expected 384 floats, got %d", len(res.Embeddings[0]))
- }
- if res.Embeddings[0][0] != 0.010071029038540258 {
- t.Fatalf("expected 0.010071029038540258, got %f", res.Embeddings[0][0])
- }
- }
- func TestAllMiniLMBatchEmbed(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
- defer cancel()
- req := api.EmbedRequest{
- Model: "all-minilm",
- Input: []string{"why is the sky blue?", "why is the grass green?"},
- }
- res, err := EmbedTestHelper(ctx, t, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- if len(res.Embeddings) != 2 {
- t.Fatalf("expected 2 embeddings, got %d", len(res.Embeddings))
- }
- if len(res.Embeddings[0]) != 384 {
- t.Fatalf("expected 384 floats, got %d", len(res.Embeddings[0]))
- }
- if res.Embeddings[0][0] != 0.010071029038540258 || res.Embeddings[1][0] != -0.00980270794235093 {
- t.Fatalf("expected 0.010071029038540258 and -0.00980270794235093, got %f and %f", res.Embeddings[0][0], res.Embeddings[1][0])
- }
- }
- func TestAllMiniLmEmbedTruncate(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
- defer cancel()
- truncTrue, truncFalse := true, false
- type testReq struct {
- Name string
- Request api.EmbedRequest
- }
- reqs := []testReq{
- {
- Name: "Target Truncation",
- Request: api.EmbedRequest{
- Model: "all-minilm",
- Input: "why",
- },
- },
- {
- Name: "Default Truncate",
- Request: api.EmbedRequest{
- Model: "all-minilm",
- Input: "why is the sky blue?",
- Options: map[string]any{"num_ctx": 1},
- },
- },
- {
- Name: "Explicit Truncate",
- Request: api.EmbedRequest{
- Model: "all-minilm",
- Input: "why is the sky blue?",
- Truncate: &truncTrue,
- Options: map[string]any{"num_ctx": 1},
- },
- },
- }
- res := make(map[string]*api.EmbedResponse)
- for _, req := range reqs {
- response, err := EmbedTestHelper(ctx, t, req.Request)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- res[req.Name] = response
- }
- if res["Target Truncation"].Embeddings[0][0] != res["Default Truncate"].Embeddings[0][0] {
- t.Fatalf("expected default request to truncate correctly")
- }
- if res["Default Truncate"].Embeddings[0][0] != res["Explicit Truncate"].Embeddings[0][0] {
- t.Fatalf("expected default request and truncate true request to be the same")
- }
- // check that truncate set to false returns an error if context length is exceeded
- _, err := EmbedTestHelper(ctx, t, api.EmbedRequest{
- Model: "all-minilm",
- Input: "why is the sky blue?",
- Truncate: &truncFalse,
- Options: map[string]any{"num_ctx": 1},
- })
- if err == nil {
- t.Fatalf("expected error, got nil")
- }
- }
|