Skip to content

Commit

Permalink
Beta40 (#40)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40
  • Loading branch information
Hoshinonyaruko authored Apr 3, 2024
1 parent 85312f8 commit e1bf911
Show file tree
Hide file tree
Showing 15 changed files with 910 additions and 67 deletions.
118 changes: 116 additions & 2 deletions applogic/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package applogic

import (
"database/sql"
"fmt"

"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
"github.com/hoshinonyaruko/gensokyo-llm/hunyuan"
Expand Down Expand Up @@ -30,6 +31,7 @@ func (app *App) addMessage(msg structs.Message) (string, error) {
}

func (app *App) EnsureTablesExist() error {
// 创建 messages 表
createMessagesTableSQL := `
CREATE TABLE IF NOT EXISTS messages (
id VARCHAR(36) PRIMARY KEY,
Expand All @@ -42,14 +44,109 @@ func (app *App) EnsureTablesExist() error {

_, err := app.DB.Exec(createMessagesTableSQL)
if err != nil {
return fmtf.Errorf("error creating messages table: %w", err)
return fmt.Errorf("error creating messages table: %w", err)
}

// 为 conversation_id 创建索引
createConvIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_conversation_id ON messages(conversation_id);`

_, err = app.DB.Exec(createConvIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on messages(conversation_id): %w", err)
}

// 为 parent_message_id 创建索引(如果您需要通过 parent_message_id 查询)
createParentMsgIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_parent_message_id ON messages(parent_message_id);`

_, err = app.DB.Exec(createParentMsgIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on messages(parent_message_id): %w", err)
}

// 为 created_at 创建索引(如果您需要对消息进行时间排序)
createCreatedAtIndexSQL := `CREATE INDEX IF NOT EXISTS idx_created_at ON messages(created_at);`

_, err = app.DB.Exec(createCreatedAtIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on messages(created_at): %w", err)
}

// 其他创建

return nil
}

func (app *App) EnsureEmbeddingsTablesExist() error {
createMessagesTableSQL := `
CREATE TABLE IF NOT EXISTS vector_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
vector BLOB NOT NULL,
norm FLOAT NOT NULL,
group_id INTEGER NOT NULL
);`

_, err := app.DB.Exec(createMessagesTableSQL)
if err != nil {
return fmt.Errorf("error creating messages table: %w", err)
}

// 为group_id和norm添加索引
createIndexSQL := `
CREATE INDEX IF NOT EXISTS idx_group_id ON vector_data(group_id);
CREATE INDEX IF NOT EXISTS idx_norm ON vector_data(norm);`

_, err = app.DB.Exec(createIndexSQL)
if err != nil {
return fmtf.Errorf("error creating indexes: %w", err)
}

// 其他创建

return nil
}

func (app *App) EnsureQATableExist() error {
// 创建 questions 表
createQuestionsTableSQL := `
CREATE TABLE IF NOT EXISTS questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question_text TEXT NOT NULL,
vector_data_id INTEGER NOT NULL,
UNIQUE(question_text),
FOREIGN KEY(vector_data_id) REFERENCES vector_data(id)
);`

_, err := app.DB.Exec(createQuestionsTableSQL)
if err != nil {
return fmt.Errorf("error creating questions table: %w", err)
}

// 创建 qa_cache 表
createQACacheTableSQL := `
CREATE TABLE IF NOT EXISTS qa_cache (
id INTEGER PRIMARY KEY AUTOINCREMENT,
answer_text TEXT NOT NULL,
question_id INTEGER NOT NULL,
FOREIGN KEY(question_id) REFERENCES questions(id)
);`

_, err = app.DB.Exec(createQACacheTableSQL)
if err != nil {
return fmt.Errorf("error creating qa_cache table: %w", err)
}

// 为 qa_cache 表的 question_id 字段创建索引
createIndexSQL := `CREATE INDEX IF NOT EXISTS idx_question_id ON qa_cache(question_id);`

_, err = app.DB.Exec(createIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on qa_cache(question_id): %w", err)
}

return nil
}

func (app *App) EnsureUserContextTableExists() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS user_context (
Expand All @@ -60,7 +157,24 @@ func (app *App) EnsureUserContextTableExists() error {

_, err := app.DB.Exec(createTableSQL)
if err != nil {
return fmtf.Errorf("error creating user_context table: %w", err)
return fmt.Errorf("error creating user_context table: %w", err)
}

// 为 conversation_id 创建索引
createConvIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_context_conversation_id ON user_context(conversation_id);`

_, err = app.DB.Exec(createConvIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_context(conversation_id): %w", err)
}

// 为 parent_message_id 创建索引
// 只有当您需要根据 parent_message_id 进行查询时才添加此索引
createParentMsgIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_context_parent_message_id ON user_context(parent_message_id);`

_, err = app.DB.Exec(createParentMsgIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_context(parent_message_id): %w", err)
}

return nil
Expand Down
18 changes: 14 additions & 4 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,23 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
//是否安全模式
safemode := config.GetGptSafeMode()
useSSe := config.GetuseSse()
// 腾讯云审核 by api2d
gptModeration := config.GetGptModeration()
var gptModerationStop bool
if gptModeration {
gptModerationStop = true
}

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

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

Expand Down Expand Up @@ -172,6 +181,7 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmtf.Sprintf("Failed to read response body: %v", err), http.StatusInternalServerError)
return
}
// fmtf.Printf("chatgpt返回:%v", string(responseBody))
// 假设已经成功发送请求并获得响应,responseBody是响应体的字节数据
var apiResponse struct {
Choices []struct {
Expand Down
Loading

0 comments on commit e1bf911

Please sign in to comment.