mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-31 04:35:16 +01:00
🎨 AI supports configuration randomness and context number https://github.com/siyuan-note/siyuan/issues/10660
This commit is contained in:
parent
97e3ff656d
commit
a7e1e3abac
12 changed files with 66 additions and 16 deletions
|
|
@ -194,6 +194,10 @@ func setAI(c *gin.Context) {
|
|||
ai.OpenAI.APIMaxTokens = 0
|
||||
}
|
||||
|
||||
if 0 >= ai.OpenAI.APITemperature || 2 < ai.OpenAI.APITemperature {
|
||||
ai.OpenAI.APITemperature = 1.0
|
||||
}
|
||||
|
||||
model.Conf.AI = ai
|
||||
model.Conf.Save()
|
||||
|
||||
|
|
|
|||
|
|
@ -29,15 +29,16 @@ type AI struct {
|
|||
}
|
||||
|
||||
type OpenAI struct {
|
||||
APIKey string `json:"apiKey"`
|
||||
APITimeout int `json:"apiTimeout"`
|
||||
APIProxy string `json:"apiProxy"`
|
||||
APIModel string `json:"apiModel"`
|
||||
APIMaxTokens int `json:"apiMaxTokens"`
|
||||
APIBaseURL string `json:"apiBaseURL"`
|
||||
APIUserAgent string `json:"apiUserAgent"`
|
||||
APIProvider string `json:"apiProvider"` // OpenAI, Azure
|
||||
APIVersion string `json:"apiVersion"` // Azure API version
|
||||
APIKey string `json:"apiKey"`
|
||||
APITimeout int `json:"apiTimeout"`
|
||||
APIProxy string `json:"apiProxy"`
|
||||
APIModel string `json:"apiModel"`
|
||||
APIMaxTokens int `json:"apiMaxTokens"`
|
||||
APITemperature float64 `json:"apiTemperature"`
|
||||
APIBaseURL string `json:"apiBaseURL"`
|
||||
APIUserAgent string `json:"apiUserAgent"`
|
||||
APIProvider string `json:"apiProvider"` // OpenAI, Azure
|
||||
APIVersion string `json:"apiVersion"` // Azure API version
|
||||
}
|
||||
|
||||
func NewAI() *AI {
|
||||
|
|
@ -69,6 +70,13 @@ func NewAI() *AI {
|
|||
}
|
||||
}
|
||||
|
||||
if temperature := os.Getenv("SIYUAN_OPENAI_API_TEMPERATURE"); "" != temperature {
|
||||
temperatureFloat, err := strconv.ParseFloat(temperature, 64)
|
||||
if nil == err {
|
||||
openAI.APITemperature = temperatureFloat
|
||||
}
|
||||
}
|
||||
|
||||
if baseURL := os.Getenv("SIYUAN_OPENAI_API_BASE_URL"); "" != baseURL {
|
||||
openAI.APIBaseURL = baseURL
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,7 +170,7 @@ type OpenAIGPT struct {
|
|||
}
|
||||
|
||||
func (gpt *OpenAIGPT) chat(msg string, contextMsgs []string) (partRet string, stop bool, err error) {
|
||||
return util.ChatGPT(msg, contextMsgs, gpt.c, Conf.AI.OpenAI.APIModel, Conf.AI.OpenAI.APIMaxTokens, Conf.AI.OpenAI.APITimeout)
|
||||
return util.ChatGPT(msg, contextMsgs, gpt.c, Conf.AI.OpenAI.APIModel, Conf.AI.OpenAI.APIMaxTokens, Conf.AI.OpenAI.APITemperature, Conf.AI.OpenAI.APITimeout)
|
||||
}
|
||||
|
||||
type CloudGPT struct {
|
||||
|
|
|
|||
|
|
@ -408,9 +408,18 @@ func InitConf() {
|
|||
if "" == Conf.AI.OpenAI.APIUserAgent {
|
||||
Conf.AI.OpenAI.APIUserAgent = util.UserAgent
|
||||
}
|
||||
if strings.HasPrefix(Conf.AI.OpenAI.APIUserAgent, "SiYuan/") {
|
||||
Conf.AI.OpenAI.APIUserAgent = util.UserAgent
|
||||
}
|
||||
if "" == Conf.AI.OpenAI.APIProvider {
|
||||
Conf.AI.OpenAI.APIProvider = "OpenAI"
|
||||
}
|
||||
if 0 > Conf.AI.OpenAI.APIMaxTokens {
|
||||
Conf.AI.OpenAI.APIMaxTokens = 0
|
||||
}
|
||||
if 0 >= Conf.AI.OpenAI.APITemperature || 2 < Conf.AI.OpenAI.APITemperature {
|
||||
Conf.AI.OpenAI.APITemperature = 1.0
|
||||
}
|
||||
|
||||
if "" != Conf.AI.OpenAI.APIKey {
|
||||
logging.LogInfof("OpenAI API enabled\n"+
|
||||
|
|
@ -419,13 +428,15 @@ func InitConf() {
|
|||
" timeout=%ds\n"+
|
||||
" proxy=%s\n"+
|
||||
" model=%s\n"+
|
||||
" maxTokens=%d",
|
||||
" maxTokens=%d\n"+
|
||||
" temperature=%.1f",
|
||||
Conf.AI.OpenAI.APIUserAgent,
|
||||
Conf.AI.OpenAI.APIBaseURL,
|
||||
Conf.AI.OpenAI.APITimeout,
|
||||
Conf.AI.OpenAI.APIProxy,
|
||||
Conf.AI.OpenAI.APIModel,
|
||||
Conf.AI.OpenAI.APIMaxTokens)
|
||||
Conf.AI.OpenAI.APIMaxTokens,
|
||||
Conf.AI.OpenAI.APITemperature)
|
||||
}
|
||||
|
||||
Conf.ReadOnly = util.ReadOnly
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import (
|
|||
"github.com/siyuan-note/logging"
|
||||
)
|
||||
|
||||
func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, maxTokens, timeout int) (ret string, stop bool, err error) {
|
||||
func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, maxTokens int, temperature float64, timeout int) (ret string, stop bool, err error) {
|
||||
var reqMsgs []openai.ChatCompletionMessage
|
||||
|
||||
for _, ctxMsg := range contextMsgs {
|
||||
|
|
@ -42,9 +42,10 @@ func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, m
|
|||
})
|
||||
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: model,
|
||||
MaxTokens: maxTokens,
|
||||
Messages: reqMsgs,
|
||||
Model: model,
|
||||
MaxTokens: maxTokens,
|
||||
Temperature: float32(temperature),
|
||||
Messages: reqMsgs,
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||
defer cancel()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue