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

Beta59 #59

Merged
merged 60 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 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
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# specific
config.yml
*.yml
*.sqlite
*.txt
*.7z

# Go specific
*.exe
Expand Down
5 changes: 5 additions & 0 deletions applogic/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"

"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
"github.com/hoshinonyaruko/gensokyo-llm/hunyuan"
"github.com/hoshinonyaruko/gensokyo-llm/structs"
Expand Down Expand Up @@ -262,6 +263,10 @@ func (app *App) updateUserContext(userID int64, parentMessageID string) error {
}

func (app *App) getHistory(conversationID, parentMessageID string) ([]structs.Message, error) {
// 如果不开启上下文
if config.GetNoContext() {
return nil, nil
}
var history []structs.Message

// SQL 查询获取历史信息
Expand Down
20 changes: 9 additions & 11 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,17 @@ func truncateHistoryGpt(history []structs.Message, prompt string) []structs.Mess
tokenCount += len(msg.Text)
}

if tokenCount <= MAX_TOKENS {
return history
}

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

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

Expand Down
19 changes: 14 additions & 5 deletions applogic/embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (app *App) CalculateTextEmbeddingHunyuan(text string) ([]float64, error) {
}

if config.GetPrintVector() {
fmt.Printf("混元返回的向量:%v\n", embedding)
fmtf.Printf("混元返回的向量:%v\n", embedding)
}

return embedding, nil
Expand Down Expand Up @@ -229,9 +229,16 @@ func (app *App) insertVectorData(text string, vector []float64) (int64, error) {
norm := math.Sqrt(sum)
n := config.GetCacheN()
k := config.GetCacheK()
l := int(norm * k)
// 先进行四舍五入,然后转换为int64
l := int64(math.Round(norm * k))
groupID := l % n

if config.GetPrintHanming() {
fmtf.Printf("(norm*k): %v\n", norm*k)
fmtf.Printf("(norm*k) mod n ==== (%v) mod %v\n", l, n)
fmtf.Printf("groupid : %v\n", groupID)
}

result, err := app.DB.Exec("INSERT INTO vector_data (text, vector, norm, group_id) VALUES (?, ?, ?, ?)", text, binaryVector, norm, groupID)
if err != nil {
return 0, err
Expand All @@ -246,7 +253,7 @@ func (app *App) insertVectorData(text string, vector []float64) (int64, error) {
}

// searchSimilarText函数根据汉明距离搜索数据库中与给定向量相似的文本
func (app *App) searchSimilarText(vector []float64, threshold int, targetGroupID int) ([]TextDistance, []int, error) {
func (app *App) searchSimilarText(vector []float64, threshold int, targetGroupID int64) ([]TextDistance, []int, error) {
binaryVector := vectorToBinaryConcurrent(vector) // 二值化查询向量
var results []TextDistance
var ids []int
Expand Down Expand Up @@ -289,17 +296,19 @@ func (app *App) searchSimilarText(vector []float64, threshold int, targetGroupID
return results, sortedIds, nil
}

func calculateGroupID(vector []float64) int {
func calculateGroupID(vector []float64) int64 {
var sum float64
for _, v := range vector {
sum += v * v
}
norm := math.Sqrt(sum)
k := config.GetCacheK()
n := config.GetCacheN()
l := int(norm * k)
// 先进行四舍五入,然后转换为int64
l := int64(math.Round(norm * k))
groupid := l % n // 通过范数计算出一个整数,并将其模n来分配到一个组
if config.GetPrintHanming() {
fmtf.Printf("(norm*k): %v\n", norm*k)
fmtf.Printf("(norm*k) mod n ==== (%v) mod %v\n", l, n)
fmtf.Printf("groupid : %v\n", groupid)
}
Expand Down
26 changes: 12 additions & 14 deletions applogic/ernie.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,17 @@ func truncateHistoryErnie(history []structs.Message, prompt string) []structs.Me
tokenCount += len(msg.Text)
}

if tokenCount <= MAX_TOKENS {
return history
}

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

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

Expand All @@ -391,9 +389,9 @@ func truncateHistoryErnie(history []structs.Message, prompt string) []structs.Me
}
}

// 第三步:确保以user结尾
if len(history) > 0 && history[len(history)-1].Role == "assistant" {
for len(history) > 0 && history[len(history)-1].Role != "user" {
// 第三步:确保以assistant结尾
if len(history) > 0 && history[len(history)-1].Role == "user" {
for len(history) > 0 && history[len(history)-1].Role != "assistant" {
history = history[:len(history)-1]
}
}
Expand Down
Loading
Loading