Skip to content

Commit

Permalink
-- UPDATE
Browse files Browse the repository at this point in the history
 -- Add DEEP SEEK to AI assistant
  • Loading branch information
NoahAmethyst committed Nov 7, 2024
1 parent af802fc commit 9d904e0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions constant/preference.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
ERNIE_APP_SECRET = "ERNIE_APP_SECRET"
QIANFAN_ACCESS_KEY = "QIANFAN_ACCESS_KEY"
QIANFAN_SECRET_KEY = "QIANFAN_SECRET_KEY"
DEEPSEEK_API_KEY = "DEEPSEEK_API_KEY"
NOT_MIRROR = "NOT_MIRROR"
FILE_ROOT = "FILE_ROOT"
OWNER = "OWNER"
Expand Down
15 changes: 11 additions & 4 deletions coolq/chat_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ func AskAssistant(assistant Assistant) {
vendor = "BingCopilot"
case ai_util.Ernie:
vendor = "百度千帆"
case ai_util.DeepSeek:
vendor = "DEEP SEEK"

default:
break
}
Expand Down Expand Up @@ -368,26 +371,30 @@ func askOfficialChatGpt(assistant Assistant, recvChan chan struct{}) {
var answer string
var resp openai.ChatCompletionResponse
var err error
vendor := "OpenAI"
switch assistant.Model() {
case ai_util.ChatGPT:
resp, err = ai_util.AskChatGpt(ctx)
case ai_util.ChatGPT4:
resp, err = ai_util.AskChatGpt4(ctx)
case ai_util.DeepSeek:
resp, err = ai_util.AskDeepSeek(ctx)

default:
resp, err = ai_util.AskChatGpt(ctx)
}

if err != nil {
answer = fmt.Sprintf("调用openAi 失败:%s", err.Error())
answer = fmt.Sprintf("调用%s 失败:%s", vendor, err.Error())
if strings.Contains(err.Error(), "401") && assistant.Sender() != assistant.Bot().state.owner {
assistant.Bot().SendPrivateMessage(assistant.Bot().state.owner, 0, &message.SendingMessage{Elements: []message.IMessageElement{
message.NewText(
fmt.Sprintf("用户[%d]调用openai失败:%s", assistant.Sender(), err.Error()))}})
fmt.Sprintf("用户[%d]调用%s失败:%s", assistant.Sender(), vendor, err.Error()))}})
}
} else {
if len(resp.Choices) == 0 || len(resp.Choices[0].Message.Content) == 0 {
log.Warnf("openai 返回空结构:%v", resp)
answer = "OpenAI未响应,请重试"
log.Warnf("%s 返回空结构:%v", vendor, resp)
answer = fmt.Sprintf("%s,请重试", vendor)
} else {
answer = resp.Choices[0].Message.Content
answer = strings.ReplaceAll(answer, "*", "")
Expand Down
2 changes: 1 addition & 1 deletion coolq/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
CMDImage: "AI作图,使用DELL.2生成图片,你需要提供提示词",
CMDOpenReporter: "开启微博、华尔街最新资讯、36氪定时推送",
CMDCloseReporter: "关闭微博、华尔街最新资讯、36氪定时推送",
CMDSwitchAssistantModel: "更换助手模式,0:Chatgpt(默认),1:Bing Chat",
CMDSwitchAssistantModel: "更换助手模式,\n0:Chatgpt(默认) \n1:Bing Chat \n2:ChatGpt(4) \n3:百度千帆 \n4:DeepSeek",
CMDEVN: "设置新的环境变量",
}
groupCmdList = []string{
Expand Down
13 changes: 13 additions & 0 deletions gotest/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func Test_ChatGpt(t *testing.T) {
t.Logf("%+v", replyMsg)
}

func Test_DeepSeek(t *testing.T) {
ctx := make([]openai.ChatCompletionMessage, 0, 4)
ctx = append(ctx, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: "魔术的起源是什么",
})
replyMsg, err := ai_util.AskDeepSeek(ctx)
if err != nil {
panic(err)
}
t.Logf("%+v", replyMsg)
}

func Test_ChatGptWithContext(t *testing.T) {
ctx := make([]openai.ChatCompletionMessage, 0, 4)
ctx = append(ctx, openai.ChatCompletionMessage{
Expand Down
15 changes: 15 additions & 0 deletions util/ai_util/ai_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
var openaiCli *openai.Client
var chimeraCli *openai.Client
var ernieCli *go_ernie.Client
var deepSeekCli *openai.Client

var openaiKey string
var chimeraKey string
var ernieKey string
var ernieSecret string
var deepSeekApiKey string

var changeSignal = make(chan struct{}, 1)
var once sync.Once
Expand Down Expand Up @@ -59,6 +61,9 @@ func setCli() {
ernieSecret = os.Getenv(constant.QIANFAN_SECRET_KEY)
}

if len(deepSeekApiKey) == 0 {
deepSeekApiKey = os.Getenv(constant.DEEPSEEK_API_KEY)
}
//OpenAI client
{
if len(openaiKey) > 0 {
Expand Down Expand Up @@ -93,6 +98,16 @@ func setCli() {
ernieCli = go_ernie.NewDefaultClient(ernieKey, ernieSecret)
}
}
//DeepSeek
{
if len(deepSeekApiKey) > 0 {
log.Info("init DeepSeek client")
config := openai.DefaultConfig(deepSeekApiKey)
config.HTTPClient.Timeout = time.Minute * 120
config.BaseURL = deepSeekBaseUrl
deepSeekCli = openai.NewClientWithConfig(config)
}
}

}

Expand Down
23 changes: 23 additions & 0 deletions util/ai_util/deepseek.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ai_util

import (
"context"
openai "github.com/sashabaranov/go-openai"
log "github.com/sirupsen/logrus"
)

const deepSeekChat = "deepseek-chat"
const deepSeekBaseUrl = "https://api.deepseek.com"

func AskDeepSeek(ctx []openai.ChatCompletionMessage) (openai.ChatCompletionResponse, error) {
initCli()
resp, err := deepSeekCli.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: deepSeekChat,
Messages: ctx,
},
)
log.Infof("收到deepSeek响应:%+v", resp)
return resp, err
}
1 change: 1 addition & 0 deletions util/ai_util/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
BingCopilot
ChatGPT4
Ernie
DeepSeek
)

0 comments on commit 9d904e0

Please sign in to comment.