From 297b9cfdc420c9ef0ac00765c5e428433a5f682b Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 11:56:59 -0400 Subject: [PATCH 01/14] feat: [Go] PROPOSAL: simple request builder --- go/ai/request_builder.go | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 go/ai/request_builder.go diff --git a/go/ai/request_builder.go b/go/ai/request_builder.go new file mode 100644 index 0000000000..bb01713bde --- /dev/null +++ b/go/ai/request_builder.go @@ -0,0 +1,57 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ai + +func NewGenerateRequest() *GenerateRequest { + return &GenerateRequest{} +} + +func NewUserTextGenerateRequest(text string) *GenerateRequest { + return NewGenerateRequest().AddUserTextMessage(text) +} + +func (req *GenerateRequest) AddMessage(msg *Message) *GenerateRequest { + req.Messages = append(req.Messages, msg) + return req +} + +func (req *GenerateRequest) AddUserTextMessage(text string) *GenerateRequest { + req.AddMessage(NewMessage(RoleUser).AddPart(NewTextPart(text))) + return req +} + +func NewMessage(role Role) *Message { + return &Message{ + Role: role, + } +} + +func NewUserMessage() *Message { + return &Message{ + Role: RoleUser, + } +} + +func NewTextMessage(role Role, text string) *Message { + return &Message{ + Role: role, + Content: []*Part{NewTextPart(text)}, + } +} + +func (msg *Message) AddPart(part *Part) *Message { + msg.Content = append(msg.Content, part) + return msg +} From a6980b06e7584d34b7a9e27e2a6284c92ee17c56 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 12:17:59 -0400 Subject: [PATCH 02/14] more setters --- go/ai/request_builder.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/go/ai/request_builder.go b/go/ai/request_builder.go index bb01713bde..7d0f7c7017 100644 --- a/go/ai/request_builder.go +++ b/go/ai/request_builder.go @@ -27,6 +27,26 @@ func (req *GenerateRequest) AddMessage(msg *Message) *GenerateRequest { return req } +func (req *GenerateRequest) SetConfig(config any) *GenerateRequest { + req.Config = config + return req +} + +func (req *GenerateRequest) SetContext(context []any) *GenerateRequest { + req.Context = context + return req +} + +func (req *GenerateRequest) SetTools(tools []*ToolDefinition) *GenerateRequest { + req.Tools = tools + return req +} + +func (req *GenerateRequest) SetOutput(output *GenerateRequestOutput) *GenerateRequest { + req.Output = output + return req +} + func (req *GenerateRequest) AddUserTextMessage(text string) *GenerateRequest { req.AddMessage(NewMessage(RoleUser).AddPart(NewTextPart(text))) return req @@ -38,12 +58,6 @@ func NewMessage(role Role) *Message { } } -func NewUserMessage() *Message { - return &Message{ - Role: RoleUser, - } -} - func NewTextMessage(role Role, text string) *Message { return &Message{ Role: role, From c71e0a22d4e1d7355560c371f5d6aa3db5d81416 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 12:59:49 -0400 Subject: [PATCH 03/14] feedback --- go/ai/request_builder.go | 71 ---------------------------------------- go/ai/request_helpers.go | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 go/ai/request_builder.go create mode 100644 go/ai/request_helpers.go diff --git a/go/ai/request_builder.go b/go/ai/request_builder.go deleted file mode 100644 index 7d0f7c7017..0000000000 --- a/go/ai/request_builder.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2024 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package ai - -func NewGenerateRequest() *GenerateRequest { - return &GenerateRequest{} -} - -func NewUserTextGenerateRequest(text string) *GenerateRequest { - return NewGenerateRequest().AddUserTextMessage(text) -} - -func (req *GenerateRequest) AddMessage(msg *Message) *GenerateRequest { - req.Messages = append(req.Messages, msg) - return req -} - -func (req *GenerateRequest) SetConfig(config any) *GenerateRequest { - req.Config = config - return req -} - -func (req *GenerateRequest) SetContext(context []any) *GenerateRequest { - req.Context = context - return req -} - -func (req *GenerateRequest) SetTools(tools []*ToolDefinition) *GenerateRequest { - req.Tools = tools - return req -} - -func (req *GenerateRequest) SetOutput(output *GenerateRequestOutput) *GenerateRequest { - req.Output = output - return req -} - -func (req *GenerateRequest) AddUserTextMessage(text string) *GenerateRequest { - req.AddMessage(NewMessage(RoleUser).AddPart(NewTextPart(text))) - return req -} - -func NewMessage(role Role) *Message { - return &Message{ - Role: role, - } -} - -func NewTextMessage(role Role, text string) *Message { - return &Message{ - Role: role, - Content: []*Part{NewTextPart(text)}, - } -} - -func (msg *Message) AddPart(part *Part) *Message { - msg.Content = append(msg.Content, part) - return msg -} diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go new file mode 100644 index 0000000000..dd562ce05e --- /dev/null +++ b/go/ai/request_helpers.go @@ -0,0 +1,71 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ai + +func NewUserTextGenerateRequest(text string) *GenerateRequest { + return &GenerateRequest{ + Messages: []*Message{ + { + Role: RoleUser, + Content: []*Part{NewTextPart(text)}, + }, + }, + } +} + +func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRequest { + return &GenerateRequest{ + Config: config, + Messages: messages, + } +} + +func NewUserMessage(parts ...*Part) *Message { + return NewMessage(RoleUser, nil, parts...) +} +func NewUserTextMessage(text string) *Message { + return NewMessage(RoleUser, nil, NewTextPart(text)) +} + +func NewModelMessage(parts ...*Part) *Message { + return NewMessage(RoleModel, nil, parts...) +} + +func NewModelTextMessage(text string) *Message { + return NewMessage(RoleModel, nil, NewTextPart(text)) +} + +func NewSystemMessage(parts ...*Part) *Message { + return NewMessage(RoleSystem, nil, parts...) +} + +func NewSystemTextMessage(text string) *Message { + return NewMessage(RoleSystem, nil, NewTextPart(text)) +} + +func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message { + return &Message{ + Role: role, + Content: parts, + Metadata: metadata, + } +} + +func NewTextMessage(role Role, text string) *Message { + return &Message{ + Role: role, + Content: []*Part{NewTextPart(text)}, + } +} From 776151708fcec13e93cc56b9ff9b2d3396f4b433 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:11:11 -0400 Subject: [PATCH 04/14] docs --- go/ai/request_helpers.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index dd562ce05e..d244ab8af1 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -14,6 +14,9 @@ package ai +// NewUserTextGenerateRequest creates a new GenerateRequest with a message with +// role set to "user" and context to single text part with the content of +// provided text. func NewUserTextGenerateRequest(text string) *GenerateRequest { return &GenerateRequest{ Messages: []*Message{ @@ -25,6 +28,8 @@ func NewUserTextGenerateRequest(text string) *GenerateRequest { } } +// NewGenerateRequest create a new GenerateRequest with provided config and +// messages. func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRequest { return &GenerateRequest{ Config: config, @@ -32,29 +37,40 @@ func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRe } } +// NewUserMessage creates a new Message with role "user" and provided parts. func NewUserMessage(parts ...*Part) *Message { return NewMessage(RoleUser, nil, parts...) } + +// NewUserTextMessage creates a new Message with role "user" and content with +// a single text part with the content of provided text. func NewUserTextMessage(text string) *Message { return NewMessage(RoleUser, nil, NewTextPart(text)) } +// NewModelMessage creates a new Message with role "model" and provided parts. func NewModelMessage(parts ...*Part) *Message { return NewMessage(RoleModel, nil, parts...) } +// NewUserTextMessage creates a new Message with role "model" and content with +// a single text part with the content of provided text. func NewModelTextMessage(text string) *Message { return NewMessage(RoleModel, nil, NewTextPart(text)) } +// NewModelMessage creates a new Message with role "system" and provided parts. func NewSystemMessage(parts ...*Part) *Message { return NewMessage(RoleSystem, nil, parts...) } +// NewUserTextMessage creates a new Message with role "system" and content with +// a single text part with the content of provided text. func NewSystemTextMessage(text string) *Message { return NewMessage(RoleSystem, nil, NewTextPart(text)) } +// NewMessage creates a new Message with the provided role, metadata and parts. func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message { return &Message{ Role: role, @@ -63,6 +79,8 @@ func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message { } } +// NewTextMessage creates a new Message with the provided role and content with +// a single part containint provided text. func NewTextMessage(role Role, text string) *Message { return &Message{ Role: role, From 7928309d6fe78d565a9a23bb11f44eed068d1a44 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:21:42 -0400 Subject: [PATCH 05/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index d244ab8af1..8d5464fb5f 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -45,7 +45,7 @@ func NewUserMessage(parts ...*Part) *Message { // NewUserTextMessage creates a new Message with role "user" and content with // a single text part with the content of provided text. func NewUserTextMessage(text string) *Message { - return NewMessage(RoleUser, nil, NewTextPart(text)) + return NewTextMessage(RoleUser, text) } // NewModelMessage creates a new Message with role "model" and provided parts. From e8242b0a4d29eb176d47a4169893396b70874b93 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:21:50 -0400 Subject: [PATCH 06/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index 8d5464fb5f..509fa5fcd0 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -56,7 +56,7 @@ func NewModelMessage(parts ...*Part) *Message { // NewUserTextMessage creates a new Message with role "model" and content with // a single text part with the content of provided text. func NewModelTextMessage(text string) *Message { - return NewMessage(RoleModel, nil, NewTextPart(text)) + return NewTextMessage(RoleModel, text) } // NewModelMessage creates a new Message with role "system" and provided parts. From e2058456b1dff5d1d5d7decba3eced320fcfb25e Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:21:58 -0400 Subject: [PATCH 07/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index 509fa5fcd0..a5c337aa2a 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -67,7 +67,7 @@ func NewSystemMessage(parts ...*Part) *Message { // NewUserTextMessage creates a new Message with role "system" and content with // a single text part with the content of provided text. func NewSystemTextMessage(text string) *Message { - return NewMessage(RoleSystem, nil, NewTextPart(text)) + return NewTextMessage(RoleSystem, text) } // NewMessage creates a new Message with the provided role, metadata and parts. From 0c4927e04a3733afb29715be6f32fd90769f2eed Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:24:30 -0400 Subject: [PATCH 08/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index a5c337aa2a..94303a0a63 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -29,7 +29,7 @@ func NewUserTextGenerateRequest(text string) *GenerateRequest { } // NewGenerateRequest create a new GenerateRequest with provided config and -// messages. +// messages. Use NewUserTextGenerateRequest if you have a simple text-only user message. func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRequest { return &GenerateRequest{ Config: config, From 9ef75e405a373d631cdd9132ed59c1fd49a8b0d3 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:24:42 -0400 Subject: [PATCH 09/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index 94303a0a63..65edc860ed 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -49,6 +49,7 @@ func NewUserTextMessage(text string) *Message { } // NewModelMessage creates a new Message with role "model" and provided parts. +// Use NewModelTextMessage if you have a text-only message. func NewModelMessage(parts ...*Part) *Message { return NewMessage(RoleModel, nil, parts...) } From 6baebe6f87025929713ce24adf6a38bd4669961c Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:25:00 -0400 Subject: [PATCH 10/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index 65edc860ed..0b52236e55 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -60,7 +60,8 @@ func NewModelTextMessage(text string) *Message { return NewTextMessage(RoleModel, text) } -// NewModelMessage creates a new Message with role "system" and provided parts. +// NewSystemMessage creates a new Message with role "system" and provided parts. +// Use NewSystemTextMessage if you have a text-only message. func NewSystemMessage(parts ...*Part) *Message { return NewMessage(RoleSystem, nil, parts...) } From 4b61083073c9861c33781aa2a3fe6abf718054b5 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:25:06 -0400 Subject: [PATCH 11/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index 0b52236e55..c0455f3264 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -73,6 +73,7 @@ func NewSystemTextMessage(text string) *Message { } // NewMessage creates a new Message with the provided role, metadata and parts. +// Use NewTextMessage if you have a text-only message. func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message { return &Message{ Role: role, From c7ee1dc6a1f16457a8d9c3aad1283a40420dd298 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:25:14 -0400 Subject: [PATCH 12/14] Update go/ai/request_helpers.go Co-authored-by: Alex Pascal --- go/ai/request_helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index c0455f3264..80e912679e 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -38,6 +38,7 @@ func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRe } // NewUserMessage creates a new Message with role "user" and provided parts. +// Use NewUserTextMessage if you have a text-only message. func NewUserMessage(parts ...*Part) *Message { return NewMessage(RoleUser, nil, parts...) } From 59fcce7f720df19cc8312109d3064832947f085e Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:41:24 -0400 Subject: [PATCH 13/14] remove NewUserTextGenerateRequest --- go/ai/request_helpers.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index a5c337aa2a..757588cf69 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -14,20 +14,6 @@ package ai -// NewUserTextGenerateRequest creates a new GenerateRequest with a message with -// role set to "user" and context to single text part with the content of -// provided text. -func NewUserTextGenerateRequest(text string) *GenerateRequest { - return &GenerateRequest{ - Messages: []*Message{ - { - Role: RoleUser, - Content: []*Part{NewTextPart(text)}, - }, - }, - } -} - // NewGenerateRequest create a new GenerateRequest with provided config and // messages. func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRequest { From e7b7f8e98f957b06e0797d5c235134f69b090828 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Wed, 26 Jun 2024 13:50:17 -0400 Subject: [PATCH 14/14] removed NewTextMessages, updated init template --- genkit-tools/cli/config/main.go.template | 11 +++++------ go/ai/gen.go | 10 ---------- go/ai/request_helpers.go | 2 +- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/genkit-tools/cli/config/main.go.template b/genkit-tools/cli/config/main.go.template index dde69c7ed9..2d2adb06f9 100644 --- a/genkit-tools/cli/config/main.go.template +++ b/genkit-tools/cli/config/main.go.template @@ -22,12 +22,11 @@ func $GENKIT_FUNC_NAME() { if m == nil { return "", errors.New("menuSuggestionFlow: failed to find model") } - resp, err := m.Generate(ctx, &ai.GenerateRequest{ - Messages: ai.NewTextMessages(ai.RoleUser, fmt.Sprintf(`Suggest an item for the menu of a %s themed restaurant`, input)), - Config: &ai.GenerationCommonConfig{ - Temperature: 1, - }, - }, nil) + resp, err := m.Generate(ctx, + ai.NewGenerateRequest( + &ai.GenerationCommonConfig{Temperature: 1}, + ai.NewUserTextMessage(fmt.Sprintf(`Suggest an item for the menu of a %s themed restaurant`, input))), + nil) if err != nil { return "", err } diff --git a/go/ai/gen.go b/go/ai/gen.go index f6810fe681..c150622c72 100644 --- a/go/ai/gen.go +++ b/go/ai/gen.go @@ -203,13 +203,3 @@ type ToolResponse struct { // An example might be map[string]any{"name":"Thomas Jefferson", "born":1743}. Output map[string]any `json:"output,omitempty"` } - -// NewTextMessages is a helper function that creates a slice of [Message] from one role and string. -func NewTextMessages(r Role, m string) []*Message { - return []*Message{ - { - Role: r, - Content: []*Part{ NewTextPart(m) }, - }, - } -} \ No newline at end of file diff --git a/go/ai/request_helpers.go b/go/ai/request_helpers.go index a710742b46..6252a455e7 100644 --- a/go/ai/request_helpers.go +++ b/go/ai/request_helpers.go @@ -16,7 +16,7 @@ package ai // NewGenerateRequest create a new GenerateRequest with provided config and // messages. Use NewUserTextGenerateRequest if you have a simple text-only user message. -func NewGenerateRequest(config map[string]any, messages ...*Message) *GenerateRequest { +func NewGenerateRequest(config any, messages ...*Message) *GenerateRequest { return &GenerateRequest{ Config: config, Messages: messages,