File size: 1,173 Bytes
f16d50c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package imitate

import (
	"fmt"
	"strings"
)

func ConvertToString(chatgptResponse *ChatGPTResponse, previousText *StringStruct, role bool, id string, model string) string {
	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
	}

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