summaryrefslogtreecommitdiff
path: root/models/card.go
blob: adfb0308667a8c80804a5e68ce8cb50f7679e328 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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"`
	Extentions     []byte `json:"extentions"`
}

type Spec2Wrapper struct {
	Data CharCardSpec `json:"data"`
}

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 `json:"sys_prompt"`
	FirstMsg  string `json:"first_msg"`
	Role      string `json:"role"`
	FilePath  string `json:"filepath"`
}

func (cc *CharCard) ToSpec(userName string) *CharCardSpec {
	descr := strings.ReplaceAll(strings.ReplaceAll(cc.SysPrompt, cc.Role, "{{char}}"), userName, "{{user}}")
	return &CharCardSpec{
		Name:        cc.Role,
		Description: descr,
		FirstMes:    cc.FirstMsg,
		Spec:        "chara_card_v2",
		SpecVersion: "2.0",
		Extentions:  []byte("{}"),
	}
}