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

Beta78 #77

Merged
merged 79 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
ea4bcc6
beta1
Hoshinonyaruko Jan 21, 2024
5002a28
beta2
Hoshinonyaruko Jan 21, 2024
969841e
beta3
Hoshinonyaruko Jan 21, 2024
a2989c2
beta4
Hoshinonyaruko Jan 21, 2024
611cd8b
beta5
Hoshinonyaruko Jan 21, 2024
dbad42b
beta6
Hoshinonyaruko Jan 21, 2024
4f8895e
beta7
Hoshinonyaruko Jan 21, 2024
30a9cab
beta8
Hoshinonyaruko Jan 21, 2024
461d879
beta9
Hoshinonyaruko Jan 21, 2024
c90c2ee
beta10
Hoshinonyaruko Jan 21, 2024
5227c32
beta11
Hoshinonyaruko Jan 21, 2024
8e14e56
beta12
Hoshinonyaruko Jan 21, 2024
6adfb7f
beta13
Hoshinonyaruko Jan 21, 2024
b04f6c0
beta14
Hoshinonyaruko Jan 21, 2024
649e740
beta15
Hoshinonyaruko Jan 21, 2024
d55cf29
beta16
Hoshinonyaruko Jan 21, 2024
1cc351e
beta16
Hoshinonyaruko Jan 21, 2024
f1a373e
beta19
Hoshinonyaruko Jan 21, 2024
cbf2fe1
beta20
Hoshinonyaruko Jan 21, 2024
3485bfc
beta21
Hoshinonyaruko Jan 21, 2024
3e3bb1a
beta22
Hoshinonyaruko Jan 21, 2024
2e4f3fa
beta23
Hoshinonyaruko Mar 29, 2024
f74075d
beta24
Hoshinonyaruko Mar 29, 2024
f3913cf
beta25
Hoshinonyaruko Mar 29, 2024
4cfb51a
beta27
Hoshinonyaruko Mar 30, 2024
d9b26c0
beta28
Hoshinonyaruko Mar 31, 2024
19da709
beta29
Hoshinonyaruko Mar 31, 2024
e06dcbf
merge
Hoshinonyaruko Mar 31, 2024
c76923e
beta30
Hoshinonyaruko Mar 31, 2024
9ae21da
beta31
Hoshinonyaruko Apr 1, 2024
4f333bc
merge
Hoshinonyaruko Apr 1, 2024
5acba8f
beta33
Hoshinonyaruko Apr 1, 2024
c637fcd
beta34
Hoshinonyaruko Apr 1, 2024
4e85b37
beta35
Hoshinonyaruko Apr 1, 2024
970948e
beta36
Hoshinonyaruko Apr 1, 2024
14271c3
beta37
Hoshinonyaruko Apr 1, 2024
6154d1a
beta38
Hoshinonyaruko Apr 1, 2024
d5be5e6
beta39
Hoshinonyaruko Apr 3, 2024
54afe18
beta40
Hoshinonyaruko Apr 3, 2024
0b71855
beta41
Hoshinonyaruko Apr 3, 2024
8c5f9a3
merge
Hoshinonyaruko Apr 3, 2024
cf752d4
beta42
Hoshinonyaruko Apr 3, 2024
3698824
beta43
Hoshinonyaruko Apr 3, 2024
1e9c189
beta44
Hoshinonyaruko Apr 3, 2024
2adb4ac
beta45
Hoshinonyaruko Apr 4, 2024
79eb713
beta45
Hoshinonyaruko Apr 4, 2024
064f0d0
beta45
Hoshinonyaruko Apr 4, 2024
5a4a59e
beta46
Hoshinonyaruko Apr 5, 2024
df30a32
beta46
Hoshinonyaruko Apr 5, 2024
c920893
beat48
Hoshinonyaruko Apr 7, 2024
3491d5d
beta49
Hoshinonyaruko Apr 8, 2024
575b2ca
beta50
Hoshinonyaruko Apr 8, 2024
4c4bbaa
beta51
Hoshinonyaruko Apr 8, 2024
2ea728b
beta52
Hoshinonyaruko Apr 8, 2024
82bf952
beta53
Hoshinonyaruko Apr 8, 2024
869685c
beta54
Hoshinonyaruko Apr 9, 2024
1f3646e
beta55
Hoshinonyaruko Apr 9, 2024
d6bbadf
beta57
Hoshinonyaruko Apr 10, 2024
fc5086c
beta58
Hoshinonyaruko Apr 10, 2024
602ff65
beta59
Hoshinonyaruko Apr 10, 2024
a82538d
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo-llm…
Hoshinonyaruko Apr 10, 2024
6433600
beta61
Hoshinonyaruko Apr 10, 2024
9ce88b3
beta62
Hoshinonyaruko Apr 12, 2024
b53617b
beta63
Hoshinonyaruko Apr 12, 2024
ff64c69
beta63
Hoshinonyaruko Apr 12, 2024
b34885f
beta64
Hoshinonyaruko Apr 13, 2024
3658648
beta65
Hoshinonyaruko Apr 13, 2024
3307439
beta66
Hoshinonyaruko Apr 15, 2024
f15c422
beta67
Hoshinonyaruko Apr 17, 2024
518de35
beta70
Hoshinonyaruko Apr 18, 2024
a65f07d
beta71
Hoshinonyaruko Apr 18, 2024
59d07c7
beta72
Hoshinonyaruko Apr 18, 2024
a16ea64
beta72
Hoshinonyaruko Apr 18, 2024
08d5864
beta74
Hoshinonyaruko Apr 19, 2024
9b09401
beta75
Hoshinonyaruko Apr 19, 2024
b51e185
beta76
Hoshinonyaruko Apr 20, 2024
c978654
beta77
Hoshinonyaruko Apr 21, 2024
6aa9f33
beta78
Hoshinonyaruko Apr 22, 2024
2ea6ae3
Merge branch 'main' into beta78
Hoshinonyaruko Apr 22, 2024
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
59 changes: 46 additions & 13 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"sync"

Expand Down Expand Up @@ -135,7 +136,7 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}

// 截断历史信息
userhistory = truncateHistoryGpt(userhistory, msg.Text)
userhistory = truncateHistoryGpt(userhistory, msg.Text, promptstr)

// 注意追加的顺序,确保问题在系统提示词之后
// 使用...操作符来展开userhistory切片并追加到history切片
Expand All @@ -146,8 +147,8 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {

// 构建请求到ChatGPT API
model := config.GetGptModel(promptstr)
apiURL := config.GetGptApiPath()
token := config.GetGptToken()
apiURL := config.GetGptApiPath(promptstr)
token := config.GetGptToken(promptstr)

// 构造消息历史和当前消息
messages := []map[string]interface{}{}
Expand All @@ -173,20 +174,52 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}

// 构建请求体
requestBody := map[string]interface{}{
"model": model,
"messages": messages,
"safe_mode": safemode,
"stream": useSSe,
"moderation": gptModeration,
"moderation_stop": gptModerationStop,
var requestBody map[string]interface{}

if config.GetStandardGptApi() {
requestBody = map[string]interface{}{
"model": model,
"messages": messages,
"stream": useSSe,
}
} else {
requestBody = map[string]interface{}{
"model": model,
"messages": messages,
"safe_mode": safemode,
"stream": useSSe,
"moderation": gptModeration,
"moderation_stop": gptModerationStop,
}
}

fmtf.Printf("chatgpt requestBody :%v", requestBody)
requestBodyJSON, _ := json.Marshal(requestBody)

// 准备HTTP请求
// 获取代理服务器地址
proxyURL := config.GetProxy(promptstr)
if err != nil {
http.Error(w, fmtf.Sprintf("Failed to get proxy: %v", err), http.StatusInternalServerError)
return
}

client := &http.Client{}

// 检查是否有有效的代理地址
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
http.Error(w, fmtf.Sprintf("Failed to parse proxy URL: %v", err), http.StatusInternalServerError)
return
}

// 配置客户端使用代理
client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
}
}

// 创建HTTP请求
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(requestBodyJSON))
if err != nil {
http.Error(w, fmtf.Sprintf("Failed to create request: %v", err), http.StatusInternalServerError)
Expand Down Expand Up @@ -438,8 +471,8 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}
}

func truncateHistoryGpt(history []structs.Message, prompt string) []structs.Message {
MAX_TOKENS := config.GetMaxTokenGpt()
func truncateHistoryGpt(history []structs.Message, prompt string, promptstr string) []structs.Message {
MAX_TOKENS := config.GetMaxTokenGpt(promptstr)

tokenCount := len(prompt)
for _, msg := range history {
Expand Down
11 changes: 8 additions & 3 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}

// 如果使用向量缓存 或者使用 向量安全词
if config.GetUseCache() || config.GetVectorSensitiveFilter() {
if config.GetUseCache(promptstr) || config.GetVectorSensitiveFilter() {
if config.GetPrintHanming() {
fmtf.Printf("计算向量的文本: %v", newmsg)
}
Expand Down Expand Up @@ -515,7 +515,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
}
//清空之前加入缓存
// 缓存省钱部分
// 缓存省钱部分 这里默认不被覆盖,如果主配置开了缓存,始终缓存.
if config.GetUseCache() {
if response != "" {
fmtf.Printf("缓存了Q:%v,A:%v,向量ID:%v", newmsg, response, lastSelectedVectorID)
Expand Down Expand Up @@ -556,7 +556,12 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
promptkeyboard = config.GetPromptkeyboard()
} else {
fmtf.Printf("ai生成气泡:%v", "Q"+newmsg+"A"+response)
promptkeyboard = GetPromptKeyboardAI("Q" + newmsg + "A" + response)
promptkeyboard = GetPromptKeyboardAI("Q"+newmsg+"A"+response, promptstr)
}

// 使用acnode.CheckWordOUT()过滤promptkeyboard中的每个字符串
for i, item := range promptkeyboard {
promptkeyboard[i] = acnode.CheckWordOUT(item)
}

// 使用acnode.CheckWordOUT()过滤promptkeyboard中的每个字符串
Expand Down
23 changes: 20 additions & 3 deletions applogic/promptkeyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"

"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
)

// ResponseDataPromptKeyboard 用于解析外层响应
Expand All @@ -19,20 +21,35 @@ type ResponseDataPromptKeyboard struct {
}

// 你要扮演一个json生成器,根据我下一句提交的QA内容,推断我可能会继续问的问题,生成json数组格式的结果,如:输入Q我好累啊A要休息一下吗,返回["嗯,我想要休息","我想喝杯咖啡","你平时怎么休息呢"],返回需要是["","",""]需要2-3个结果
func GetPromptKeyboardAI(msg string) []string {
url := config.GetAIPromptkeyboardPath()
func GetPromptKeyboardAI(msg string, promptstr string) []string {
baseurl := config.GetAIPromptkeyboardPath()
// 使用net/url包来构建和编码URL
urlParams := url.Values{}
if promptstr != "" {
urlParams.Add("prompt", promptstr)
}

// 将查询参数编码后附加到基本URL上
fullURL := baseurl
if len(urlParams) > 0 {
fullURL += "?" + urlParams.Encode()
}

fmtf.Printf("Generated PromptKeyboard URL:%v\n", fullURL)

requestBody, err := json.Marshal(map[string]interface{}{
"message": msg,
"conversationId": "",
"parentMessageId": "",
"user_id": "",
})

if err != nil {
fmt.Printf("Error marshalling request: %v\n", err)
return config.GetPromptkeyboard()
}

resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
resp, err := http.Post(fullURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
fmt.Printf("Error sending request: %v\n", err)
return config.GetPromptkeyboard()
Expand Down
48 changes: 47 additions & 1 deletion applogic/rwkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (app *App) ChatHandlerRwkv(w http.ResponseWriter, r *http.Request) {
}

// 截断历史信息
userhistory = truncateHistoryGpt(userhistory, msg.Text)
userhistory = truncateHistoryRwkv(userhistory, msg.Text, promptstr)

// 注意追加的顺序,确保问题在系统提示词之后
// 使用...操作符来展开userhistory切片并追加到history切片
Expand Down Expand Up @@ -439,3 +439,49 @@ func (app *App) ChatHandlerRwkv(w http.ResponseWriter, r *http.Request) {
}

}

func truncateHistoryRwkv(history []structs.Message, prompt string, promptstr string) []structs.Message {
MAX_TOKENS := config.GetRwkvMaxTokens(promptstr)

tokenCount := len(prompt)
for _, msg := range history {
tokenCount += len(msg.Text)
}

if tokenCount >= MAX_TOKENS {
// 第一步:从开始逐个移除消息,直到满足令牌数量限制
for tokenCount > MAX_TOKENS && len(history) > 0 {
tokenCount -= len(history[0].Text)
history = history[1:]

// 确保移除后,历史记录仍然以user消息结尾
if len(history) > 0 && history[0].Role == "assistant" {
tokenCount -= len(history[0].Text)
history = history[1:]
}
}
}

// 第二步:检查并移除包含空文本的QA对
for i := 0; i < len(history)-1; i++ { // 使用len(history)-1是因为我们要检查成对的消息
q := history[i]
a := history[i+1]

// 检查Q和A是否成对,且A的角色应为assistant,Q的角色为user,避免删除非QA对的消息
if q.Role == "user" && a.Role == "assistant" && (len(q.Text) == 0 || len(a.Text) == 0) {
fmtf.Println("closeai-找到了空的对话: ", q, a)
// 移除这对QA
history = append(history[:i], history[i+2:]...)
i-- // 因为删除了元素,调整索引以正确检查下一个元素
}
}

// 确保以user结尾,如果不是则尝试移除直到满足条件
if len(history) > 0 && history[len(history)-1].Role != "user" {
for len(history) > 0 && history[len(history)-1].Role != "user" {
history = history[:len(history)-1]
}
}

return history
}
Loading
Loading