Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
feat(openai): add dimensions in openai component (#200)
Browse files Browse the repository at this point in the history
Because

- in many use cases, users may not need to output vectors of such high
dimensionality and might only require the first 'x' dimensions.

This commit

- add dimensions in api caller
  • Loading branch information
chuang8511 authored Jul 8, 2024
1 parent e27e46c commit 0d08912
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
13 changes: 13 additions & 0 deletions ai/openai/v0/config/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@
],
"title": "Text",
"type": "string"
},
"dimensions": {
"description": "The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.",
"instillAcceptFormats": [
"integer"
],
"instillUIOrder": 2,
"instillUpstreamTypes": [
"value",
"reference"
],
"title": "Dimensions",
"type": "integer"
}
},
"required": [
Expand Down
20 changes: 16 additions & 4 deletions ai/openai/v0/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,22 @@ func (e *execution) Execute(_ context.Context, inputs []*structpb.Struct) ([]*st
}

resp := TextEmbeddingsResp{}
req := client.R().SetBody(TextEmbeddingsReq{
Model: inputStruct.Model,
Input: []string{inputStruct.Text},
}).SetResult(&resp)

var reqParams TextEmbeddingsReq
if inputStruct.Dimensions == 0 {
reqParams = TextEmbeddingsReq{
Model: inputStruct.Model,
Input: []string{inputStruct.Text},
}
} else {
reqParams = TextEmbeddingsReq{
Model: inputStruct.Model,
Input: []string{inputStruct.Text},
Dimensions: inputStruct.Dimensions,
}
}

req := client.R().SetBody(reqParams).SetResult(&resp)

if _, err := req.Post(embeddingsPath); err != nil {
return inputs, err
Expand Down
10 changes: 6 additions & 4 deletions ai/openai/v0/text_embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const (
)

type TextEmbeddingsInput struct {
Text string `json:"text"`
Model string `json:"model"`
Text string `json:"text"`
Model string `json:"model"`
Dimensions int `json:"dimensions"`
}

type TextEmbeddingsOutput struct {
Embedding []float64 `json:"embedding"`
}

type TextEmbeddingsReq struct {
Model string `json:"model"`
Input []string `json:"input"`
Model string `json:"model"`
Dimensions int `json:"dimensions,omitempty"`
Input []string `json:"input"`
}

type TextEmbeddingsResp struct {
Expand Down

0 comments on commit 0d08912

Please sign in to comment.