support token calculate
This commit is contained in:
1 parent
dd3a784cb5
commit
6cb7ca9902
4 files changed
+47
-8
No files matched your search
@@ -93,6 +93,8 @@ class BillingMessageHandler:
|
||||
input_params=message_data["input_params"],
|
||||
call_success=message_data["call_success"],
|
||||
unit_price=Decimal(message_data["unit_price"]),
|
||||
input_token=Decimal(message_data["input_token"]),
|
||||
output_token=Decimal(message_data["output_token"]),
|
||||
call_start_time=call_start_time,
|
||||
call_end_time=call_end_time,
|
||||
apikey_id=message_data.get("apikey_id"), # Support older version messages that don't have this field
|
||||
@@ -184,6 +186,7 @@ class BillingMessageHandler:
|
||||
status=1, # Completed
|
||||
transaction_id=call_log_id,
|
||||
channel_user_id=None,
|
||||
callback_data=json.dumps(billing_message),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
@@ -104,7 +104,15 @@ class BillingService:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Pre-deduction check failed - User ID: {user_id}, Service ID: {service_id}: {str(e)}", exc_info=True)
|
||||
return PreDeductResult(success=False, message=f"System error: {str(e)}", service_price=Decimal("0"), user_balance=Decimal("0"))
|
||||
return PreDeductResult(
|
||||
success=False,
|
||||
message=f"System error: {str(e)}",
|
||||
service_price=Decimal("0"),
|
||||
user_balance=Decimal("0"),
|
||||
input_token_price=Decimal("0"),
|
||||
output_token_price=Decimal("0"),
|
||||
charge_type=ChargeType.FREE.value
|
||||
)
|
||||
|
||||
async def send_billing_message(self, call_log: ApiCallLogInfo, call_success: bool, call_end_time: datetime) -> None:
|
||||
"""
|
||||
@@ -166,7 +174,6 @@ class BillingService:
|
||||
# Try to get from Redis cache
|
||||
cache_key = f"service:price:{service_id}"
|
||||
cached_data = self.redis.get(cache_key)
|
||||
|
||||
if cached_data:
|
||||
try:
|
||||
data = json.loads(cached_data)
|
||||
|
||||
@@ -145,7 +145,21 @@ class McpServerFactory:
|
||||
db = next(get_db())
|
||||
call_success = False
|
||||
result: List[types.ContentBlock] = []
|
||||
|
||||
# calculate input token amount
|
||||
input_token_amount = Decimal("0")
|
||||
output_token_amount = Decimal("0")
|
||||
input_token = 0
|
||||
output_token = 0
|
||||
|
||||
# Calculate input token amount based on charge type
|
||||
if pre_deduct_result.charge_type == "per_token":
|
||||
# Estimate input tokens from arguments
|
||||
input_text = json.dumps(arguments)
|
||||
estimated_input_tokens = len(input_text.split()) * 1.3 # Rough estimation: 1.3 tokens per word
|
||||
input_token = estimated_input_tokens
|
||||
# Calculate input token amount (prices are per million tokens)
|
||||
input_token_amount = (Decimal(str(estimated_input_tokens)) / Decimal("1000000")) * pre_deduct_result.input_token_price
|
||||
|
||||
try:
|
||||
# Create service instance
|
||||
mcp_service = self._create_mcp_service(db)
|
||||
@@ -167,6 +181,17 @@ class McpServerFactory:
|
||||
result = await self.tool_service.execute_tool(tool_config, arguments, auth_info)
|
||||
call_success = True
|
||||
logger.info(f"Tool call successful - User ID: {user_id}, Tool: {name}")
|
||||
|
||||
# Calculate output token amount after tool execution
|
||||
if pre_deduct_result.charge_type == "per_token" and result:
|
||||
output_text = ""
|
||||
for content in result:
|
||||
if isinstance(content, types.TextContent):
|
||||
output_text += content.text
|
||||
estimated_output_tokens = len(output_text.split()) * 1.3 # Rough estimation: 1.3 tokens per word
|
||||
output_token = estimated_output_tokens
|
||||
# Calculate output token amount (prices are per million tokens)
|
||||
output_token_amount = (Decimal(str(estimated_output_tokens)) / Decimal("1000000")) * pre_deduct_result.output_token_price
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Tool call failed - User ID: {user_id}, Tool: {name}: {str(e)}", exc_info=True)
|
||||
@@ -186,9 +211,9 @@ class McpServerFactory:
|
||||
api_id=call_log_id,
|
||||
tool_name=name,
|
||||
input_params=json.dumps(arguments),
|
||||
# unit_price=pre_deduct_result.service_price,
|
||||
input_token_amount=0,
|
||||
output_token_amount=0,
|
||||
unit_price=input_token_amount + output_token_amount,
|
||||
input_token=input_token,
|
||||
output_token=output_token,
|
||||
call_start_time=call_start_time,
|
||||
call_end_time=call_end_time,
|
||||
apikey_id=apikey_id,
|
||||
|
||||
@@ -15,6 +15,9 @@ class BillingMessage:
|
||||
input_params: str
|
||||
call_success: bool
|
||||
unit_price: Decimal
|
||||
input_token: Decimal
|
||||
output_token: Decimal
|
||||
charge_type: str
|
||||
call_start_time: datetime
|
||||
call_end_time: Optional[datetime] = None
|
||||
call_log_id: Optional[str] = None
|
||||
@@ -29,8 +32,9 @@ class ApiCallLogInfo:
|
||||
tool_name: str
|
||||
input_params: str
|
||||
unit_price: Decimal
|
||||
input_token_amount: Decimal
|
||||
output_token_amount: Decimal
|
||||
input_token: Decimal
|
||||
output_token: Decimal
|
||||
charge_type: str
|
||||
call_start_time: datetime
|
||||
call_end_time: Optional[datetime] = None
|
||||
apikey_id: Optional[str] = None
|
||||
|
||||
Reference in new issue
Block a user