@@ -18,8 +18,14 @@ class Qwen(SimpleService): """ TOKENS_COST = { - 'qwq-32b': {'input': Decimal('36'), 'output': Decimal('36')}, # 1M tokens - 'qwq-32b:free': {'input': Decimal('0'), 'output': Decimal('0')}, # 1M tokens + 'qwq-32b': { + 'input': Decimal('36'), + 'output': Decimal('36') + }, # 1M tokens + 'qwq-32b:free': { + 'input': Decimal('0'), + 'output': Decimal('0') + }, # 1M tokens } def calculate_price(self, version: str, input_tokens: int, output_tokens: int) -> Decimal: @@ -44,17 +50,19 @@ class Qwen(SimpleService): def make(self, input_message: Message, save: bool = True) -> list[Message]: start_time = time.time() version = f'qwen/{input_message.info.pop("version", "qwq-32b")}' - callback_data = {'provider': {'order': ['DeepInfra']}, **input_message.info} + callback_data = {'provider': {'order': ['DeepInfra']}, 'max_tokens': 10000, **input_message.info} messages = self.get_chat_history() messages.insert( 0, { 'role': 'system', 'content': ( - 'Отвечай строго на том языке, на котором к тебе обратились или прямо указали, на каком языке отвечать. ' - 'Если язык невозможно однозначно определить или сообщение не несёт явного смысла на этом языке — используй русский язык. ' - 'Если сообщение не несёт очевидного смысла вообще — ответь, что не знаешь, как помочь. ' - 'Не уходи в длительные рассуждения и отвечай максимально чётко и по делу.' + 'The internal reasoning and processing of the request must also be in Russian! It is forbidden to switch to English or other languages!\n' + 'If the language cannot be clearly identified or the message does not make explicit sense in that language, use STRICTLY Russian.\n' + 'Answer STRICTLY in the language in which you were addressed or directly indicated in which language to respond. Think and analyze in Russian.\n' + 'If the message doesnt make any obvious sense at all, reply that you dont know how to help.\n' + 'I FORBID you to go into lengthy discussions, you must answer AS clearly and to the point as possible.\n' + 'Write your arguments STRICTLY in UTF-8 and in the language in which you applied!' ) } ) @@ -112,7 +112,6 @@ def replicate_run(callback_url: str, payload: dict[str, Any]): input=payload, ) - @shared_task def openrouter_run(version: str, messages: list, callback_data: dict, model_name: str): for proxy in Proxy.objects.all(): @@ -129,9 +128,18 @@ def openrouter_run(version: str, messages: list, callback_data: dict, model_name if ( (data := resp.json()) and data.get('choices') - and (content := ','.join([choice['message']['content'] for choice in data.get('choices')])) ): - return (content, data['usage']['prompt_tokens'], data['usage']['completion_tokens']) + content = ','.join(choice['message']['content'] for choice in data.get('choices')) + reasoning = ','.join( + reasoning for choice in data.get('choices', []) + if (reasoning := choice['message'].get('reasoning')) is not None + ) + answer = reasoning + if reasoning and content: + answer = f'Рассуждение:\n{reasoning}\nОсновная мысль:\n{content}' + elif content: + answer = content + return answer, data['usage']['prompt_tokens'], data['usage']['completion_tokens'] logger.error(f'Error occured via model {model_name}. Data: {resp.content}') raise Exception(f'No answer from {model_name}, please retry later') @@ -145,21 +153,6 @@ def upscale_run(payload: dict[str, tuple[str, IO]]) -> list[str]: return json.loads(content) -@shared_task -def mistral_run(payload: dict[str, Any]): - headers = { - 'Content-Type': 'application/json', - 'Authorization': settings.MISTRAL_API_KEY, - } - return json.loads( - requests.post( - 'https://api.mistral.ai/v1/chat/completions', - json.dumps(payload), - headers=headers, - ).content - ) - - @shared_task def claude_run(payload: dict[str, Any]): headers = {