InsertSystem inserts a system message after the existing system block and before the first non-system message.
(prompt []fantasy.Message, instruction string)
| 271 | // InsertSystem inserts a system message after the existing system |
| 272 | // block and before the first non-system message. |
| 273 | func InsertSystem(prompt []fantasy.Message, instruction string) []fantasy.Message { |
| 274 | instruction = strings.TrimSpace(instruction) |
| 275 | if instruction == "" { |
| 276 | return prompt |
| 277 | } |
| 278 | |
| 279 | systemMessage := fantasy.Message{ |
| 280 | Role: fantasy.MessageRoleSystem, |
| 281 | Content: []fantasy.MessagePart{ |
| 282 | fantasy.TextPart{Text: instruction}, |
| 283 | }, |
| 284 | } |
| 285 | |
| 286 | out := make([]fantasy.Message, 0, len(prompt)+1) |
| 287 | inserted := false |
| 288 | for _, message := range prompt { |
| 289 | if !inserted && message.Role != fantasy.MessageRoleSystem { |
| 290 | out = append(out, systemMessage) |
| 291 | inserted = true |
| 292 | } |
| 293 | out = append(out, message) |
| 294 | } |
| 295 | if !inserted { |
| 296 | out = append(out, systemMessage) |
| 297 | } |
| 298 | return out |
| 299 | } |
| 300 | |
| 301 | // AppendUser appends an instruction as a user message at the end of |
| 302 | // the prompt. |
no outgoing calls