feat: add ai cost tracking foundation
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.models import Base
|
||||
from app.models.ai_config import ModelConfig
|
||||
from app.models.logs import AiRequestLog
|
||||
from app.services.ai_request_log_service import AiRequestLogService
|
||||
|
||||
|
||||
def _db() -> Session:
|
||||
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
Base.metadata.create_all(engine)
|
||||
return Session(engine)
|
||||
|
||||
|
||||
def test_ai_request_log_estimates_cost_from_model_price():
|
||||
with _db() as db:
|
||||
model = ModelConfig(
|
||||
id=1,
|
||||
provider="test",
|
||||
api_type="openai_compatible",
|
||||
model_name="test-model",
|
||||
api_url="https://example.com",
|
||||
api_key="secret",
|
||||
input_price_per_1k=Decimal("0.002"),
|
||||
output_price_per_1k=Decimal("0.006"),
|
||||
currency="CNY",
|
||||
timeout_second=30,
|
||||
)
|
||||
db.add(model)
|
||||
db.commit()
|
||||
|
||||
AiRequestLogService.write_success(
|
||||
db,
|
||||
session_id=1,
|
||||
message_id=2,
|
||||
user_id=3,
|
||||
model_id=1,
|
||||
model_name="test-model",
|
||||
prompt="hello",
|
||||
knowledge_ids="1",
|
||||
retrieve_count=2,
|
||||
input_token=1000,
|
||||
output_token=500,
|
||||
cost_ms=120,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
log = db.query(AiRequestLog).one()
|
||||
assert log.estimated_cost == Decimal("0.005000")
|
||||
assert log.currency == "CNY"
|
||||
assert log.knowledge_hit == 1
|
||||
assert log.question_type == "knowledge_grounded"
|
||||
assert "命中知识库" in (log.route_reason or "")
|
||||
|
||||
Reference in New Issue
Block a user