Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #269 from moeakwak/main
Browse files Browse the repository at this point in the history
support multimodal_text content and attachments
  • Loading branch information
linweiyuan authored Oct 2, 2023
2 parents 47f92fd + 3029d78 commit 7c2f761
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
13 changes: 7 additions & 6 deletions api/chatgpt/typings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ func (c *CreateConversationRequest) AddMessage(role string, content string) {
c.Messages = append(c.Messages, Message{
ID: uuid.New().String(),
Author: Author{Role: role},
Content: Content{ContentType: "text", Parts: []string{content}},
Content: Content{ContentType: "text", Parts: []interface{}{content}},
})
}

type Message struct {
Author Author `json:"author"`
Content Content `json:"content"`
ID string `json:"id"`
Author Author `json:"author"`
Content Content `json:"content"`
ID string `json:"id"`
Metadata interface{} `json:"metadata"`
}

type Author struct {
Role string `json:"role"`
}

type Content struct {
ContentType string `json:"content_type"`
Parts []string `json:"parts"`
ContentType string `json:"content_type"`
Parts []interface{} `json:"parts"`
}

type CreateConversationResponse struct {
Expand Down
25 changes: 23 additions & 2 deletions api/imitate/convert.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package imitate

import (
"fmt"
"strings"
)

func ConvertToString(chatgptResponse *ChatGPTResponse, previousText *StringStruct, role bool, id string, model string) string {
text := strings.ReplaceAll(chatgptResponse.Message.Content.Parts[0], *&previousText.Text, "")
var text string

if len(chatgptResponse.Message.Content.Parts) == 1 {
if part, ok := chatgptResponse.Message.Content.Parts[0].(string); ok {
text = strings.ReplaceAll(part, previousText.Text, "")
previousText.Text = part
} else {
text = fmt.Sprintf("%v", chatgptResponse.Message.Content.Parts[0])
}
} else {
// When using GPT-4 messages with images (multimodal_text), the length of 'parts' might be 2.
// Since the chatgpt API currently does not support multimodal content
// and there is no official format for multimodal content,
// the content is temporarily returned as is.
var parts []string
for _, part := range chatgptResponse.Message.Content.Parts {
parts = append(parts, fmt.Sprintf("%v", part))
}
text = strings.Join(parts, ", ")
}

translatedResponse := NewChatCompletionChunk(text, id, model)
if role {
translatedResponse.Choices[0].Delta.Role = chatgptResponse.Message.Author.Role
}
previousText.Text = chatgptResponse.Message.Content.Parts[0]

return "data: " + translatedResponse.String() + "\n\n"
}

0 comments on commit 7c2f761

Please sign in to comment.