diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json index c0d18b6de..dbce9d042 100644 --- a/app/appearance/langs/en_US.json +++ b/app/appearance/langs/en_US.json @@ -295,6 +295,8 @@ "apiModelTip": "The model parameter passed in when requesting the API is used to control the generated text style (the Deployment ID needs to be filled in when using the Azure OpenAI service)", "apiMaxTokens": "Maximum number of Tokens", "apiMaxTokensTip": "The max_tokens parameter passed in when requesting the API is used to control the length of the generated text", + "apiTemperature": "Temperature", + "apiTemperatureTip": "The temperature parameter passed in when requesting the API is used to control the randomness of the generated text", "apiBaseURL": "API Base URL", "apiBaseURLTip": "The base address of the request, such as https://api.openai.com/v1", "apiUserAgentTip": "The user agent that initiated the request, that is, the HTTP header User-Agent", diff --git a/app/appearance/langs/es_ES.json b/app/appearance/langs/es_ES.json index f04702e0d..6e785f2af 100644 --- a/app/appearance/langs/es_ES.json +++ b/app/appearance/langs/es_ES.json @@ -295,6 +295,8 @@ "apiModelTip": "El parámetro model pasado al solicitar la API se usa para controlar el estilo del texto generado (el ID de implementación debe completarse cuando se usa el servicio Azure OpenAI)", "apiMaxTokens": "Número máximo de tokens", "apiMaxTokensTip": "El parámetro max_tokens que se pasa al solicitar la API se usa para controlar la longitud del texto generado", + "apiTemperature": "Temperatura", + "apiTemperatureTip": "El parámetro temperature pasado al solicitar la API se utiliza para controlar la aleatoriedad del texto generado", "apiBaseURL": "URL base de la API", "apiBaseURLTip": "La dirección base de la solicitud, como https://api.openai.com/v1", "apiUserAgentTip": "El agente de usuario que inició la solicitud, es decir, el encabezado HTTP User-Agent", diff --git a/app/appearance/langs/fr_FR.json b/app/appearance/langs/fr_FR.json index b6240a898..c809674c0 100644 --- a/app/appearance/langs/fr_FR.json +++ b/app/appearance/langs/fr_FR.json @@ -295,6 +295,8 @@ "apiModelTip": "Le paramètre model transmis lors de la demande de l'API est utilisé pour contrôler le style de texte généré (l'ID de déploiement doit être renseigné lors de l'utilisation du service Azure OpenAI)", "apiMaxTokens": "Nombre maximum de jetons", "apiMaxTokensTip": "Le paramètre max_tokens transmis lors de la demande de l'API est utilisé pour contrôler la longueur du texte généré", + "apiTemperature": "Température", + "apiTemperatureTip": "Le paramètre temperature transmis lors de la requête à l'API est utilisé pour contrôler le caractère aléatoire du texte généré", "apiBaseURL": "URL de base de l'API", "apiBaseURLTip": "L'adresse de base de la requête, telle que https://api.openai.com/v1", "apiUserAgentTip": "L'agent utilisateur qui a initié la requête, c'est-à-dire l'en-tête HTTP User-Agent", diff --git a/app/appearance/langs/zh_CHT.json b/app/appearance/langs/zh_CHT.json index 20e8a3641..266615518 100644 --- a/app/appearance/langs/zh_CHT.json +++ b/app/appearance/langs/zh_CHT.json @@ -295,6 +295,8 @@ "apiModelTip": "請求 API 時傳入的 model 參數,用於控制產生的文字風格(使用 Azure OpenAI 服務時需填入 Deployment ID)", "apiMaxTokens": "最大 Token 數", "apiMaxTokensTip": "請求 API 時傳入的 max_tokens 參數,用於控制生成的文字長度", + "apiTemperature": "溫度", + "apiTemperatureTip": "請求 API 時傳入的 temperature 參數,用來控制產生的文字隨機性", "apiBaseURL": "API 基礎地址", "apiBaseURLTip": "發起請求的基礎地址,如 https://api.openai.com/v1", "apiUserAgentTip": "發起請求的使用者代理,即 HTTP 標頭 User-Agent", diff --git a/app/appearance/langs/zh_CN.json b/app/appearance/langs/zh_CN.json index 6dce05a66..b7967d706 100644 --- a/app/appearance/langs/zh_CN.json +++ b/app/appearance/langs/zh_CN.json @@ -295,6 +295,8 @@ "apiModelTip": "请求 API 时传入的 model 参数,用于控制生成的文本风格(使用 Azure OpenAI 服务时需填入 Deployment ID)", "apiMaxTokens": "最大 Token 数", "apiMaxTokensTip": "请求 API 时传入的 max_tokens 参数,用于控制生成的文本长度", + "apiTemperature": "温度", + "apiTemperatureTip": "请求 API 时传入的 temperature 参数,用于控制生成的文本随机性", "apiBaseURL": "API 基础地址", "apiBaseURLTip": "发起请求的基础地址,如 https://api.openai.com/v1", "apiVersion": "API 版本", diff --git a/app/src/config/ai.ts b/app/src/config/ai.ts index 2751d4f2f..8cc4a0f54 100644 --- a/app/src/config/ai.ts +++ b/app/src/config/ai.ts @@ -29,6 +29,12 @@ export const ai = {
${window.siyuan.languages.apiMaxTokensTip}
+
+ ${window.siyuan.languages.apiTemperature} +
+ +
${window.siyuan.languages.apiTemperatureTip}
+
${window.siyuan.languages.apiModel}
@@ -96,6 +102,14 @@ export const ai = {
+
+
+ ${window.siyuan.languages.apiTemperature} +
${window.siyuan.languages.apiTemperatureTip}
+
+ + +
${window.siyuan.languages.apiModel} @@ -176,6 +190,7 @@ export const ai = { apiKey: (ai.element.querySelector("#apiKey") as HTMLInputElement).value, apiModel: (ai.element.querySelector("#apiModel") as HTMLSelectElement).value, apiMaxTokens: parseInt((ai.element.querySelector("#apiMaxTokens") as HTMLInputElement).value), + apiTemperature: parseInt((ai.element.querySelector("#apiTemperature") as HTMLInputElement).value), apiProxy: (ai.element.querySelector("#apiProxy") as HTMLInputElement).value, apiTimeout: parseInt((ai.element.querySelector("#apiTimeout") as HTMLInputElement).value), apiProvider: (ai.element.querySelector("#apiProvider") as HTMLSelectElement).value, diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index d21eb9f20..0f059fc79 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -738,6 +738,7 @@ interface IConfig { apiKey: string apiModel: string apiMaxTokens: number + apiTemperature: number apiProxy: string apiTimeout: number }, diff --git a/kernel/api/setting.go b/kernel/api/setting.go index b3d85c858..754e814dc 100644 --- a/kernel/api/setting.go +++ b/kernel/api/setting.go @@ -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() diff --git a/kernel/conf/ai.go b/kernel/conf/ai.go index a21e2227a..2c154ef1d 100644 --- a/kernel/conf/ai.go +++ b/kernel/conf/ai.go @@ -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 } diff --git a/kernel/model/ai.go b/kernel/model/ai.go index 99652ca67..9a0c3e64a 100644 --- a/kernel/model/ai.go +++ b/kernel/model/ai.go @@ -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 { diff --git a/kernel/model/conf.go b/kernel/model/conf.go index a2144b0b4..329245a80 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -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 diff --git a/kernel/util/openai.go b/kernel/util/openai.go index 50e1b6e2f..16ab27a2e 100644 --- a/kernel/util/openai.go +++ b/kernel/util/openai.go @@ -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()