Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unifying string and vision converter #43

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 15 additions & 64 deletions pkg/adapter/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ type ChatCompletionMessage struct {
Content json.RawMessage `json:"content"`
}

func (m *ChatCompletionMessage) stringContent() (str string, err error) {
err = json.Unmarshal(m.Content, &str)
if err != nil {
return "", errors.Wrap(err, "json.Unmarshal")
}
return
}

func (m *ChatCompletionMessage) multiContent() (parts []openai.ChatMessagePart, err error) {
err = json.Unmarshal(m.Content, &parts)
if err != nil {
return nil, errors.Wrap(err, "json.Unmarshal")
}
return
}

// ChatCompletionRequest represents a request structure for chat completion API.
type ChatCompletionRequest struct {
Model string `json:"model" binding:"required"`
Expand All @@ -42,21 +26,29 @@ type ChatCompletionRequest struct {
}

func (req *ChatCompletionRequest) ToGenaiMessages() ([]*genai.Content, error) {
if req.Model == Gemini1Dot5ProV || req.Model == openai.GPT4VisionPreview {
return req.toVisionGenaiContent()
} else if req.Model == TextEmbedding004 || req.Model == string(openai.AdaEmbeddingV2) {
if req.Model == TextEmbedding004 || req.Model == string(openai.AdaEmbeddingV2) {
return nil, errors.New("Chat Completion is not supported for embedding model")
}

return req.toStringGenaiContent()
return req.toVisionGenaiContent()
}

func (req *ChatCompletionRequest) toVisionGenaiContent() ([]*genai.Content, error) {
content := make([]*genai.Content, 0, len(req.Messages))
for _, message := range req.Messages {
parts, err := message.multiContent()
if err != nil {
return nil, errors.Wrap(err, "message.multiContent")
var parts []openai.ChatMessagePart

// Attempt to unmarshal into a slice of parts
if err := json.Unmarshal(message.Content, &parts); err != nil {
// If it fails, try unmarshalling into a single string
var singleString string
if err := json.Unmarshal(message.Content, &singleString); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal message content")
}
// Convert single string to a part
parts = []openai.ChatMessagePart{
{Type: openai.ChatMessagePartTypeText, Text: singleString},
}
}

prompt := make([]genai.Part, 0, len(parts))
Expand Down Expand Up @@ -103,47 +95,6 @@ func (req *ChatCompletionRequest) toVisionGenaiContent() ([]*genai.Content, erro
return content, nil
}

func (req *ChatCompletionRequest) toStringGenaiContent() ([]*genai.Content, error) {
content := make([]*genai.Content, 0, len(req.Messages))
for _, message := range req.Messages {
str, err := message.stringContent()
if err != nil {
return nil, errors.Wrap(err, "message.stringContent")
}

prompt := []genai.Part{
genai.Text(str),
}

switch message.Role {
case openai.ChatMessageRoleSystem:
content = append(content, []*genai.Content{
{
Parts: prompt,
Role: genaiRoleUser,
},
{
Parts: []genai.Part{
genai.Text(""),
},
Role: genaiRoleModel,
},
}...)
case openai.ChatMessageRoleAssistant:
content = append(content, &genai.Content{
Parts: prompt,
Role: genaiRoleModel,
})
case openai.ChatMessageRoleUser:
content = append(content, &genai.Content{
Parts: prompt,
Role: genaiRoleUser,
})
}
}
return content, nil
}

type CompletionChoice struct {
Index int `json:"index"`
Delta struct {
Expand Down
Loading