-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathembedding.go
More file actions
77 lines (63 loc) · 1.6 KB
/
Copy pathembedding.go
File metadata and controls
77 lines (63 loc) · 1.6 KB
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
package openai
import (
"context"
"fmt"
"go.jetify.com/ai/api"
"go.jetify.com/ai/provider/openai/internal/codec"
)
// EmbeddingModel represents an OpenAI embedding model.
type EmbeddingModel struct {
modelID string
pc ProviderConfig
}
var _ api.EmbeddingModel[string] = &EmbeddingModel{}
// NewEmbeddingModel creates a new OpenAI embedding model.
func (p *Provider) NewEmbeddingModel(modelID string) *EmbeddingModel {
// Create model with provider's client
model := &EmbeddingModel{
modelID: modelID,
pc: ProviderConfig{
providerName: fmt.Sprintf("%s.embedding", p.name),
client: p.client,
},
}
return model
}
func (m *EmbeddingModel) ProviderName() string {
return m.pc.providerName
}
func (m *EmbeddingModel) SpecificationVersion() string {
return "v2"
}
func (m *EmbeddingModel) ModelID() string {
return m.modelID
}
// SupportsParallelCalls implements api.EmbeddingModel.
func (m *EmbeddingModel) SupportsParallelCalls() bool {
return true
}
// MaxEmbeddingsPerCall implements api.EmbeddingModel.
func (m *EmbeddingModel) MaxEmbeddingsPerCall() *int {
max := 2048
return &max
}
// DoEmbed implements api.EmbeddingModel.
func (m *EmbeddingModel) DoEmbed(
ctx context.Context,
values []string,
opts api.EmbeddingOptions,
) (api.EmbeddingResponse, error) {
embeddingParams, openaiOpts, _, err := codec.EncodeEmbedding(
m.modelID,
values,
opts,
)
if err != nil {
return api.EmbeddingResponse{}, err
}
resp, err := m.pc.client.Embeddings.New(ctx, embeddingParams, openaiOpts...)
if err != nil {
return api.EmbeddingResponse{}, err
}
return codec.DecodeEmbedding(resp)
}