@@ -9,7 +9,6 @@ from django.conf import settings from messages.models import Message 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 @@ -25,7 +24,8 @@ class Perplexity(SimpleService): TOKENS_COST = { 'sonar': { 'input': Decimal('300'), - 'output': Decimal('300') + 'output': Decimal('300'), + 'search': Decimal('1500') }, # 1M tokens 'sonar-deep-research': { 'input': Decimal('600'), # 1M tokens @@ -44,10 +44,10 @@ class Perplexity(SimpleService): def calculate_price(self, version: str, input_tokens: int, output_tokens: int) -> Decimal: price_map = self.TOKENS_COST[version.split('/')[1]] price = ( - input_tokens * price_map['input'] / 1_000_000 + output_tokens * price_map['output'] / 1_000_000 + input_tokens * price_map['input'] / 1_000_000 + + output_tokens * price_map['output'] / 1_000_000 + + price_map['search'] / 1_000 ) - if version == 'perplexity/sonar-pro-search': - price += price_map['search'] / 1_000 return price.quantize(Decimal('0.1'), rounding='ROUND_UP') def save_results(self, content: Iterator[Any], t: timedelta, save: bool = True) -> list[Message]: @@ -68,38 +68,35 @@ class Perplexity(SimpleService): callback_data = {'provider': {'order': ['Perplexity']}, **input_message.info} messages = self.get_chat_history() messages.append({'role': 'user', 'content': input_message.content}) - if version == 'perplexity/sonar-pro-search': - try: - for proxy in Proxy.objects.all(): - response = httpx.post( - url='https://openrouter.ai/api/v1/chat/completions', headers={'Authorization': f'Bearer {settings.OPENROUTER_API_KEY}'}, - proxy=f'{proxy.protocol}://{proxy.address}', timeout=600, - json={'model': version, 'messages': messages, **callback_data} - ) - if response.status_code not in (200, 201): - raise - response = response.json() - annotations = response['choices'][0]['message'].get('annotations', []) - content = response['choices'][0]['message']['content'] - if annotations: - content = ( - re.sub( - r'\[(\d+)\]', - lambda m: f' [[{m.group(1)}]]({str(annotations[int(m.group(1)) - 1]["url_citation"]["url"])})', - content - ) - + f'\n\n### Ресурсы:\n{"\n".join( - [ - f'{num}. {a["url_citation"]["title"]} ({a["url_citation"]["url"]})' - for num, a in enumerate(annotations, start=1) - ] - )}' + try: + for proxy in Proxy.objects.all(): + response = httpx.post( + url='https://openrouter.ai/api/v1/chat/completions', headers={'Authorization': f'Bearer {settings.OPENROUTER_API_KEY}'}, + proxy=f'{proxy.protocol}://{proxy.address}', timeout=600, + json={'model': version, 'messages': messages, **callback_data} + ) + if response.status_code not in (200, 201): + raise + response = response.json() + annotations = response['choices'][0]['message'].get('annotations', []) + content = response['choices'][0]['message']['content'] + if annotations: + content = ( + re.sub( + r'\[(\d+)\]', + lambda m: f' [[{m.group(1)}]]({str(annotations[int(m.group(1)) - 1]["url_citation"]["url"])})', + content ) - result = [content, response['usage']['prompt_tokens'], response['usage']['completion_tokens']] - except Exception: - raise Exception(f'No answer from Perplexity, please retry later') - else: - result = openrouter_run(version, messages, callback_data, 'Perplexity') + + f'\n\n### Ресурсы:\n{"\n".join( + [ + f'{num}. {a["url_citation"]["title"]} ({a["url_citation"]["url"]})' + for num, a in enumerate(annotations, start=1) + ] + )}' + ) + result = [content, response['usage']['prompt_tokens'], response['usage']['completion_tokens']] + except Exception: + raise Exception(f'No answer from Perplexity, please retry later') process_time = timedelta(seconds=(time.time() - start_time)) self.handle_invoice( input_message.content_object.model,