Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chatid #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions code/handlers/event_msg_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
}
msg := a.handler.sessionCache.GetMsg(*a.info.sessionId)
// 如果没有提示词,默认模拟ChatGPT
msg = setDefaultPrompt(msg)
// msg = setDefaultPrompt(msg)
msg = append(msg, openai.Messages{
Role: "user", Content: a.info.qParsed,
})
Expand All @@ -58,7 +58,7 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
aiMode := a.handler.sessionCache.GetAIMode(*a.info.sessionId)
fmt.Println("msg: ", msg)
fmt.Println("aiMode: ", aiMode)
completions, err := a.handler.gpt.Completions(msg, aiMode)
completions, err := a.handler.gpt.Completions(msg, aiMode, *a.info.sessionId)
if err != nil {
replyMsg(*a.ctx, fmt.Sprintf(
"🤖️:消息机器人摆烂了,请稍后再试~\n错误信息: %v", err), a.info.msgId)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (*MessageAction) Execute(a *ActionInfo) bool {
return true
}

//判断msg中的是否包含system role
// 判断msg中的是否包含system role
func hasSystemRole(msg []openai.Messages) bool {
for _, m := range msg {
if m.Role == "system" {
Expand Down
2 changes: 1 addition & 1 deletion code/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
//WarnLevel
//InfoLevel
//DebugLevel
logger.Level = logrus.InfoLevel
logger.Level = logrus.DebugLevel

}

Expand Down
2 changes: 1 addition & 1 deletion code/services/openai/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (gpt *ChatGPT) doAPIRequestWithRetry(url, method string,
if err != nil || response.StatusCode < 200 || response.StatusCode >= 300 {

body, _ := ioutil.ReadAll(response.Body)
fmt.Println("body", string(body))
logger.Debug("response body", string(body))

gpt.Lb.SetAvailability(api.Key, false)
if retry == maxRetries {
Expand Down
10 changes: 7 additions & 3 deletions code/services/openai/gpt3.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type ChatGPTChoiceItem struct {

// ChatGPTRequestBody 响应体
type ChatGPTRequestBody struct {
ChatId string `json:"chatId"`
Stream bool `json:"stream"`
Detail bool `json:"detail"`
Model string `json:"model"`
Messages []Messages `json:"messages"`
MaxTokens int `json:"max_tokens"`
Expand All @@ -68,9 +71,10 @@ func (msg *Messages) CalculateTokenLength() int {
return tokenizer.MustCalToken(text)
}

func (gpt *ChatGPT) Completions(msg []Messages, aiMode AIMode) (resp Messages,
func (gpt *ChatGPT) Completions(msg []Messages, aiMode AIMode, chatid string) (resp Messages,
err error) {
requestBody := ChatGPTRequestBody{
ChatId: chatid,
Model: gpt.Model,
Messages: msg,
MaxTokens: gpt.MaxTokens,
Expand All @@ -81,13 +85,13 @@ func (gpt *ChatGPT) Completions(msg []Messages, aiMode AIMode) (resp Messages,
}
gptResponseBody := &ChatGPTResponseBody{}
url := gpt.FullUrl("chat/completions")
//fmt.Println(url)
logger.Debug(url)
logger.Debug("request url ", url)
logger.Debug("request body ", requestBody)
if url == "" {
return resp, errors.New("无法获取openai请求地址")
}
err = gpt.sendRequestWithBodyType(url, "POST", jsonBody, requestBody, gptResponseBody)
logger.Debugf("gptresponse %v", gptResponseBody)
if err == nil && len(gptResponseBody.Choices) > 0 {
resp = gptResponseBody.Choices[0].Message
} else {
Expand Down