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
Prev Previous commit
Next Next commit
docs
pavelgj committed Jun 26, 2024
commit 776151708fcec13e93cc56b9ff9b2d3396f4b433
18 changes: 18 additions & 0 deletions go/ai/request_helpers.go
Original file line number Diff line number Diff line change
@@ -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,36 +28,49 @@ func NewUserTextGenerateRequest(text string) *GenerateRequest {
}
}

// NewGenerateRequest create a new GenerateRequest with provided config and
// messages.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
func NewGenerateRequest(config map[string]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
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 NewMessage(RoleUser, nil, NewTextPart(text))
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
}

// NewModelMessage creates a new Message with role "model" and provided parts.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
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))
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
}

// NewModelMessage creates a new Message with role "system" and provided parts.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
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))
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
}

// NewMessage creates a new Message with the provided role, metadata and parts.
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
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,