From 3a1c38901c5acf01b2ff4152ce0096baff0c0f24 Mon Sep 17 00:00:00 2001 From: Travis Cline Date: Wed, 24 Jan 2024 17:56:50 -0800 Subject: [PATCH 1/2] llms: Add convenience helper for text parts --- llms/helpers.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 llms/helpers.go diff --git a/llms/helpers.go b/llms/helpers.go new file mode 100644 index 000000000..19f4e90cd --- /dev/null +++ b/llms/helpers.go @@ -0,0 +1,18 @@ +package llms + +import "github.com/tmc/langchaingo/schema" + +// 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 +} From 7e6af81932185323fc9b18c377a731247e5fc85f Mon Sep 17 00:00:00 2001 From: Travis Cline Date: Thu, 25 Jan 2024 18:57:20 -0800 Subject: [PATCH 2/2] llms: Cleanup TextParts and test it --- llms/generatecontent.go | 15 ++++++++++++++ llms/generatecontent_test.go | 39 ++++++++++++++++++++++++++++++++++++ llms/helpers.go | 18 ----------------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 llms/generatecontent_test.go delete mode 100644 llms/helpers.go 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) + } + }) + } +} diff --git a/llms/helpers.go b/llms/helpers.go deleted file mode 100644 index 19f4e90cd..000000000 --- a/llms/helpers.go +++ /dev/null @@ -1,18 +0,0 @@ -package llms - -import "github.com/tmc/langchaingo/schema" - -// 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 -}