feat: add mathpixel
This commit is contained in:
52
internal/storage/dao/recognition_log.go
Normal file
52
internal/storage/dao/recognition_log.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RecognitionLogProvider 第三方服务提供商
|
||||
type RecognitionLogProvider string
|
||||
|
||||
const (
|
||||
ProviderMathpix RecognitionLogProvider = "mathpix"
|
||||
ProviderSiliconflow RecognitionLogProvider = "siliconflow"
|
||||
ProviderTexpixel RecognitionLogProvider = "texpixel"
|
||||
)
|
||||
|
||||
// RecognitionLog 识别调用日志表,记录第三方API调用请求和响应
|
||||
type RecognitionLog struct {
|
||||
BaseModel
|
||||
TaskID int64 `gorm:"column:task_id;bigint;not null;default:0;index;comment:关联任务ID" json:"task_id"`
|
||||
Provider RecognitionLogProvider `gorm:"column:provider;varchar(32);not null;comment:服务提供商" json:"provider"`
|
||||
RequestBody string `gorm:"column:request_body;type:longtext;comment:请求体" json:"request_body"`
|
||||
ResponseBody string `gorm:"column:response_body;type:longtext;comment:响应体" json:"response_body"`
|
||||
}
|
||||
|
||||
func (RecognitionLog) TableName() string {
|
||||
return "recognition_log"
|
||||
}
|
||||
|
||||
type RecognitionLogDao struct{}
|
||||
|
||||
func NewRecognitionLogDao() *RecognitionLogDao {
|
||||
return &RecognitionLogDao{}
|
||||
}
|
||||
|
||||
// Create 创建日志记录
|
||||
func (d *RecognitionLogDao) Create(tx *gorm.DB, log *RecognitionLog) error {
|
||||
return tx.Create(log).Error
|
||||
}
|
||||
|
||||
// GetByTaskID 根据任务ID获取日志
|
||||
func (d *RecognitionLogDao) GetByTaskID(tx *gorm.DB, taskID int64) ([]*RecognitionLog, error) {
|
||||
var logs []*RecognitionLog
|
||||
err := tx.Where("task_id = ?", taskID).Order("created_at DESC").Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
// GetByProvider 根据提供商获取日志
|
||||
func (d *RecognitionLogDao) GetByProvider(tx *gorm.DB, provider RecognitionLogProvider, limit int) ([]*RecognitionLog, error) {
|
||||
var logs []*RecognitionLog
|
||||
err := tx.Where("provider = ?", provider).Order("created_at DESC").Limit(limit).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
@@ -1,66 +1,16 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type JSON []byte
|
||||
|
||||
// ContentCodec 定义内容编解码接口
|
||||
type ContentCodec interface {
|
||||
Encode() (JSON, error)
|
||||
Decode() error
|
||||
GetContent() interface{} // 更明确的方法名
|
||||
}
|
||||
|
||||
type FormulaRecognitionContent struct {
|
||||
content JSON
|
||||
Latex string `json:"latex"`
|
||||
AdjustLatex string `json:"adjust_latex"`
|
||||
EnhanceLatex string `json:"enhance_latex"`
|
||||
}
|
||||
|
||||
func (c *FormulaRecognitionContent) Encode() (JSON, error) {
|
||||
b, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (c *FormulaRecognitionContent) Decode() error {
|
||||
return json.Unmarshal(c.content, c)
|
||||
}
|
||||
|
||||
// GetPreferredContent 按优先级返回公式内容
|
||||
func (c *FormulaRecognitionContent) GetContent() interface{} {
|
||||
c.Decode()
|
||||
if c.EnhanceLatex != "" {
|
||||
return c.EnhanceLatex
|
||||
} else if c.AdjustLatex != "" {
|
||||
return c.AdjustLatex
|
||||
} else {
|
||||
return c.Latex
|
||||
}
|
||||
}
|
||||
|
||||
type RecognitionResult struct {
|
||||
BaseModel
|
||||
TaskID int64 `gorm:"column:task_id;bigint;not null;default:0;comment:任务ID" json:"task_id"`
|
||||
TaskType TaskType `gorm:"column:task_type;varchar(16);not null;comment:任务类型;default:''" json:"task_type"`
|
||||
Content JSON `gorm:"column:content;type:json;not null;comment:识别内容" json:"content"`
|
||||
}
|
||||
|
||||
// NewContentCodec 创建对应任务类型的内容编解码器
|
||||
func (r *RecognitionResult) NewContentCodec() ContentCodec {
|
||||
switch r.TaskType {
|
||||
case TaskTypeFormula:
|
||||
return &FormulaRecognitionContent{content: r.Content}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
Latex string `json:"latex"`
|
||||
Markdown string `json:"markdown"` // Mathpix Markdown 格式
|
||||
MathML string `json:"mathml"` // MathML 格式
|
||||
}
|
||||
|
||||
type RecognitionResultDao struct {
|
||||
|
||||
Reference in New Issue
Block a user