Skip to content

Commit

Permalink
feat: add more API from lolimi (#56)
Browse files Browse the repository at this point in the history
* feat: more lolimi api

* fix: LolimiAi2Mem TalkPlain
修复用户的请求内容不会包含在序列化的 JSON 数据中的问题
修复响应解析问题

* fix: lolimi2Message initial value

* fix: API key leak when error occurred

* fix: disable LolimiAi2Mem
LolimiAi2Mem 有严重 Bug,编写者不知道怎么解决

* chore: merge lolimi.go and lolimi2.go

* fix: typo

* chore: merge LolimiAi and LolimiMemoryAi

* fix: panic
  • Loading branch information
EatHatsuneShallots authored Aug 22, 2024
1 parent 71c23d2 commit 632cea9
Showing 1 changed file with 84 additions and 12 deletions.
96 changes: 84 additions & 12 deletions aireply/lolimi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aireply

import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
Expand All @@ -12,26 +14,49 @@ import (

// LolimiAi Lolimi回复类
type LolimiAi struct {
u string
n string
b []string
u string // API 地址
n string // AI 名称
k string // API 密钥
b []string // Banwords
t bool // API 响应模式是否为文本
l int // 记忆限制数(小于 1 的值可以禁用记忆模式)
m []lolimiMessage // 记忆数据
}

// lolimiMessage 消息记忆记录
type lolimiMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}

const (
lolimiURL = "https://api.lolimi.cn"
lolimiURL = "https://apii.lolimi.cn"
// MomoURL api地址
MomoURL = lolimiURL + "/API/AI/mm.php?msg=%v"
MomoURL = lolimiURL + "/api/mmai/mm?key=%s&msg=%s"
// MomoBotName ...
MomoBotName = "沫沫"
// JingfengURL api地址
JingfengURL = lolimiURL + "/API/AI/jj.php?msg=%v"
JingfengURL = lolimiURL + "/api/jjai/jj?key=%s&msg=%s"
// JingfengBotName ...
JingfengBotName = "婧枫"
// GPT4oURL api地址
GPT4oURL = lolimiURL + "/api/4o/gpt4o?key=%s&msg=%s"
// GPT4oBotName ...
// TODO 换个更好的名字
GPT4oBotName = "GPT4o"

// 带记忆 POST 请求专区

// C4oURL api地址
C4oURL = lolimiURL + "/api/c4o/c?key=%s"
// C4oBotName ...
// TODO 换个更好的名字
C4oBotName = "GPT4o"
)

// NewLolimiAi ...
func NewLolimiAi(u, name string, banwords ...string) *LolimiAi {
return &LolimiAi{u: u, n: name, b: banwords}
func NewLolimiAi(u, name string, key string, textMode bool, memoryLimit int, banwords ...string) *LolimiAi {
return &LolimiAi{u: u, n: name, k: key, t: textMode, l: memoryLimit, b: banwords}
}

// String ...
Expand All @@ -42,12 +67,42 @@ func (l *LolimiAi) String() string {
// TalkPlain 取得回复消息
func (l *LolimiAi) TalkPlain(_ int64, msg, nickname string) string {
msg = strings.ReplaceAll(msg, nickname, l.n)
u := fmt.Sprintf(l.u, url.QueryEscape(msg))
data, err := web.GetData(u)
var u string
var data []byte
var err error
if l.l > 0 {
u = fmt.Sprintf(l.u, url.QueryEscape(l.k))
json, err := json.Marshal(
append(l.m,
lolimiMessage{
Role: "user", Content: msg,
},
),
)
if err != nil {
//panic(err)
return "ERROR: " + err.Error()
}
// TODO: 可能会返回
// "请使用psot格式请求如有疑问进官方群"
data, err = web.PostData(u, "application/json", bytes.NewReader(json))

Check failure on line 88 in aireply/lolimi.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `err` is never used (staticcheck)
} else {
u := fmt.Sprintf(l.u, url.QueryEscape(l.k), url.QueryEscape(msg))
data, err = web.GetData(u)
}
if err != nil {
return "ERROR: " + err.Error()
errMsg := err.Error()
// Remove the key from error message
errMsg = strings.ReplaceAll(errMsg, l.k, "********")
return "ERROR: " + errMsg
}
var replystr string
if l.t {
replystr = binary.BytesToString(data)
} else {
replystr = gjson.Get(binary.BytesToString(data), "data.output").String()
}
replystr := gjson.Get(binary.BytesToString(data), "data.output").String()
// TODO: 是否要删除遗留代码
replystr = strings.ReplaceAll(replystr, "<img src=\"", "[CQ:image,file=")
replystr = strings.ReplaceAll(replystr, "<br>", "\n")
replystr = strings.ReplaceAll(replystr, "\" />", "]")
Expand All @@ -57,6 +112,23 @@ func (l *LolimiAi) TalkPlain(_ int64, msg, nickname string) string {
return "ERROR: 回复可能含有敏感内容"
}
}
if l.l > 0 {
// 添加记忆
var m []lolimiMessage
if len(l.m) >= l.l-1 && len(l.m) >= 2 {
m = l.m[2:]
} else {
m = l.m
}
l.m = append(m,

Check failure on line 123 in aireply/lolimi.go

View workflow job for this annotation

GitHub Actions / lint

appendAssign: append result not assigned to the same slice (gocritic)
lolimiMessage{
Role: "user", Content: msg,
},
lolimiMessage{
Role: "assistant", Content: textReply,
},
)
}
return textReply
}

Expand Down

0 comments on commit 632cea9

Please sign in to comment.