From fefea3b5088980a1f2cc4768e115981b38bc7836 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 10 Mar 2023 16:41:15 +0800 Subject: [PATCH] feat: if no available API, revive one randomly --- code/services/loadbalancer/loadbalancer.go | 10 +++++++++- readme.md | 8 ++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/code/services/loadbalancer/loadbalancer.go b/code/services/loadbalancer/loadbalancer.go index ca460de4..930482ba 100644 --- a/code/services/loadbalancer/loadbalancer.go +++ b/code/services/loadbalancer/loadbalancer.go @@ -1,7 +1,10 @@ package loadbalancer import ( + "fmt" + "math/rand" "sync" + "time" ) type API struct { @@ -36,7 +39,12 @@ func (lb *LoadBalancer) GetAPI() *API { } } if len(availableAPIs) == 0 { - return nil + //随机复活一个 + fmt.Printf("No available API, revive one randomly\n") + rand.Seed(time.Now().UnixNano()) + index := rand.Intn(len(lb.apis)) + lb.apis[index].Available = true + return lb.apis[index] } selectedAPI := availableAPIs[0] diff --git a/readme.md b/readme.md index 97f5342a..99f3254b 100644 --- a/readme.md +++ b/readme.md @@ -208,10 +208,14 @@ docker run -d --name feishu-chatgpt -p 9000:9000 \ --env BOT_NAME=chatGpt \ --env OPENAI_KEY="sk-xxx1,sk-xxx2,sk-xxx3" \ --env API_URL="https://api.openai.com" \ ---env HTTP_PROXY="http://host.docker.internal:7890" \ +--env HTTP_PROXY="" \ feishu-chatgpt:latest ``` - +注意: +- `BOT_NAME` 为飞书机器人名称,例如 `chatGpt` +- `OPENAI_KEY` 为openai key,多个key用逗号分隔,例如 `sk-xxx1,sk-xxx2,sk-xxx3` +- `HTTP_PROXY` 为宿主机的proxy地址,例如 `http://host.docker.internal:7890` +- `API_URL` 为openai api 接口地址,例如 `https://api.openai.com`, 没有反向代理的话,可以不用设置 --- 小白简易化 docker 部署