-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6d0910
commit c70c3e7
Showing
11 changed files
with
336 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ | |
/.idea/* | ||
/coverage.txt | ||
/builds/ | ||
.DS_Store | ||
.DS_Store | ||
/archiver.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// archiver - ai.go | ||
// 2024-02-20 20:58 | ||
// Benny <[email protected]> | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
log "github.com/sirupsen/logrus" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type Response struct { | ||
Candidates []Candidate `json:"candidates"` | ||
} | ||
|
||
type Candidate struct { | ||
Content ContentData `json:"content"` | ||
} | ||
|
||
type ContentData struct { | ||
Parts []Part `json:"parts"` | ||
} | ||
|
||
type Part struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
type Content struct { | ||
Role string `json:"role"` | ||
Parts []Part `json:"parts"` | ||
} | ||
|
||
type Data struct { | ||
Contents []Content `json:"contents"` | ||
} | ||
|
||
func askAI(userID int64) string { | ||
var data Data | ||
chats := getChats(userID) | ||
if len(chats) > 0 { | ||
for _, chat := range chats { | ||
part := Part{Text: chat.Text} | ||
content := Content{ | ||
Role: chat.Role, | ||
Parts: []Part{part}, | ||
} | ||
data.Contents = append(data.Contents, content) | ||
} | ||
} | ||
|
||
jsonData, _ := json.Marshal(data) | ||
log.Infoln(string(jsonData)) | ||
resp, err := http.Post(geminiURL, "application/json", bytes.NewBuffer(jsonData)) | ||
defer resp.Body.Close() | ||
if err != nil || resp.StatusCode != http.StatusOK { | ||
body, _ := io.ReadAll(resp.Body) | ||
text := string(body) | ||
log.Errorf("Request failed, %s", text) | ||
return text | ||
} | ||
|
||
var response Response | ||
_ = json.NewDecoder(resp.Body).Decode(&response) | ||
return response.Candidates[0].Content.Parts[0].Text | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// archiver - database.go | ||
// 2024-02-20 20:00 | ||
// Benny <[email protected]> | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/glebarez/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var db, _ = gorm.Open(sqlite.Open("archiver.db"), &gorm.Config{}) | ||
|
||
type User struct { | ||
gorm.Model | ||
UserID int64 `gorm:"index;unique"` // 假设Telegram的用户ID是整数 | ||
Mode string // AI模式和普通模式 | ||
Link string | ||
Token string // Future use | ||
} | ||
|
||
type Chat struct { | ||
gorm.Model | ||
UserID int64 `gorm:"index"` // userID | ||
Role string // 发送者角色:model 或 user | ||
Text string // 保存的消息文本 | ||
} | ||
|
||
func getUser(userID int64) *User { | ||
var user User | ||
db.Where("user_id = ?", userID).First(&user) | ||
return &user | ||
} | ||
|
||
func enableAI(userID int64, link string) *User { | ||
user := getUser(userID) | ||
user.Mode = modeAI | ||
user.Link = link | ||
if user.UserID == 0 { | ||
// create new user | ||
user.UserID = userID | ||
db.Create(&user) | ||
} else { | ||
// update db | ||
db.Save(&user) | ||
} | ||
|
||
return user | ||
} | ||
|
||
func disableAI(userId int64) { | ||
// reset mode to normal, clear link | ||
user := getUser(userId) | ||
user.Mode = modeNormal | ||
user.Link = "" | ||
db.Save(&user) | ||
} | ||
|
||
func getChats(userID int64) []Chat { | ||
var chats []Chat | ||
db.Where("user_id = ?", userID).Find(&chats) | ||
return chats | ||
} | ||
|
||
func getChatsCount(userID int64) int64 { | ||
var count int64 | ||
db.Model(&Chat{}).Where("user_id = ?", userID).Count(&count) | ||
return count | ||
} | ||
|
||
func addChat(userId int64, role, text string) { | ||
chat := Chat{ | ||
UserID: userId, | ||
Role: role, | ||
Text: text, | ||
} | ||
db.Create(&chat) | ||
} | ||
|
||
func deleteChat(userID int64) int64 { | ||
// delete according to userID | ||
t := db.Where("user_id = ?", userID).Delete(&Chat{}) | ||
return t.RowsAffected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.