Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Go] simple request helpers #488

Merged
merged 16 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions genkit-tools/cli/config/main.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 0 additions & 10 deletions go/ai/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
},
}
}
79 changes: 79 additions & 0 deletions go/ai/request_helpers.go
Original file line number Diff line number Diff line change
@@ -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.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
// Use NewUserTextMessage if you have a text-only message.
func NewUserMessage(parts ...*Part) *Message {
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
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.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
// 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.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
// 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)},
}
}
Loading