|
- package models
-
- import (
- "code.gitea.io/gitea/modules/timeutil"
- "fmt"
- )
-
- type LlmChat struct {
- ID string `xorm:"pk"`
- UserId int64 `xorm:"INDEX"`
- Count int
- Prompt string `xorm:"text"`
- Answer string `xorm:"text"`
- InvalidCount int
- InvalidType string
- InvalidTool string
- InvalidDetail string `xorm:"text"`
- ChatStatus int
- ModelName string
- ChatType string
- ChatId string
- KnowledgeBaseName string
- VectorStoreType string
- EmbeddingModel string
- Endpoint string
- CreatedUnix timeutil.TimeStamp `xorm:"created"`
- UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
- }
-
- func SaveChat(llmChat *LlmChat) error {
- sess := xStatistic.NewSession()
- defer sess.Close()
- re, err := sess.Insert(llmChat)
- if err != nil {
- fmt.Printf("insert llmChat error %s\n", err.Error())
- return err
- }
- fmt.Printf("success to save llmChat db.re=%+v\n", fmt.Sprint(re))
- return nil
- }
-
- func QueryChatCount(userId int64, modelName string) int64 {
- sess := xStatistic.NewSession()
- defer sess.Close()
- query := "SELECT SUM(count) AS count FROM public.llm_chat WHERE chat_status = 1 AND user_id = ? AND model_name = ?"
- sumList, err := sess.QueryInterface(query, userId, modelName)
- if err == nil {
- if len(sumList) == 1 {
- return convertInterfaceToInt64(sumList[0]["count"])
- }
- }
- return 0
- }
-
- func QueryInvalidPromptCount(userId int64) int64 {
- sess := xStatistic.NewSession()
- defer sess.Close()
- query := "SELECT SUM(invalid_count) AS count FROM public.llm_chat WHERE invalid_type='prompt' and user_id = ?"
- sumList, err := sess.QueryInterface(query, userId)
- if err == nil {
- if len(sumList) == 1 {
- return convertInterfaceToInt64(sumList[0]["count"])
- }
- }
- return 0
- }
-
- func QueryChatStatistics() ([]map[string]interface{}, error) {
- sess := xStatistic.NewSession()
- query := `
- SELECT
- COALESCE(model_name, 'total') as model_name,
- COUNT(DISTINCT id) as chat,
- COUNT(DISTINCT user_id) as chat_user,
- COUNT(DISTINCT id) / COUNT(DISTINCT user_id) as chat_per_user_avg,
- COUNT(DISTINCT CASE WHEN chat_type = 'llm' THEN id END) as chat_llm,
- COUNT(DISTINCT CASE WHEN chat_type = 'llm' THEN user_id END) as chat_llm_user,
- COUNT(DISTINCT CASE WHEN chat_type = 'kb' THEN id END) as chat_kb,
- COUNT(DISTINCT CASE WHEN chat_type = 'kb' THEN user_id END) as chat_kb_user,
- COALESCE(SUM(invalid_count),0) as chat_illegal,
- COALESCE(SUM(CASE WHEN invalid_type = 'prompt' THEN 1 END),0) as chat_illegal_prompt,
- COALESCE(SUM(CASE WHEN invalid_type = 'answer' THEN 1 END),0) as chat_illegal_answer
- FROM
- llm_chat
- GROUP BY
- ROLLUP(model_name)`
-
- results, err := sess.SQL(query).QueryInterface()
- if err != nil {
- return nil, err
- }
-
- return results, nil
- }
|