@@ -24,15 +24,11 @@ class Gemma(SimpleService): 'output': Decimal('40'), } - TOOLS_TOKEN_COSTS = {'text-embedding-3-large': {'output': Decimal('0.000065')}} - - def calculate_price(self, input_tokens: int, output_tokens: int, embedding_tokens: int) -> Decimal: + def calculate_price(self, input_tokens: int, output_tokens: int) -> Decimal: price = ( input_tokens * self.TOKENS_COST['input'] / 1_000_000 + output_tokens * self.TOKENS_COST['output'] / 1_000_000 ) - if embedding_tokens > 0: - price += self.TOOLS_TOKEN_COSTS['text-embedding-3-large']['output'] * embedding_tokens return price.quantize(Decimal('0.01'), rounding='ROUND_UP') def save_results(self, content: str, t: timedelta, save: bool = True) -> list[Message]: @@ -54,45 +50,16 @@ class Gemma(SimpleService): } messages = self.get_chat_history() messages.append({'role': 'user', 'content': input_message.content}) - embedding_tokens = 0 if input_message.file: - file_service = FileProcessingService - file_bytes = input_message.file.read() - kind = filetype.guess(file_bytes[:20]) - raw_file_extension = kind.extension - file_extension = file_service.get_file_extension(raw_file_extension, file_bytes) - if file_extension in ('pdf', 'doc', 'docx', 'xlsx'): - text = file_service.get_file_data(file_extension, file_bytes) - chunks = EmbeddingService.split_text_to_chunks(text) - if len(text) > 20_000: - for proxy in Proxy.objects.all(): - document_name = chunks[0].partition(f':{chr(10)}')[2].split(f'{chr(10)}')[0][:100] - embedding_tokens, file_data = EmbeddingService.get_large_file_data( - self.store.messages.first().pk, chunks, proxy, input_message.content - ) - messages[-1]['content'] = EmbeddingService.make_embeddings_prompt( - document_name=document_name, - section_texts=file_data, - question=input_message.content, - ) - else: - messages[-1]['content'] = ( - f'Используй системный промпт. Содержание файла: ' - f'{chunks}. Вопрос: {input_message.content}' - ) - else: - kind = filetype.guess(file_bytes[:20]) - mime = kind.mime if kind else 'application/octet-stream' - normalized_image = Image.open(input_message.file) - format = 'jpeg' if kind.extension == 'jpg' else kind.extension - buf = BytesIO() - normalized_image.save(buf, format=format) - image_url = f'data:{mime};base64,{base64.b64encode(buf.getvalue()).decode("utf-8")}' - buf.close() - messages[-1]['content'] = [ - {'type': 'text', 'text': input_message.content}, - {'type': 'image_url', 'image_url': {'url': image_url}}, - ] + kind = filetype.guess(input_message.file.read(20)) + mime = kind.mime if kind else 'application/octet-stream' + input_message.file.seek(0) + image_url = f'data:{mime};base64,{base64.b64encode(input_message.file.read()).decode("utf-8")}' + input_message.file.close() + messages[-1]['content'] = [ + {'type': 'text', 'text': input_message.content}, + {'type': 'image_url', 'image_url': {'url': image_url}}, + ] start_time = time.time() result = openrouter_run('google/gemma-3-4b-it', messages, callback_data, 'Gemma') process_time = timedelta(seconds=(time.time() - start_time)) @@ -100,7 +67,6 @@ class Gemma(SimpleService): input_message.content_object.model, input_tokens=result[1], output_tokens=result[2], - embedding_tokens=embedding_tokens, ) msgs = self.save_results(result[0], process_time) return msgs @@ -2,18 +2,13 @@ import base64 import time from datetime import timedelta from decimal import Decimal -from io import BytesIO from typing import Any, Iterator import filetype -from PIL import Image from messages.models import Message -from ml_model.services.EmbeddingService import EmbeddingService -from ml_model.services.FileService import FileProcessingService from ml_model.services.base import SimpleService from ml_model.tasks import openrouter_run -from poller.models import Proxy from tools.chats.models import Chat from tools.copywrite.models import Copywrite from tools.public_api.models import APIStore @@ -22,15 +17,11 @@ from tools.public_api.models import APIStore class Grok_4_1_Fast(SimpleService): TOKENS_COST = {'input': Decimal('40'), 'output': Decimal('100')} - TOOLS_TOKEN_COSTS = {'text-embedding-3-large': {'output': Decimal('0.000065')}} - - def calculate_price(self, input_tokens: int, output_tokens: int, embedding_tokens: int) -> Decimal: + def calculate_price(self, input_tokens: int, output_tokens: int) -> Decimal: price = ( input_tokens * self.TOKENS_COST['input'] / 1_000_000 + output_tokens * self.TOKENS_COST['output'] / 1_000_000 ) - if embedding_tokens > 0: - price += self.TOOLS_TOKEN_COSTS['text-embedding-3-large']['output'] * embedding_tokens return price.quantize(Decimal('0.01'), rounding='ROUND_UP') def save_results(self, content: Iterator[Any], t: timedelta, save: bool = True) -> list[Message]: @@ -52,45 +43,16 @@ class Grok_4_1_Fast(SimpleService): } messages = self.get_chat_history() messages.append({'role': 'user', 'content': input_message.content}) - embedding_tokens = 0 if input_message.file: - file_service = FileProcessingService - file_bytes = input_message.file.read() - kind = filetype.guess(file_bytes[:20]) - raw_file_extension = kind.extension - file_extension = file_service.get_file_extension(raw_file_extension, file_bytes) - if file_extension in ('pdf', 'doc', 'docx', 'xlsx'): - text = file_service.get_file_data(file_extension, file_bytes) - chunks = EmbeddingService.split_text_to_chunks(text) - if len(text) > 20_000: - for proxy in Proxy.objects.all(): - document_name = chunks[0].partition(f':{chr(10)}')[2].split(f'{chr(10)}')[0][:100] - embedding_tokens, file_data = EmbeddingService.get_large_file_data( - self.store.messages.first().pk, chunks, proxy, input_message.content - ) - messages[-1]['content'] = EmbeddingService.make_embeddings_prompt( - document_name=document_name, - section_texts=file_data, - question=input_message.content, - ) - else: - messages[-1]['content'] = ( - f'Используй системный промпт. Содержание файла: ' - f'{chunks}. Вопрос: {input_message.content}' - ) - else: - kind = filetype.guess(file_bytes[:20]) - mime = kind.mime if kind else 'application/octet-stream' - normalized_image = Image.open(input_message.file) - format = 'jpeg' if kind.extension == 'jpg' else kind.extension - buf = BytesIO() - normalized_image.save(buf, format=format) - image_url = f'data:{mime};base64,{base64.b64encode(buf.getvalue()).decode("utf-8")}' - buf.close() - messages[-1]['content'] = [ - {'type': 'text', 'text': input_message.content}, - {'type': 'image_url', 'image_url': {'url': image_url}}, - ] + kind = filetype.guess(input_message.file.read(20)) + mime = kind.mime if kind else 'application/octet-stream' + input_message.file.seek(0) + image_url = f'data:{mime};base64,{base64.b64encode(input_message.file.read()).decode("utf-8")}' + input_message.file.close() + messages[-1]['content'] = [ + {'type': 'text', 'text': input_message.content}, + {'type': 'image_url', 'image_url': {'url': image_url}}, + ] start_time = time.time() result = openrouter_run('x-ai/grok-4.1-fast', messages, callback_data, 'Grok 4.1 Fast') process_time = timedelta(seconds=(time.time() - start_time)) @@ -98,7 +60,6 @@ class Grok_4_1_Fast(SimpleService): input_message.content_object.model, input_tokens=result[1], output_tokens=result[2], - embedding_tokens=embedding_tokens, ) msgs = self.save_results(result[0], process_time) return msgs