-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_strategy.go
137 lines (117 loc) · 3.36 KB
/
openai_strategy.go
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
package llmconnector
import (
"context"
"encoding/json"
"fmt"
"github.com/simp-lee/gohttpclient"
)
type OpenAIStrategy struct {
chatClient *gohttpclient.Client
embedClient *gohttpclient.Client
config *Config
}
func NewOpenAIStrategy(config Config) (*OpenAIStrategy, error) {
if config.APIKey == "" {
return nil, fmt.Errorf("OpenAI API key is required")
}
// Set default values if not provided
if config.ChatURL == "" {
config.ChatURL = "https://api.openai.com/v1/chat/completions"
}
if config.EmbedURL == "" {
config.EmbedURL = "https://api.openai.com/v1/engines/text-similarity/embeddings"
}
// Use default common config if not set
if config.CommonConfig == (CommonConfig{}) {
config.CommonConfig = DefaultCommonConfig()
}
// Prepare the chat client
chatClient, err := createClient(config.CommonConfig, config.APIKey)
if err != nil {
return nil, fmt.Errorf("failed to create OpenAI chat client: %w", err)
}
// Prepare the embedding client
embedClient, err := createClient(config.CommonConfig, config.APIKey)
if err != nil {
return nil, fmt.Errorf("failed to create OpenAI embedding client: %w", err)
}
return &OpenAIStrategy{
chatClient: chatClient,
embedClient: embedClient,
config: &config,
}, nil
}
func (s *OpenAIStrategy) Chat(ctx context.Context, chatMessages []ChatMessage, options *ChatOptions) (ChatResponse, error) {
request := map[string]interface{}{
"model": options.Model,
"messages": chatMessages,
}
if options.Temperature != nil {
request["temperature"] = *options.Temperature
}
if options.MaxTokens != nil {
request["max_tokens"] = *options.MaxTokens
}
if options.TopP != nil {
request["top_p"] = *options.TopP
}
if options.Stop != nil {
request["stop"] = options.Stop
}
resp, err := s.chatClient.Post(ctx, s.config.ChatURL, request)
if err != nil {
return nil, fmt.Errorf("OpenAI chat request failed: %w", err)
}
var openAIResp OpenAIChatResponse
if err := json.Unmarshal(resp, &openAIResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal OpenAI chat response: %w", err)
}
return &openAIResp, nil
}
type OpenAIChatResponse struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
func (r *OpenAIChatResponse) GetContent() string {
if len(r.Choices) > 0 {
return r.Choices[0].Message.Content
}
return ""
}
func (s *OpenAIStrategy) Embed(ctx context.Context, texts []string, options *EmbedOptions) (EmbedResponse, error) {
request := map[string]interface{}{
"model": options.Model,
"input": map[string]interface{}{
"texts": texts,
},
}
if options.EmbeddingType != "" {
request["params"] = map[string]string{
"text_type": options.EmbeddingType,
}
}
resp, err := s.embedClient.Post(ctx, s.config.EmbedURL, request)
if err != nil {
return nil, fmt.Errorf("OpenAI embed request failed: %w", err)
}
var openAIResp OpenAIEmbedResponse
if err := json.Unmarshal(resp, &openAIResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal OpenAI embed response: %w", err)
}
return &openAIResp, nil
}
type OpenAIEmbedResponse struct {
Data []struct {
Embedding []float32 `json:"embedding"`
} `json:"data"`
}
func (r *OpenAIEmbedResponse) GetEmbeddings() [][]float32 {
embeddings := make([][]float32, len(r.Data))
for i, data := range r.Data {
embeddings[i] = data.Embedding
}
return embeddings
}