-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcable.go
140 lines (128 loc) · 3.06 KB
/
cable.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
138
139
140
package simp
import (
"fmt"
"regexp"
"strings"
"github.com/sashabaranov/go-openai"
)
var (
MarkUser = ">>>"
MarkAsst = "<<<"
// captures: whitespace, mark, annotation
marx = regexp.MustCompile(`(?m)^([\t ]*)(>{3,}|<{3,})(?: +(\S+))?$`)
// captures: png, jpg, jpeg, gif, webp urls
imgrx = regexp.MustCompile(`https?://\S+\.(?:png|jpg|jpeg|gif|webp)`)
)
type Message struct {
Role string
Content string
Annotation string
}
type Cable struct {
Thread []Message
Whitespace string
}
func (c *Cable) AppendUser(s string) {
c.Thread = append(c.Thread, Message{Role: "user", Content: s})
}
func (c Cable) Empty() bool {
return len(c.Thread) == 0
}
func (c Cable) Messages() []openai.ChatCompletionMessage {
t := []openai.ChatCompletionMessage{}
for _, m := range c.Thread {
mm := openai.ChatCompletionMessage{Role: m.Role}
imgs := imgrx.FindAllString(m.Content, -1)
if len(imgs) == 0 {
mm.Content = m.Content
t = append(t, mm)
continue
}
mm.MultiContent = append(mm.MultiContent, openai.ChatMessagePart{
Type: "text",
Text: m.Content,
})
for _, img := range imgs {
mm.MultiContent = append(mm.MultiContent, openai.ChatMessagePart{
Type: "image_url",
ImageURL: &openai.ChatMessageImageURL{URL: img},
})
}
t = append(t, mm)
}
if len(t) == 0 {
return nil
}
return t
}
func (c Cable) String() string {
var s strings.Builder
for i, m := range c.Thread {
if i == 0 && m.Role == "system" {
s.WriteString(m.Content + "\n\n")
continue
}
s.WriteString(c.Whitespace)
switch m.Role {
case "user":
s.WriteString(MarkUser)
case "assistant":
s.WriteString(MarkAsst)
}
if m.Annotation != "" {
s.WriteString(" " + m.Annotation)
}
s.WriteString("\n" + m.Content + "\n\n")
}
return s.String()
}
// ParseCable attempts to parse a string into a Cable structure.
//
// It returns an error if the input is not a cable, or appears
// to be malformed.
func ParseCable(s string) (c Cable, err error) {
c.Whitespace = "\t"
trim := strings.TrimSpace
s = trim(s)
marx := marx.FindAllStringSubmatchIndex(s, -1)
if len(marx) == 0 {
// If no marks found, treat entire input as user message
c.Thread = []Message{{Role: "user", Content: s}}
return
}
// Handle system message if present
if marx[0][0] > 0 {
if s := trim(s[:marx[0][0]]); s != "" {
c.Thread = append(c.Thread, Message{Role: "system", Content: s})
}
}
// Record the whitespace pattern.
if len(marx) > 0 {
c.Whitespace = s[marx[0][2]:marx[0][3]]
}
// Process each mark and its following content
for i, pos := range marx {
var m Message
// Determine role based on direction
mark := s[pos[4]:pos[5]]
m.Role = "user"
if mark[0] == '<' {
m.Role = "assistant"
}
// Extract annotation if present
if pos[7] > pos[6] {
m.Annotation = s[pos[6]:pos[7]]
}
// Extract content
head, tail := pos[1], len(s)
if i < len(marx)-1 {
tail = marx[i+1][0]
}
m.Content = trim(s[head:tail])
if m.Content == "" {
return c, fmt.Errorf("empty message content at mark %d", i)
}
c.Thread = append(c.Thread, m)
}
return
}