-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversation.go
35 lines (27 loc) · 1.05 KB
/
conversation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package anthrogo
import (
"fmt"
)
// Conversation is a structure holding all messages of the conversation.
type Conversation struct {
Messages []CompletionMessage
}
// NewConversation creates and returns a new Conversation.
func NewConversation() *Conversation {
return &Conversation{}
}
// AddMessage appends a new message to the conversation. Role indicates if the message is from the "Assistant" or the "User".
func (c *Conversation) AddMessage(role Role, content string) {
c.Messages = append(c.Messages, CompletionMessage{Role: role, Content: content})
}
// GeneratePrompt formats the conversation into a string which can be used as a prompt for the assistant.
// The prompt ends with an empty Assistant message, indicating where the assistant's next message should go.
func (c *Conversation) GeneratePrompt() string {
prompt := ""
for _, msg := range c.Messages {
prompt += fmt.Sprintf("\n\n%s: %s", msg.Role, msg.Content)
}
// Always ends with an empty Assistant message for the next completion
prompt += fmt.Sprintf("\n\nAssistant:")
return prompt
}