Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: New settings UI https://github.com/siyuan-note/siyuan/issues/17601

Daniele97a72cba5b4
diff --git a/kernel/api/setting.go b/kernel/api/setting.go
index c98bbfb0f4..7ef388d7a9 100644
--- a/kernel/api/setting.go
+++ b/kernel/api/setting.go
@@ -233,6 +233,9 @@ func setAI(c *gin.Context) {
if nil == ai.Agent {
ai.Agent = model.Conf.AI.Agent
}
+ if nil == ai.Chat {
+ ai.Chat = model.Conf.AI.Chat
+ }
if len(ai.Scenarios) == 0 {
ai.Scenarios = model.Conf.AI.Scenarios
}
@@ -246,6 +249,20 @@ func setAI(c *gin.Context) {
}
}
model.Conf.AI = ai
+
+ // Chat is the runtime view of the chat-scenario model's behavior params; the
+ // settings page persists them on Model, so mirror them onto Chat after assign.
+ // https://github.com/siyuan-note/siyuan/issues/17797
+ if _, m := model.Conf.AI.GetScenarioModel(conf.ScenarioChat); nil != m {
+ if nil == model.Conf.AI.Chat {
+ model.Conf.AI.Chat = &conf.Chat{}
+ }
+ model.Conf.AI.Chat.MaxCompletionTokens = m.MaxTokens
+ model.Conf.AI.Chat.Temperature = m.Temperature
+ model.Conf.AI.Chat.MaxHistoryMessages = m.MaxContexts
+ model.Conf.AI.Chat.MaxContinueRounds = m.MaxContexts
+ }
+
model.Conf.AI.Normalize()
model.Conf.Save()
diff --git a/kernel/conf/ai.go b/kernel/conf/ai.go
index 58e81092d6..1234de956d 100644
--- a/kernel/conf/ai.go
+++ b/kernel/conf/ai.go
@@ -30,6 +30,7 @@ type AI struct {
MCP *MCP `json:"mcp"`
Embedding *Embedding `json:"embedding"`
Agent *Agent `json:"agent"`
+ Chat *Chat `json:"chat"`
Providers []*Provider `json:"providers"`
Scenarios []*Scenario `json:"scenarios"`
}
@@ -43,14 +44,23 @@ type Agent struct {
MaxToolCallRounds int `json:"maxToolCallRounds"`
}
+// Chat holds behavior parameters used by the chat scenario. They are kept
+// here (instead of on Model) to mirror Agent and to decouple scenario behavior
+// from the model registry. See https://github.com/siyuan-note/siyuan/issues/17797
+type Chat struct {
+ MaxHistoryMessages int `json:"maxHistoryMessages"` // Max number of prior turns kept as context
+ MaxContinueRounds int `json:"maxContinueRounds"` // Max continuation rounds when finish_reason=length
+ Temperature float64 `json:"temperature"` // Alignment with Agent.Temperature
+ MaxCompletionTokens int `json:"maxCompletionTokens"` // Alignment with Agent.MaxCompletionTokens
+}
+
type Embedding struct {
- ID string `json:"id"`
- DisplayName string `json:"displayName,omitempty"`
- Enabled bool `json:"enabled"`
- APIKey string `json:"apiKey"`
- BaseURL string `json:"baseURL"`
- Name string `json:"name"`
- Timeout int `json:"timeout"`
+ ID string `json:"id"`
+ Enabled bool `json:"enabled"`
+ APIKey string `json:"apiKey"`
+ BaseURL string `json:"baseURL"`
+ Name string `json:"name"`
+ Timeout int `json:"timeout"`
}
type Provider struct {
@@ -63,6 +73,9 @@ type Provider struct {
Models []*Model `json:"models"`
}
+// Model is the provider-scoped model registry entry. MaxTokens/Temperature/
+// MaxContexts remain the persisted UI-facing config (the settings page still
+// reads/writes them). Chat holds the runtime view derived from them.
type Model struct {
ID string `json:"id"`
DisplayName string `json:"displayName,omitempty"`
@@ -108,6 +121,12 @@ func NewAI() *AI {
MaxCompletionTokens: 4096,
MaxToolCallRounds: 64,
},
+ Chat: &Chat{
+ MaxHistoryMessages: 7,
+ MaxContinueRounds: 7,
+ Temperature: 1.0,
+ MaxCompletionTokens: 0,
+ },
}
apiKey := os.Getenv("SIYUAN_OPENAI_API_KEY")
@@ -133,19 +152,25 @@ func NewAI() *AI {
MaxContexts: 7,
Enabled: true,
}
+ // Behavior params persist on Model (UI-facing) and are mirrored onto Chat
+ // (runtime view). Env var names kept for backward compat.
if maxTokens := os.Getenv("SIYUAN_OPENAI_API_MAX_TOKENS"); "" != maxTokens {
if v, err := strconv.Atoi(maxTokens); err == nil {
model.MaxTokens = v
+ ai.Chat.MaxCompletionTokens = v
}
}
if temperature := os.Getenv("SIYUAN_OPENAI_API_TEMPERATURE"); "" != temperature {
if v, err := strconv.ParseFloat(temperature, 64); err == nil {
model.Temperature = v
+ ai.Chat.Temperature = v
}
}
if maxContexts := os.Getenv("SIYUAN_OPENAI_API_MAX_CONTEXTS"); "" != maxContexts {
if v, err := strconv.Atoi(maxContexts); err == nil {
model.MaxContexts = v
+ ai.Chat.MaxHistoryMessages = v
+ ai.Chat.MaxContinueRounds = v
}
}
@@ -492,13 +517,12 @@ func migrateModel(raw map[string]any) *Model {
func migrateEmbedding(raw map[string]any) *Embedding {
return &Embedding{
- ID: getString(raw, "id"),
- DisplayName: getString(raw, "name"),
- Enabled: getBool(raw, "enabled"),
- APIKey: getString(raw, "apiKey"),
- BaseURL: getString(raw, "apiBaseURL"),
- Name: getString(raw, "apiModel"),
- Timeout: getInt(raw, "apiTimeout"),
+ ID: getString(raw, "id"),
+ Enabled: getBool(raw, "enabled"),
+ APIKey: getString(raw, "apiKey"),
+ BaseURL: getString(raw, "apiBaseURL"),
+ Name: getString(raw, "apiModel"),
+ Timeout: getInt(raw, "apiTimeout"),
}
}
diff --git a/kernel/model/ai.go b/kernel/model/ai.go
index 6ce9e773c9..567bd3b649 100644
--- a/kernel/model/ai.go
+++ b/kernel/model/ai.go
@@ -92,8 +92,14 @@ func chatGPTContinueWrite(msg string, contextMsgs []string, cloud bool) (ret str
return
}
- if m.MaxContexts < len(contextMsgs) {
- contextMsgs = contextMsgs[len(contextMsgs)-m.MaxContexts:]
+ chat := Conf.AI.Chat
+ if nil == chat {
+ err = errors.New("no AI chat config")
+ return
+ }
+
+ if chat.MaxHistoryMessages < len(contextMsgs) {
+ contextMsgs = contextMsgs[len(contextMsgs)-chat.MaxHistoryMessages:]
}
var gpt GPT
@@ -101,14 +107,16 @@ func chatGPTContinueWrite(msg string, contextMsgs []string, cloud bool) (ret str
gpt = &CloudGPT{}
} else {
gpt = &OpenAIGPT{
- c: util.NewOpenAIClient(prov.APIKey, prov.BaseURL),
- m: m,
- timeout: prov.RequestTimeout,
+ c: util.NewOpenAIClient(prov.APIKey, prov.BaseURL),
+ m: m,
+ timeout: prov.RequestTimeout,
+ maxCompletionTokens: chat.MaxCompletionTokens,
+ temperature: chat.Temperature,
}
}
buf := &bytes.Buffer{}
- for i := 0; i < m.MaxContexts; i++ {
+ for i := 0; i < chat.MaxContinueRounds; i++ {
part, stop, chatErr := gpt.chat(msg, contextMsgs)
buf.WriteString(part)
@@ -180,13 +188,15 @@ type GPT interface {
}
type OpenAIGPT struct {
- c *openai.Client
- m *conf.Model
- timeout int
+ c *openai.Client
+ m *conf.Model
+ timeout int
+ maxCompletionTokens int
+ temperature float64
}
func (gpt *OpenAIGPT) chat(msg string, contextMsgs []string) (partRet string, stop bool, err error) {
- return util.ChatGPT(msg, contextMsgs, gpt.c, gpt.m.Name, gpt.m.MaxTokens, gpt.m.Temperature, gpt.timeout)
+ return util.ChatGPT(msg, contextMsgs, gpt.c, gpt.m.Name, gpt.maxCompletionTokens, gpt.temperature, gpt.timeout)
}
type CloudGPT struct {
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 1a61ffd8bf..0b54c1b15b 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -602,6 +602,14 @@ func InitConf() {
MaxToolCallRounds: 64,
}
}
+ if nil == Conf.AI.Chat {
+ Conf.AI.Chat = &conf.Chat{
+ MaxHistoryMessages: 7,
+ MaxContinueRounds: 7,
+ Temperature: 1.0,
+ MaxCompletionTokens: 0,
+ }
+ }
for _, p := range Conf.AI.Providers {
if nil == p {
continue
@@ -630,6 +638,28 @@ func InitConf() {
}
}
}
+ // Chat is the runtime view of the chat-scenario model's behavior params.
+ // The settings page still persists them on Model; mirror them onto Chat so
+ // the chat runtime reads from a single, Agent-symmetric place.
+ // https://github.com/siyuan-note/siyuan/issues/17797
+ if prov, m := Conf.AI.GetScenarioModel(conf.ScenarioChat); nil != prov && nil != m {
+ Conf.AI.Chat.MaxCompletionTokens = m.MaxTokens
+ Conf.AI.Chat.Temperature = m.Temperature
+ Conf.AI.Chat.MaxHistoryMessages = m.MaxContexts
+ Conf.AI.Chat.MaxContinueRounds = m.MaxContexts
+ }
+ if 0 > Conf.AI.Chat.MaxCompletionTokens {
+ Conf.AI.Chat.MaxCompletionTokens = 0
+ }
+ if 0 >= Conf.AI.Chat.Temperature || 2 < Conf.AI.Chat.Temperature {
+ Conf.AI.Chat.Temperature = 1.0
+ }
+ if 1 > Conf.AI.Chat.MaxHistoryMessages || 64 < Conf.AI.Chat.MaxHistoryMessages {
+ Conf.AI.Chat.MaxHistoryMessages = 7
+ }
+ if 1 > Conf.AI.Chat.MaxContinueRounds || 64 < Conf.AI.Chat.MaxContinueRounds {
+ Conf.AI.Chat.MaxContinueRounds = 7
+ }
for _, p := range Conf.AI.Providers {
if p == nil || len(p.APIKey) == 0 {
@@ -643,15 +673,17 @@ func InitConf() {
" baseURL=%s\n"+
" timeout=%ds\n"+
" model=%s\n"+
- " maxTokens=%d\n"+
+ " maxCompletionTokens=%d\n"+
" temperature=%.1f\n"+
- " maxContexts=%d",
+ " maxHistoryMessages=%d\n"+
+ " maxContinueRounds=%d",
p.BaseURL,
p.RequestTimeout,
m.Name,
- m.MaxTokens,
- m.Temperature,
- m.MaxContexts)
+ Conf.AI.Chat.MaxCompletionTokens,
+ Conf.AI.Chat.Temperature,
+ Conf.AI.Chat.MaxHistoryMessages,
+ Conf.AI.Chat.MaxContinueRounds)
}
}