-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: junjie.jiang <[email protected]>
- Loading branch information
1 parent
7f76022
commit 38243dd
Showing
5 changed files
with
318 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* # Licensed to the LF AI & Data foundation under one | ||
* # or more contributor license agreements. See the NOTICE file | ||
* # distributed with this work for additional information | ||
* # regarding copyright ownership. The ASF licenses this file | ||
* # to you under the Apache License, Version 2.0 (the | ||
* # "License"); you may not use this file except in compliance | ||
* # with the License. You may obtain a copy of the License at | ||
* # | ||
* # http://www.apache.org/licenses/LICENSE-2.0 | ||
* # | ||
* # Unless required by applicable law or agreed to in writing, software | ||
* # distributed under the License is distributed on an "AS IS" BASIS, | ||
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* # See the License for the specific language governing permissions and | ||
* # limitations under the License. | ||
*/ | ||
|
||
|
||
package function | ||
|
||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/milvus-io/milvus/pkg/mq/msgstream" | ||
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" | ||
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb" | ||
) | ||
|
||
|
||
type Runner interface { | ||
GetSchema() *schemapb.FunctionSchema | ||
GetOutputFields() []*schemapb.FieldSchema | ||
|
||
MaxBatch() int | ||
ProcessInsert(inputs []*schemapb.FieldData) ([]*schemapb.FieldData, error) | ||
} | ||
|
||
|
||
type FunctionExecutor struct { | ||
runners []Runner | ||
} | ||
|
||
|
||
func newFunctionExecutor(schema *schemapb.CollectionSchema) (*FunctionExecutor, error) { | ||
executor := new(FunctionExecutor) | ||
for _, f_schema := range schema.Functions { | ||
switch f_schema.GetType() { | ||
case schemapb.FunctionType_BM25: | ||
case schemapb.FunctionType_OpenAIEmbedding: | ||
f, err := NewOpenAIEmbeddingFunction(schema, f_schema) | ||
if err != nil { | ||
return nil, err | ||
} | ||
executor.runners = append(executor.runners, f) | ||
default: | ||
return nil, fmt.Errorf("unknown functionRunner type %s", f_schema.GetType().String()) | ||
} | ||
} | ||
return executor, nil | ||
} | ||
|
||
func (executor *FunctionExecutor)processSingeFunction(idx int, msg *msgstream.InsertMsg) ([]*schemapb.FieldData, error){ | ||
runner := executor.runners[idx] | ||
inputs := make([]*schemapb.FieldData, 0, len(runner.GetSchema().InputFieldIds)) | ||
for _, id := range runner.GetSchema().InputFieldIds { | ||
for _, field := range msg.FieldsData{ | ||
if field.FieldId == id { | ||
inputs = append(inputs, field) | ||
} | ||
} | ||
} | ||
|
||
if len(inputs) != len(runner.GetSchema().InputFieldIds) { | ||
return nil, fmt.Errorf("Input field not found") | ||
} | ||
|
||
outputs, err := runner.ProcessInsert(inputs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return outputs, nil | ||
} | ||
|
||
func (executor *FunctionExecutor)ProcessInsert(msg *msgstream.InsertMsg) error{ | ||
numRows := msg.NumRows | ||
for _, runner := range executor.runners { | ||
if numRows > uint64(runner.MaxBatch()) { | ||
return fmt.Errorf("numRows [%d] > function [%s]'s max batch [%d]", numRows, runner.GetSchema().Name, runner.MaxBatch()) | ||
} | ||
} | ||
|
||
outputs := make(chan []*schemapb.FieldData, len(executor.runners)) | ||
var wg sync.WaitGroup | ||
for idx, _ := range executor.runners { | ||
wg.Add(1) | ||
go func(index int) { | ||
defer wg.Done() | ||
data, err := executor.processSingeFunction(index, msg) | ||
if err != nil { | ||
outputs <- nil | ||
} | ||
outputs <- data | ||
}(idx) | ||
} | ||
|
||
wg.Wait() | ||
close(outputs) | ||
for output := range outputs { | ||
msg.FieldsData = append(msg.FieldsData, output...) | ||
} | ||
return nil | ||
} | ||
|
||
|
||
func (executor *FunctionExecutor)ProcessSearch(msg *milvuspb.SearchRequest) error{ | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* # Licensed to the LF AI & Data foundation under one | ||
* # or more contributor license agreements. See the NOTICE file | ||
* # distributed with this work for additional information | ||
* # regarding copyright ownership. The ASF licenses this file | ||
* # to you under the Apache License, Version 2.0 (the | ||
* # "License"); you may not use this file except in compliance | ||
* # with the License. You may obtain a copy of the License at | ||
* # | ||
* # http://www.apache.org/licenses/LICENSE-2.0 | ||
* # | ||
* # Unless required by applicable law or agreed to in writing, software | ||
* # distributed under the License is distributed on an "AS IS" BASIS, | ||
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* # See the License for the specific language governing permissions and | ||
* # limitations under the License. | ||
*/ | ||
|
||
|
||
package function | ||
|
||
|
||
import ( | ||
"io" | ||
"fmt" | ||
"testing" | ||
"net/http" | ||
"net/http/httptest" | ||
"encoding/json" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb" | ||
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb" | ||
"github.com/milvus-io/milvus/internal/models" | ||
"github.com/milvus-io/milvus/pkg/mq/msgstream" | ||
"github.com/milvus-io/milvus-proto/go-api/v2/msgpb" | ||
) | ||
|
||
|
||
func TestFunctionExecutor(t *testing.T) { | ||
suite.Run(t, new(FunctionExecutorSuite)) | ||
} | ||
|
||
type FunctionExecutorSuite struct { | ||
suite.Suite | ||
} | ||
|
||
|
||
func (s *OpenAIEmbeddingFunctionSuite) creataSchema(url string) *schemapb.CollectionSchema{ | ||
return &schemapb.CollectionSchema{ | ||
Name: "test", | ||
Fields: []*schemapb.FieldSchema{ | ||
{FieldID: 100, Name: "int64", DataType: schemapb.DataType_Int64}, | ||
{FieldID: 101, Name: "text", DataType: schemapb.DataType_VarChar}, | ||
{FieldID: 102, Name: "vector", DataType: schemapb.DataType_FloatVector, | ||
TypeParams: []*commonpb.KeyValuePair{ | ||
{Key: "dim", Value: "4"}, | ||
}}, | ||
{FieldID: 103, Name: "vector2", DataType: schemapb.DataType_FloatVector, | ||
TypeParams: []*commonpb.KeyValuePair{ | ||
{Key: "dim", Value: "4"}, | ||
}}, | ||
}, | ||
Functions: []*schemapb.FunctionSchema{ | ||
{ | ||
Name: "test", | ||
Type: schemapb.FunctionType_OpenAIEmbedding, | ||
InputFieldIds: []int64{101}, | ||
OutputFieldIds: []int64{102}, | ||
Params: []*commonpb.KeyValuePair{ | ||
{Key: ModelNameParamKey, Value: "text-embedding-ada-002"}, | ||
{Key: OpenaiApiKeyParamKey, Value: "mock"}, | ||
{Key: OpenaiEmbeddingUrlParamKey, Value: url}, | ||
}, | ||
}, | ||
{ | ||
Name: "test", | ||
Type: schemapb.FunctionType_OpenAIEmbedding, | ||
InputFieldIds: []int64{101}, | ||
OutputFieldIds: []int64{103}, | ||
Params: []*commonpb.KeyValuePair{ | ||
{Key: ModelNameParamKey, Value: "text-embedding-ada-002"}, | ||
{Key: OpenaiApiKeyParamKey, Value: "mock"}, | ||
{Key: OpenaiEmbeddingUrlParamKey, Value: url}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
} | ||
|
||
func (s *OpenAIEmbeddingFunctionSuite)createMsg(texts []string) *msgstream.InsertMsg{ | ||
|
||
data := []*schemapb.FieldData{} | ||
f := schemapb.FieldData{ | ||
Type: schemapb.DataType_VarChar, | ||
FieldId: 101, | ||
IsDynamic: false, | ||
Field: &schemapb.FieldData_Scalars{ | ||
Scalars: &schemapb.ScalarField{ | ||
Data: &schemapb.ScalarField_StringData{ | ||
StringData: &schemapb.StringArray{ | ||
Data: texts, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
data = append(data, &f) | ||
|
||
msg := msgstream.InsertMsg{ | ||
InsertRequest: &msgpb.InsertRequest{ | ||
FieldsData: data, | ||
}, | ||
} | ||
return &msg | ||
} | ||
|
||
|
||
func (s *OpenAIEmbeddingFunctionSuite)createEmbedding(texts []string, dim int) [][]float32 { | ||
embeddings := make([][]float32, 0) | ||
for i := 0; i < len(texts); i++ { | ||
f := float32(i) | ||
emb := make([]float32, 0) | ||
for j := 0; j < dim; j++ { | ||
emb = append(emb, f + float32(j) * 0.1) | ||
} | ||
embeddings = append(embeddings, emb) | ||
} | ||
return embeddings | ||
} | ||
|
||
func (s *OpenAIEmbeddingFunctionSuite) TestExecutor() { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var req models.EmbeddingRequest | ||
body, _ := io.ReadAll(r.Body) | ||
defer r.Body.Close() | ||
json.Unmarshal(body, &req) | ||
|
||
var res models.EmbeddingResponse | ||
res.Object = "list" | ||
res.Model = "text-embedding-3-small" | ||
embs := s.createEmbedding(req.Input, 4) | ||
for i := 0; i < len(req.Input); i++ { | ||
res.Data = append(res.Data, models.EmbeddingData{ | ||
Object: "embedding", | ||
Embedding: embs[i], | ||
Index: i, | ||
}) | ||
} | ||
|
||
res.Usage = models.Usage{ | ||
PromptTokens: 1, | ||
TotalTokens: 100, | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
data, _ := json.Marshal(res) | ||
w.Write(data) | ||
|
||
})) | ||
|
||
defer ts.Close() | ||
schema := s.creataSchema(ts.URL) | ||
exec, err := newFunctionExecutor(schema) | ||
s.NoError(err) | ||
msg := s.createMsg([]string{"sentence", "sentence"}) | ||
exec.ProcessInsert(msg) | ||
fmt.Println(msg) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.