Files
ai_api_web/backend/models/model.py
2026-01-22 18:26:47 +08:00

65 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from models import db
from datetime import datetime
class Model(db.Model):
__tablename__ = 'models'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False, unique=True, index=True)
provider = db.Column(db.String(100), nullable=False, index=True) # Anthropic, OpenAI等
description = db.Column(db.Text, nullable=True)
input_price = db.Column(db.Numeric(10, 6), nullable=False) # 输入价格 per 1M tokens
output_price = db.Column(db.Numeric(10, 6), nullable=False) # 输出价格 per 1M tokens
billing_type = db.Column(db.String(50), default='pay_as_you_go') # pay_as_you_go, per_request
endpoint_type = db.Column(db.String(50), default='anthropic') # anthropic, openai
tags = db.Column(db.Text, nullable=True) # JSON字符串存储标签数组
available_groups = db.Column(db.Text, nullable=True) # JSON字符串存储可用分组
multiplier = db.Column(db.Numeric(5, 2), default=1.00) # 倍率
is_active = db.Column(db.Boolean, default=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
def to_dict(self, show_recharge_price=False, show_multiplier=False):
"""转换为字典"""
import json
tags = []
if self.tags:
try:
tags = json.loads(self.tags)
except:
tags = []
available_groups = []
if self.available_groups:
try:
available_groups = json.loads(self.available_groups)
except:
available_groups = []
result = {
'id': self.id,
'name': self.name,
'provider': self.provider,
'description': self.description,
'input_price': float(self.input_price),
'output_price': float(self.output_price),
'billing_type': self.billing_type,
'billing_type_display': '按量计费' if self.billing_type == 'pay_as_you_go' else '按次计费',
'endpoint_type': self.endpoint_type,
'tags': tags,
'available_groups': available_groups,
'is_active': self.is_active,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
if show_recharge_price:
# 显示充值价格假设充值比例0.65
result['recharge_input_price'] = float(self.input_price) * 0.65
result['recharge_output_price'] = float(self.output_price) * 0.65
if show_multiplier:
result['multiplier'] = float(self.multiplier)
return result