Skip to content

Commit 8026020

Browse files
Feat multipart (#18)
Feat multipart (#18)
1 parent 8029cea commit 8026020

9 files changed

Lines changed: 983 additions & 11 deletions

File tree

entity/prompt.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ const (
2929
)
3030

3131
type Message struct {
32-
Role Role `json:"role"`
33-
Content *string `json:"content,omitempty"`
32+
Role Role `json:"role"`
33+
Content *string `json:"content,omitempty"`
34+
Parts []*ContentPart `json:"parts,omitempty"`
3435
}
3536

3637
type Role string
@@ -43,6 +44,20 @@ const (
4344
RolePlaceholder Role = "placeholder"
4445
)
4546

47+
type ContentPart struct {
48+
Type ContentType `json:"type"`
49+
Text *string `json:"text,omitempty"`
50+
ImageURL *string `json:"image_url,omitempty"`
51+
}
52+
53+
type ContentType string
54+
55+
const (
56+
ContentTypeText ContentType = "text"
57+
ContentTypeImageURL ContentType = "image_url"
58+
ContentTypeMultiPartVariable ContentType = "multi_part_variable"
59+
)
60+
4661
type ToolType string
4762

4863
const (
@@ -69,6 +84,7 @@ const (
6984
VariableTypeArrayInteger VariableType = "array<integer>"
7085
VariableTypeArrayFloat VariableType = "array<float>"
7186
VariableTypeArrayObject VariableType = "array<object>"
87+
VariableTypeMultiPart VariableType = "multi_part"
7288
)
7389

7490
type ToolChoiceType string
@@ -142,6 +158,35 @@ func (m *Message) DeepCopy() *Message {
142158
if m.Content != nil {
143159
copied.Content = util.Ptr(*m.Content)
144160
}
161+
if m.Parts != nil {
162+
copied.Parts = deepCopyContentParts(m.Parts)
163+
}
164+
return copied
165+
}
166+
167+
func deepCopyContentParts(parts []*ContentPart) []*ContentPart {
168+
if parts == nil {
169+
return nil
170+
}
171+
172+
copied := make([]*ContentPart, len(parts))
173+
for i, part := range parts {
174+
copied[i] = part.DeepCopy()
175+
}
176+
return copied
177+
}
178+
179+
func (cp *ContentPart) DeepCopy() *ContentPart {
180+
if cp == nil {
181+
return nil
182+
}
183+
copied := &ContentPart{
184+
Type: cp.Type,
185+
ImageURL: cp.ImageURL,
186+
}
187+
if cp.Text != nil {
188+
copied.Text = util.Ptr(*cp.Text)
189+
}
145190
return copied
146191
}
147192

entity/prompt_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"testing"
88

99
. "github.com/smartystreets/goconvey/convey"
10+
11+
"github.com/coze-dev/cozeloop-go/internal/util"
1012
)
1113

1214
func TestPromptDeepCopy(t *testing.T) {
@@ -210,6 +212,75 @@ func TestMessageDeepCopy(t *testing.T) {
210212
So(copied.Role, ShouldEqual, RoleTool)
211213
So(copied.Content, ShouldBeNil)
212214
})
215+
216+
Convey("Test message with Role and Parts", func() {
217+
url := "http://example.com/image.png"
218+
parts := []*ContentPart{
219+
{
220+
Type: "text",
221+
Text: util.Ptr("Hello"),
222+
},
223+
{
224+
Type: "image",
225+
ImageURL: &url,
226+
},
227+
}
228+
msg := &Message{
229+
Role: "user",
230+
Parts: parts,
231+
}
232+
copied := msg.DeepCopy()
233+
So(copied, ShouldNotBeNil)
234+
So(copied.Role, ShouldEqual, msg.Role)
235+
So(copied.Content, ShouldBeNil)
236+
So(copied.Parts, ShouldNotBeNil)
237+
So(len(copied.Parts), ShouldEqual, len(msg.Parts))
238+
for i, part := range copied.Parts {
239+
So(part.Type, ShouldEqual, msg.Parts[i].Type)
240+
if part.Text != nil {
241+
So(*part.Text, ShouldEqual, *msg.Parts[i].Text)
242+
}
243+
if part.ImageURL != nil {
244+
So(part.ImageURL, ShouldEqual, msg.Parts[i].ImageURL)
245+
}
246+
}
247+
})
248+
249+
Convey("Test message with Role, Content, and Parts", func() {
250+
content := "Hello, World!"
251+
url := "http://example.com/image.png"
252+
parts := []*ContentPart{
253+
{
254+
Type: "text",
255+
Text: util.Ptr("Hello"),
256+
},
257+
{
258+
Type: "image",
259+
ImageURL: &url,
260+
},
261+
}
262+
msg := &Message{
263+
Role: "user",
264+
Content: &content,
265+
Parts: parts,
266+
}
267+
copied := msg.DeepCopy()
268+
So(copied, ShouldNotBeNil)
269+
So(copied.Role, ShouldEqual, msg.Role)
270+
So(copied.Content, ShouldNotBeNil)
271+
So(*copied.Content, ShouldEqual, *msg.Content)
272+
So(copied.Parts, ShouldNotBeNil)
273+
So(len(copied.Parts), ShouldEqual, len(msg.Parts))
274+
for i, part := range copied.Parts {
275+
So(part.Type, ShouldEqual, msg.Parts[i].Type)
276+
if part.Text != nil {
277+
So(*part.Text, ShouldEqual, *msg.Parts[i].Text)
278+
}
279+
if part.ImageURL != nil {
280+
So(part.ImageURL, ShouldEqual, msg.Parts[i].ImageURL)
281+
}
282+
}
283+
})
213284
})
214285
}
215286

0 commit comments

Comments
 (0)