Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor out message role function
  • Loading branch information
sgoedecke committed Jun 5, 2025
commit b2cb12076a739098e19f3824213a92c402d61f25
13 changes: 3 additions & 10 deletions cmd/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,9 @@ func (h *evalCommandHandler) templateMessages(testCase map[string]interface{}) (
return nil, fmt.Errorf("failed to template message content: %w", err)
}

var role azuremodels.ChatMessageRole
switch strings.ToLower(msg.Role) {
case "system":
role = azuremodels.ChatMessageRoleSystem
case "user":
role = azuremodels.ChatMessageRoleUser
case "assistant":
role = azuremodels.ChatMessageRoleAssistant
default:
return nil, fmt.Errorf("unknown message role: %s", msg.Role)
role, err := prompt.GetAzureChatMessageRole(msg.Role)
if err != nil {
return nil, err
}

messages = append(messages, azuremodels.ChatMessage{
Expand Down
13 changes: 9 additions & 4 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,17 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
return err
}

switch strings.ToLower(m.Role) {
case "system":
role, err := prompt.GetAzureChatMessageRole(m.Role)
if err != nil {
return err
}

switch role {
case azuremodels.ChatMessageRoleSystem:
conversation.systemPrompt = content
case "user":
case azuremodels.ChatMessageRoleUser:
conversation.AddMessage(azuremodels.ChatMessageRoleUser, content)
case "assistant":
case azuremodels.ChatMessageRoleAssistant:
conversation.AddMessage(azuremodels.ChatMessageRoleAssistant, content)
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ func TemplateString(templateStr string, data interface{}) (string, error) {
return result, nil
}

// GetAzureChatMessageRole converts a role string to azuremodels.ChatMessageRole
func GetAzureChatMessageRole(role string) (azuremodels.ChatMessageRole, error) {
switch strings.ToLower(role) {
case "system":
return azuremodels.ChatMessageRoleSystem, nil
case "user":
return azuremodels.ChatMessageRoleUser, nil
case "assistant":
return azuremodels.ChatMessageRoleAssistant, nil
default:
return "", fmt.Errorf("unknown message role: %s", role)
}
}

// BuildChatCompletionOptions creates a ChatCompletionOptions with the file's model and parameters
func (f *File) BuildChatCompletionOptions(messages []azuremodels.ChatMessage) azuremodels.ChatCompletionOptions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the new functions!

req := azuremodels.ChatCompletionOptions{
Expand Down
Loading