diff --git a/llms/generatecontent.go b/llms/generatecontent.go index 8d91c2003..f43756dbd 100644 --- a/llms/generatecontent.go +++ b/llms/generatecontent.go @@ -82,3 +82,18 @@ type ContentChoice struct { // FuncCall is non-nil when the model asks to invoke a function/tool. FuncCall *schema.FunctionCall } + +// TextParts is a helper function to create a MessageContent with a role and a +// list of text parts. +func TextParts(role schema.ChatMessageType, parts ...string) MessageContent { + result := MessageContent{ + Role: role, + Parts: []ContentPart{}, + } + for _, part := range parts { + result.Parts = append(result.Parts, TextContent{ + Text: part, + }) + } + return result +} diff --git a/llms/generatecontent_test.go b/llms/generatecontent_test.go new file mode 100644 index 000000000..611f50d2e --- /dev/null +++ b/llms/generatecontent_test.go @@ -0,0 +1,39 @@ +package llms + +import ( + "reflect" + "testing" + + "github.com/tmc/langchaingo/schema" +) + +func TestTextParts(t *testing.T) { + t.Parallel() + type args struct { + role schema.ChatMessageType + parts []string + } + tests := []struct { + name string + args args + want MessageContent + }{ + {"basics", args{schema.ChatMessageTypeHuman, []string{"a", "b", "c"}}, MessageContent{ + Role: schema.ChatMessageTypeHuman, + Parts: []ContentPart{ + TextContent{Text: "a"}, + TextContent{Text: "b"}, + TextContent{Text: "c"}, + }, + }}, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + if got := TextParts(tt.args.role, tt.args.parts...); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TextParts() = %v, want %v", got, tt.want) + } + }) + } +}