2024-08-16 19:43:27 +00:00
|
|
|
package common
|
|
|
|
|
2024-09-15 13:09:45 +00:00
|
|
|
import goopenai "github.com/sashabaranov/go-openai"
|
|
|
|
|
2024-08-16 19:43:27 +00:00
|
|
|
type Message struct {
|
|
|
|
Role string `json:"role"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatRequest struct {
|
2024-09-03 13:13:03 +00:00
|
|
|
ContextName string
|
|
|
|
SessionName string
|
|
|
|
PatternName string
|
|
|
|
PatternVariables map[string]string
|
|
|
|
Message string
|
2024-08-16 19:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChatOptions struct {
|
2024-09-15 18:38:19 +00:00
|
|
|
Model string
|
|
|
|
Temperature float64
|
|
|
|
TopP float64
|
|
|
|
PresencePenalty float64
|
|
|
|
FrequencyPenalty float64
|
|
|
|
Raw bool
|
2024-08-16 19:43:27 +00:00
|
|
|
}
|
2024-08-22 18:57:49 +00:00
|
|
|
|
|
|
|
// NormalizeMessages remove empty messages and ensure messages order user-assist-user
|
|
|
|
func NormalizeMessages(msgs []*Message, defaultUserMessage string) (ret []*Message) {
|
|
|
|
// Iterate over messages to enforce the odd position rule for user messages
|
|
|
|
fullMessageIndex := 0
|
|
|
|
for _, message := range msgs {
|
|
|
|
if message.Content == "" {
|
|
|
|
// Skip empty messages as the anthropic API doesn't accept them
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure, that each odd position shall be a user message
|
2024-09-15 13:09:45 +00:00
|
|
|
if fullMessageIndex%2 == 0 && message.Role != goopenai.ChatMessageRoleUser {
|
|
|
|
ret = append(ret, &Message{Role: goopenai.ChatMessageRoleUser, Content: defaultUserMessage})
|
2024-08-22 18:57:49 +00:00
|
|
|
fullMessageIndex++
|
|
|
|
}
|
|
|
|
ret = append(ret, message)
|
|
|
|
fullMessageIndex++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|