From 21f32605c814b7f188efa5b64a51d7b802b3b1ca Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Sun, 28 Apr 2024 16:17:16 +0800 Subject: [PATCH 1/9] feat: safe send channel --- common/go-channel.go | 17 ++++++++++++++++- relay/channel/openai/relay-openai.go | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/common/go-channel.go b/common/go-channel.go index 4f00dff..8cd49ae 100644 --- a/common/go-channel.go +++ b/common/go-channel.go @@ -16,7 +16,22 @@ func SafeGoroutine(f func()) { }() } -func SafeSend(ch chan bool, value bool) (closed bool) { +func SafeSendBool(ch chan bool, value bool) (closed bool) { + defer func() { + // Recover from panic if one occured. A panic would mean the channel was closed. + if recover() != nil { + closed = true + } + }() + + // This will panic if the channel is closed. + ch <- value + + // If the code reaches here, then the channel was not closed. + return false +} + +func SafeSendString(ch chan string, value string) (closed bool) { defer func() { // Recover from panic if one occured. A panic would mean the channel was closed. if recover() != nil { diff --git a/relay/channel/openai/relay-openai.go b/relay/channel/openai/relay-openai.go index d627575..6825119 100644 --- a/relay/channel/openai/relay-openai.go +++ b/relay/channel/openai/relay-openai.go @@ -50,7 +50,7 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*d if data[:6] != "data: " && data[:6] != "[DONE]" { continue } - dataChan <- data + common.SafeSendString(dataChan, data) data = data[6:] if !strings.HasPrefix(data, "[DONE]") { streamItems = append(streamItems, data) @@ -123,7 +123,7 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*d // wait data out time.Sleep(2 * time.Second) } - common.SafeSend(stopChan, true) + common.SafeSendBool(stopChan, true) }() service.SetEventStreamHeaders(c) c.Stream(func(w io.Writer) bool { From 5425b5bfc3d144c205e2950d4bb537ed3cc2103e Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Sun, 28 Apr 2024 20:45:34 +0800 Subject: [PATCH 2/9] fix: aws claude --- relay/channel/claude/dto.go | 19 ++++++++++--------- relay/channel/claude/relay-claude.go | 5 ++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/relay/channel/claude/dto.go b/relay/channel/claude/dto.go index 2d13e46..47f0c3b 100644 --- a/relay/channel/claude/dto.go +++ b/relay/channel/claude/dto.go @@ -24,15 +24,16 @@ type ClaudeMessage struct { } type ClaudeRequest struct { - Model string `json:"model"` - Prompt string `json:"prompt,omitempty"` - System string `json:"system,omitempty"` - Messages []ClaudeMessage `json:"messages,omitempty"` - MaxTokens uint `json:"max_tokens,omitempty"` - StopSequences []string `json:"stop_sequences,omitempty"` - Temperature float64 `json:"temperature,omitempty"` - TopP float64 `json:"top_p,omitempty"` - TopK int `json:"top_k,omitempty"` + Model string `json:"model"` + Prompt string `json:"prompt,omitempty"` + System string `json:"system,omitempty"` + Messages []ClaudeMessage `json:"messages,omitempty"` + MaxTokens uint `json:"max_tokens,omitempty"` + MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"` + StopSequences []string `json:"stop_sequences,omitempty"` + Temperature float64 `json:"temperature,omitempty"` + TopP float64 `json:"top_p,omitempty"` + TopK int `json:"top_k,omitempty"` //ClaudeMetadata `json:"metadata,omitempty"` Stream bool `json:"stream,omitempty"` } diff --git a/relay/channel/claude/relay-claude.go b/relay/channel/claude/relay-claude.go index 07767a4..d1d8e6e 100644 --- a/relay/channel/claude/relay-claude.go +++ b/relay/channel/claude/relay-claude.go @@ -30,15 +30,14 @@ func RequestOpenAI2ClaudeComplete(textRequest dto.GeneralOpenAIRequest) *ClaudeR claudeRequest := ClaudeRequest{ Model: textRequest.Model, Prompt: "", - MaxTokens: textRequest.MaxTokens, StopSequences: nil, Temperature: textRequest.Temperature, TopP: textRequest.TopP, TopK: textRequest.TopK, Stream: textRequest.Stream, } - if claudeRequest.MaxTokens == 0 { - claudeRequest.MaxTokens = 4096 + if claudeRequest.MaxTokensToSample == 0 { + claudeRequest.MaxTokensToSample = 4096 } prompt := "" for _, message := range textRequest.Messages { From 7fb6420e66619090d34c7c9fcb3c4d573752b4ce Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Mon, 29 Apr 2024 00:06:25 +0800 Subject: [PATCH 3/9] fix: aws claude system --- relay/channel/aws/dto.go | 1 + 1 file changed, 1 insertion(+) diff --git a/relay/channel/aws/dto.go b/relay/channel/aws/dto.go index 7450908..7a267b1 100644 --- a/relay/channel/aws/dto.go +++ b/relay/channel/aws/dto.go @@ -5,6 +5,7 @@ import "one-api/relay/channel/claude" type AwsClaudeRequest struct { // AnthropicVersion should be "bedrock-2023-05-31" AnthropicVersion string `json:"anthropic_version"` + System string `json:"system"` Messages []claude.ClaudeMessage `json:"messages"` MaxTokens int `json:"max_tokens,omitempty"` Temperature float64 `json:"temperature,omitempty"` From 234e39ddeb7f31641ac9b5c7def73ec3bccf5d63 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Wed, 1 May 2024 01:32:01 +0800 Subject: [PATCH 4/9] feat: update midjourney task info update timeout --- controller/midjourney.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/midjourney.go b/controller/midjourney.go index 3d779c8..2d538f1 100644 --- a/controller/midjourney.go +++ b/controller/midjourney.go @@ -86,7 +86,7 @@ func UpdateMidjourneyTaskBulk() { continue } // 设置超时时间 - timeout := time.Second * 5 + timeout := time.Second * 15 ctx, cancel := context.WithTimeout(context.Background(), timeout) // 使用带有超时的 context 创建新的请求 req = req.WithContext(ctx) From cad380eb16e999d91afeec4b473b1afed7e190f4 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Wed, 1 May 2024 01:37:47 +0800 Subject: [PATCH 5/9] feat: able to set AccountFilter --- constant/midjourney.go | 1 + model/option.go | 3 +++ service/midjourney.go | 14 ++++++++------ web/src/components/OperationSetting.js | 7 +++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/constant/midjourney.go b/constant/midjourney.go index cd38d5f..a8d0bfe 100644 --- a/constant/midjourney.go +++ b/constant/midjourney.go @@ -1,6 +1,7 @@ package constant var MjNotifyEnabled = false +var MjAccountFilterEnabled = false var MjModeClearEnabled = false var MjForwardUrlEnabled = true diff --git a/model/option.go b/model/option.go index 57bbc38..1adc84c 100644 --- a/model/option.go +++ b/model/option.go @@ -92,6 +92,7 @@ func InitOptionMap() { common.OptionMap["DataExportDefaultTime"] = common.DataExportDefaultTime common.OptionMap["DefaultCollapseSidebar"] = strconv.FormatBool(common.DefaultCollapseSidebar) common.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(constant.MjNotifyEnabled) + common.OptionMap["MjAccountFilterEnabled"] = strconv.FormatBool(constant.MjAccountFilterEnabled) common.OptionMap["MjModeClearEnabled"] = strconv.FormatBool(constant.MjModeClearEnabled) common.OptionMap["MjForwardUrlEnabled"] = strconv.FormatBool(constant.MjForwardUrlEnabled) common.OptionMap["CheckSensitiveEnabled"] = strconv.FormatBool(constant.CheckSensitiveEnabled) @@ -197,6 +198,8 @@ func updateOptionMap(key string, value string) (err error) { common.DefaultCollapseSidebar = boolValue case "MjNotifyEnabled": constant.MjNotifyEnabled = boolValue + case "MjAccountFilterEnabled": + constant.MjAccountFilterEnabled = boolValue case "MjModeClearEnabled": constant.MjModeClearEnabled = boolValue case "MjForwardUrlEnabled": diff --git a/service/midjourney.go b/service/midjourney.go index ccf5141..6bb3a9e 100644 --- a/service/midjourney.go +++ b/service/midjourney.go @@ -165,7 +165,9 @@ func DoMidjourneyHttpRequest(c *gin.Context, timeout time.Duration, fullRequestU if err != nil { return MidjourneyErrorWithStatusCodeWrapper(constant.MjErrorUnknown, "read_request_body_failed", http.StatusInternalServerError), nullBytes, err } - delete(mapResult, "accountFilter") + if !constant.MjAccountFilterEnabled { + delete(mapResult, "accountFilter") + } if !constant.MjNotifyEnabled { delete(mapResult, "notifyHook") } @@ -174,11 +176,11 @@ func DoMidjourneyHttpRequest(c *gin.Context, timeout time.Duration, fullRequestU } if constant.MjModeClearEnabled { if prompt, ok := mapResult["prompt"].(string); ok { - prompt = strings.Replace(prompt, "--fast", "", -1) - prompt = strings.Replace(prompt, "--relax", "", -1) - prompt = strings.Replace(prompt, "--turbo", "", -1) - - mapResult["prompt"] = prompt + prompt = strings.Replace(prompt, "--fast", "", -1) + prompt = strings.Replace(prompt, "--relax", "", -1) + prompt = strings.Replace(prompt, "--turbo", "", -1) + + mapResult["prompt"] = prompt } } reqBody, err := json.Marshal(mapResult) diff --git a/web/src/components/OperationSetting.js b/web/src/components/OperationSetting.js index 44b6214..7566faa 100644 --- a/web/src/components/OperationSetting.js +++ b/web/src/components/OperationSetting.js @@ -38,6 +38,7 @@ const OperationSetting = () => { StopOnSensitiveEnabled: '', SensitiveWords: '', MjNotifyEnabled: '', + MjAccountFilterEnabled: '', MjModeClearEnabled: '', MjForwardUrlEnabled: '', DrawingEnabled: '', @@ -323,6 +324,12 @@ const OperationSetting = () => { name='MjNotifyEnabled' onChange={handleInputChange} /> +