Spaces:
Running
Running
File size: 4,573 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package client
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/usecases/modulecomponents/ent"
)
func TestGetScore(t *testing.T) {
t.Run("when the server has a successful answer", func(t *testing.T) {
server := httptest.NewServer(&testCrossRankerHandler{
t: t,
res: RankResponse{
Query: "Where do I work?",
Scores: []DocumentScore{
{
Document: "I work at Apple",
Score: 0.15,
},
},
},
})
defer server.Close()
c := New(server.URL, 0, nullLogger())
res, err := c.Rank(context.Background(), "Where do I work?", []string{"I work at Apple"}, nil)
assert.Nil(t, err)
assert.Equal(t, ent.RankResult{
Query: "Where do I work?",
DocumentScores: []ent.DocumentScore{
{
Document: "I work at Apple",
Score: 0.15,
},
},
}, *res)
})
t.Run("when the server has an error", func(t *testing.T) {
server := httptest.NewServer(&testCrossRankerHandler{
t: t,
res: RankResponse{
Error: "some error from the server",
},
})
defer server.Close()
c := New(server.URL, 0, nullLogger())
_, err := c.Rank(context.Background(), "prop",
[]string{"I work at Apple"}, nil)
require.NotNil(t, err)
assert.Contains(t, err.Error(), "some error from the server")
})
t.Run("when we send requests in batches", func(t *testing.T) {
server := httptest.NewServer(&testCrossRankerHandler{
t: t,
res: RankResponse{
Query: "Where do I work?",
Scores: []DocumentScore{
{
Document: "I work at Apple",
Score: 0.15,
},
},
},
batchedResults: [][]DocumentScore{
{
{
Document: "Response 1",
Score: 0.99,
},
{
Document: "Response 2",
Score: 0.89,
},
},
{
{
Document: "Response 3",
Score: 0.19,
},
{
Document: "Response 4",
Score: 0.29,
},
},
{
{
Document: "Response 5",
Score: 0.79,
},
{
Document: "Response 6",
Score: 0.789,
},
},
{
{
Document: "Response 7",
Score: 0.0001,
},
},
},
})
defer server.Close()
c := New(server.URL, 0, nullLogger())
c.maxDocuments = 2
query := "Where do I work?"
documents := []string{
"Response 1", "Response 2", "Response 3", "Response 4",
"Response 5", "Response 6", "Response 7",
}
resp, err := c.Rank(context.Background(), query, documents, nil)
require.Nil(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.DocumentScores)
for i := range resp.DocumentScores {
assert.Equal(t, documents[i], resp.DocumentScores[i].Document)
if i == 0 {
assert.Equal(t, 0.99, resp.DocumentScores[i].Score)
}
if i == len(documents)-1 {
assert.Equal(t, 0.0001, resp.DocumentScores[i].Score)
}
}
})
}
type testCrossRankerHandler struct {
lock sync.RWMutex
t *testing.T
res RankResponse
batchedResults [][]DocumentScore
}
func (f *testCrossRankerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.lock.Lock()
defer f.lock.Unlock()
assert.Equal(f.t, "/rerank", r.URL.String())
assert.Equal(f.t, http.MethodPost, r.Method)
if f.res.Error != "" {
w.WriteHeader(500)
}
bodyBytes, err := io.ReadAll(r.Body)
require.Nil(f.t, err)
defer r.Body.Close()
var req RankInput
require.Nil(f.t, json.Unmarshal(bodyBytes, &req))
containsDocument := func(req RankInput, in string) bool {
for _, doc := range req.Documents {
if doc == in {
return true
}
}
return false
}
index := 0
if len(f.batchedResults) > 0 {
if containsDocument(req, "Response 3") {
index = 1
}
if containsDocument(req, "Response 5") {
index = 2
}
if containsDocument(req, "Response 7") {
index = 3
}
f.res.Scores = f.batchedResults[index]
}
jsonBytes, _ := json.Marshal(f.res)
w.Write(jsonBytes)
}
|