Feat: add character card support
This commit is contained in:
41
models/card.go
Normal file
41
models/card.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package models
|
||||
|
||||
import "strings"
|
||||
|
||||
// https://github.com/malfoyslastname/character-card-spec-v2/blob/main/spec_v2.md
|
||||
// what a bloat; trim to Role->Msg pair and first msg
|
||||
type CharCardSpec struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Personality string `json:"personality"`
|
||||
FirstMes string `json:"first_mes"`
|
||||
Avatar string `json:"avatar"`
|
||||
Chat string `json:"chat"`
|
||||
MesExample string `json:"mes_example"`
|
||||
Scenario string `json:"scenario"`
|
||||
CreateDate string `json:"create_date"`
|
||||
Talkativeness string `json:"talkativeness"`
|
||||
Fav bool `json:"fav"`
|
||||
Creatorcomment string `json:"creatorcomment"`
|
||||
Spec string `json:"spec"`
|
||||
SpecVersion string `json:"spec_version"`
|
||||
Tags []any `json:"tags"`
|
||||
}
|
||||
|
||||
func (c *CharCardSpec) Simplify(userName, fpath string) *CharCard {
|
||||
fm := strings.ReplaceAll(strings.ReplaceAll(c.FirstMes, "{{char}}", c.Name), "{{user}}", userName)
|
||||
sysPr := strings.ReplaceAll(strings.ReplaceAll(c.Description, "{{char}}", c.Name), "{{user}}", userName)
|
||||
return &CharCard{
|
||||
SysPrompt: sysPr,
|
||||
FirstMsg: fm,
|
||||
Role: c.Name,
|
||||
FilePath: fpath,
|
||||
}
|
||||
}
|
||||
|
||||
type CharCard struct {
|
||||
SysPrompt string
|
||||
FirstMsg string
|
||||
Role string
|
||||
FilePath string
|
||||
}
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
type Chat struct {
|
||||
ID uint32 `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Msgs string `db:"msgs" json:"msgs"` // []MessagesStory to string json
|
||||
Msgs string `db:"msgs" json:"msgs"` // []RoleMsg to string json
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (c Chat) ToHistory() ([]MessagesStory, error) {
|
||||
resp := []MessagesStory{}
|
||||
func (c Chat) ToHistory() ([]RoleMsg, error) {
|
||||
resp := []RoleMsg{}
|
||||
if err := json.Unmarshal([]byte(c.Msgs), &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -5,12 +5,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// type FuncCall struct {
|
||||
// XMLName xml.Name `xml:"tool_call"`
|
||||
// Name string `xml:"name"`
|
||||
// Args []string `xml:"args"`
|
||||
// }
|
||||
|
||||
type FuncCall struct {
|
||||
Name string `json:"name"`
|
||||
Args []string `json:"args"`
|
||||
@@ -56,12 +50,12 @@ type LLMRespChunk struct {
|
||||
} `json:"usage"`
|
||||
}
|
||||
|
||||
type MessagesStory struct {
|
||||
type RoleMsg struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func (m MessagesStory) ToText(i int) string {
|
||||
func (m RoleMsg) ToText(i int) string {
|
||||
icon := ""
|
||||
switch m.Role {
|
||||
case "assistant":
|
||||
@@ -72,20 +66,22 @@ func (m MessagesStory) ToText(i int) string {
|
||||
icon = fmt.Sprintf("(%d) <system>: ", i)
|
||||
case "tool":
|
||||
icon = fmt.Sprintf("(%d) <tool>: ", i)
|
||||
default:
|
||||
icon = fmt.Sprintf("(%d) <%s>: ", i, m.Role)
|
||||
}
|
||||
textMsg := fmt.Sprintf("%s%s\n", icon, m.Content)
|
||||
return strings.ReplaceAll(textMsg, "\n\n", "\n")
|
||||
}
|
||||
|
||||
type ChatBody struct {
|
||||
Model string `json:"model"`
|
||||
Stream bool `json:"stream"`
|
||||
Messages []MessagesStory `json:"messages"`
|
||||
Model string `json:"model"`
|
||||
Stream bool `json:"stream"`
|
||||
Messages []RoleMsg `json:"messages"`
|
||||
}
|
||||
|
||||
type ChatToolsBody struct {
|
||||
Model string `json:"model"`
|
||||
Messages []MessagesStory `json:"messages"`
|
||||
Model string `json:"model"`
|
||||
Messages []RoleMsg `json:"messages"`
|
||||
Tools []struct {
|
||||
Type string `json:"type"`
|
||||
Function struct {
|
||||
|
||||
Reference in New Issue
Block a user