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 new file mode 100644 index 0000000000..6252a455e7 --- /dev/null +++ b/go/ai/request_helpers.go @@ -0,0 +1,79 @@ +// 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 + +// NewGenerateRequest create a new GenerateRequest with provided config and +// messages. Use NewUserTextGenerateRequest if you have a simple text-only user message. +func NewGenerateRequest(config any, messages ...*Message) *GenerateRequest { + return &GenerateRequest{ + Config: config, + Messages: messages, + } +} + +// 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...) +} + +// 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 NewTextMessage(RoleUser, text) +} + +// 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...) +} + +// 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 NewTextMessage(RoleModel, text) +} + +// 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...) +} + +// 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 NewTextMessage(RoleSystem, text) +} + +// 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, + Content: parts, + Metadata: metadata, + } +} + +// 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, + Content: []*Part{NewTextPart(text)}, + } +}