Matt Low
0384c7cb66
This refactor splits out all conversation concerns into a new `conversation` package. There is now a split between `conversation` and `api`s representation of `Message`, the latter storing the minimum information required for interaction with LLM providers. There is necessary conversation between the two when making LLM calls.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package conversation
|
|
|
|
import (
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
|
)
|
|
|
|
// ApplySystemPrompt updates the contents of an existing system Message if it
|
|
// exists, or returns a new slice with the system Message prepended.
|
|
func ApplySystemPrompt(m []Message, system string, force bool) []Message {
|
|
if len(m) > 0 && m[0].Role == api.MessageRoleSystem {
|
|
if force {
|
|
m[0].Content = system
|
|
}
|
|
return m
|
|
} else {
|
|
return append([]Message{{
|
|
Role: api.MessageRoleSystem,
|
|
Content: system,
|
|
}}, m...)
|
|
}
|
|
}
|
|
|
|
func MessageToAPI(m Message) api.Message {
|
|
return api.Message{
|
|
Role: m.Role,
|
|
Content: m.Content,
|
|
ToolCalls: m.ToolCalls,
|
|
ToolResults: m.ToolResults,
|
|
}
|
|
}
|
|
|
|
func MessagesToAPI(messages []Message) []api.Message {
|
|
ret := make([]api.Message, 0, len(messages))
|
|
for _, m := range messages {
|
|
ret = append(ret, MessageToAPI(m))
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func MessageFromAPI(m api.Message) Message {
|
|
return Message{
|
|
Role: m.Role,
|
|
Content: m.Content,
|
|
ToolCalls: m.ToolCalls,
|
|
ToolResults: m.ToolResults,
|
|
}
|
|
}
|
|
|
|
func MessagesFromAPI(messages []api.Message) []Message {
|
|
ret := make([]Message, 0, len(messages))
|
|
for _, m := range messages {
|
|
ret = append(ret, MessageFromAPI(m))
|
|
}
|
|
return ret
|
|
}
|