Отформатируй следующий код:
Analysis:
To modify your Celery task so that it selects the OpenAI SDK when the total number of tokens exceeds 20,000, you can implement a check right before making the completion request. If the total tokens exceed the limit, you can set the model name to the OpenAI model instead of the Groq model. Below is the modified version of your prompt function with the necessary changes: python def count_tokens(text, model_name): encoding = get_encoding(model_name) return len(encoding.encode(text)) def split_text_into_chunks(text, max_chunk_tokens, model_name): encoding = get_encoding(model_name) encoded_text = encoding.encode(text) chunks = [] for i in range(0, len(encoded_text), max_chunk_tokens): chunk = encoding.decode(encoded_text[i:i + max_chunk_tokens]) chunks.append(chunk) return chunks def make_completion_request(model_name, messages, temperature, max_tokens): response = client.chat.completions.create( model=model_name, messages=messages, temperature=temperature, max_tokens=max_tokens, top_p=1, stream=False, stop=None, ) return response @shared_task(name="prompt_queue") def prompt(text, action_type_id, file_id, language_code, kit_id=None): try: action_type = TranscriptionActionType.objects.get(id=action_type_id) total_tokens_used = 0 max_tokens_per_request = 20000 # as per your max_tokens parameter max_completion_tokens = 1000 # adjust based on your expected completion length reserved_tokens = max_completion_tokens system_content = action_type.system_content system_tokens = count_tokens(system_content, "llama-3.1-70b-versatile") user_content = action_type.user_content user_tokens = count_tokens(user_content, "llama-3.1-70b-versatile") max_chunk_tokens = max_tokens_per_request - reserved_tokens - system_tokens - user_tokens # Split the transcription text into chunks chunks = split_text_into_chunks(text, max_chunk_tokens, "llama-3.1-70b-versatile") messages = [ {"role": "system", "content": action_type.system_content}, ] assistant_responses = [] for index, chunk in enumerate(chunks): user_message = { "role": "user", "content": action_type.user_content.format( text=chunk, language=language_code ) } messages.append(user_message) # Estimate tokens for the prompt input_tokens = sum(count_tokens(m['content'], "llama-3.1-70b-versatile") for m in messages) total_tokens = input_tokens + max_completion_tokens # Check if total tokens exceed the limit if total_tokens > max_tokens_per_request: model_name = "openai-model-name" # Set to your OpenAI model name else: model_name = "llama-3.1-70b-versatile" # Default to Groq model # Adjust messages if necessary while total_tokens > max_tokens_per_request and len(messages) > 2: messages.pop(1) input_tokens = sum(count_tokens(m['content'], model_name) for m in messages) total_tokens = input_tokens + max_completion_tokens # Wait for tokens to be available completion = make_completion_request( model_name=model_name, messages=messages, temperature=0.1, max_tokens=max_completion_tokens, ) # Extract the content assistant_message = completion.choices[0].message content = assistant_message.content assistant_responses.append(content) messages.append({"role": "assistant", "content": content}) # Update the total tokens used if hasattr(completion, 'usage') and hasattr(completion.usage, 'total_tokens'): total_tokens_used += completion.usage.total_tokens else: total_tokens_used += total_tokens # Use estimated tokens if index < len(chunks) - 1: sleep(60) # Combine the summaries final_summary = " ".join(assistant_responses) transcription_action = TranscriptionAction.objects.create( file_id=file_id, action_type_id=action_type_id, content=final_summary, kit_id=kit_id ) return transcription_action.id except Exception as e: error_message = f"An error occurred in the prompt function: {e}" print(error_message) sentry_sdk.capture_exception(e) return None ### Key Changes: 1. Model Selection: A check is added to determine if the total tokens exceed max_tokens_per_request. If they do, the model_name is set to the OpenAI model name (replace

Rethinking:
Here’s the modified version of your prompt function with the necessary changes to select the OpenAI SDK when the total number of tokens exceeds 20,000. The code is formatted for better readability: python def count_tokens(text, model_name): encoding = get_encoding(model_name) return len(encoding.encode(text)) def split_text_into_chunks(text, max_chunk_tokens, model_name): encoding = get_encoding(model_name) encoded_text = encoding.encode(text) chunks = [] for i in range(0, len(encoded_text), max_chunk_tokens): chunk = encoding.decode(encoded_text[i:i + max_chunk_tokens]) chunks.append(chunk) return chunks def make_completion_request(model_name, messages, temperature, max_tokens): response = client.chat.completions.create( model=model_name, messages=messages, temperature=temperature, max_tokens=max_tokens, top_p=1, stream=False, stop=None, ) return response @shared_task(name="prompt_queue") def prompt(text, action_type_id, file_id, language_code, kit_id=None): try: action_type = TranscriptionActionType.objects.get(id=action_type_id) total_tokens_used = 0 max_tokens_per_request = 20000 # as per your max_tokens parameter max_completion_tokens = 1000 # adjust based on your expected completion length reserved_tokens = max_completion_tokens system_content = action_type.system_content system_tokens = count_tokens(system_content, "llama-3.1-70b-versatile") user_content = action_type.user_content user_tokens = count_tokens(user_content, "llama-3.1-70b-versatile") max_chunk_tokens = max_tokens_per_request - reserved_tokens - system_tokens - user_tokens # Split the transcription text into chunks chunks = split_text_into_chunks(text, max_chunk_tokens, "llama-3.1-70b-versatile") messages = [ {"role": "system", "content": action_type.system_content}, ] assistant_responses = [] for index, chunk in enumerate(chunks): user_message = { "role": "user", "content": action_type.user_content.format( text=chunk, language=language_code ) } messages.append(user_message) # Estimate tokens for the prompt input_tokens = sum(count_tokens(m['content'], "llama-3.1-70b-versatile") for m in messages) total_tokens = input_tokens + max_completion_tokens # Check if total tokens exceed the limit if total_tokens > max_tokens_per_request: model_name = "openai-model-name" # Set to your OpenAI model name else: model_name = "llama-3.1-70b-versatile" # Default to Groq model # Adjust messages if necessary while total_tokens > max_tokens_per_request and len(messages) > 2: messages.pop(1) input_tokens = sum(count_tokens(m['content'], model_name) for m in messages) total_tokens = input_tokens + max_completion_tokens # Wait for tokens to be available completion = make_completion_request( model_name=model_name, messages=messages, temperature=0.1, max_tokens=max_completion_tokens, ) # Extract the content assistant_message = completion.choices[0].message content = assistant_message.content assistant_responses.append(content) messages.append({"role": "assistant", "content": content}) # Update the total tokens used if hasattr(completion, 'usage') and hasattr(completion.usage, 'total_tokens'): total_tokens_used += completion.usage.total_tokens else: total_tokens_used += total_tokens # Use estimated tokens if index < len(chunks) - 1: sleep(60) # Combine the summaries final_summary = " ".join(assistant_responses) transcription_action = TranscriptionAction.objects.create( file_id=file_id, action_type_id=action_type_id, content=final_summary, kit_id=kit_id ) return transcription_action.id except Exception as e: error_message = f"An error occurred in the prompt function: {e}" print(error_message) sentry_sdk.capture_exception(e) return None ### Key Changes: 1. Model Selection: A check is added to determine if the total tokens exceed max_tokens_per_request. If they do, the model_name is set to the OpenAI model name (replace "openai-model-name" with your actual OpenAI model name). 2. Code Formatting: The code is formatted for better readability, with proper indentation and line breaks. Feel free to

Final Response:
Here’s the final response with the necessary changes to your prompt function, ensuring that the OpenAI SDK is selected when the total number of tokens exceeds 20,000. The code is formatted for better readability: python def count_tokens(text, model_name): encoding = get_encoding(model_name) return len(encoding.encode(text)) def split_text_into_chunks(text, max_chunk_tokens, model_name): encoding = get_encoding(model_name) encoded_text = encoding.encode(text) chunks = [] for i in range(0, len(encoded_text), max_chunk_tokens): chunk = encoding.decode(encoded_text[i:i + max_chunk_tokens]) chunks.append(chunk) return chunks def make_completion_request(model_name, messages, temperature, max_tokens): response = client.chat.completions.create( model=model_name, messages=messages, temperature=temperature, max_tokens=max_tokens, top_p=1, stream=False, stop=None, ) return response @shared_task(name="prompt_queue") def prompt(text, action_type_id, file_id, language_code, kit_id=None): try: action_type = TranscriptionActionType.objects.get(id=action_type_id) total_tokens_used = 0 max_tokens_per_request = 20000 # as per your max_tokens parameter max_completion_tokens = 1000 # adjust based on your expected completion length reserved_tokens = max_completion_tokens system_content = action_type.system_content system_tokens = count_tokens(system_content, "llama-3.1-70b-versatile") user_content = action_type.user_content user_tokens = count_tokens(user_content, "llama-3.1-70b-versatile") max_chunk_tokens = max_tokens_per_request - reserved_tokens - system_tokens - user_tokens # Split the transcription text into chunks chunks = split_text_into_chunks(text, max_chunk_tokens, "llama-3.1-70b-versatile") messages = [ {"role": "system", "content": action_type.system_content}, ] assistant_responses = [] for index, chunk in enumerate(chunks): user_message = { "role": "user", "content": action_type.user_content.format( text=chunk, language=language_code ) } messages.append(user_message) # Estimate tokens for the prompt input_tokens = sum(count_tokens(m['content'], "llama-3.1-70b-versatile") for m in messages) total_tokens = input_tokens + max_completion_tokens # Check if total tokens exceed the limit if total_tokens > max_tokens_per_request: model_name = "openai-model-name" # Set to your OpenAI model name else: model_name = "llama-3.1-70b-versatile" # Default to Groq model # Adjust messages if necessary while total_tokens > max_tokens_per_request and len(messages) > 2: messages.pop(1) input_tokens = sum(count_tokens(m['content'], model_name) for m in messages) total_tokens = input_tokens + max_completion_tokens # Wait for tokens to be available completion = make_completion_request( model_name=model_name, messages=messages, temperature=0.1, max_tokens=max_completion_tokens, ) # Extract the content assistant_message = completion.choices[0].message content = assistant_message.content assistant_responses.append(content) messages.append({"role": "assistant", "content": content}) # Update the total tokens used if hasattr(completion, 'usage') and hasattr(completion.usage, 'total_tokens'): total_tokens_used += completion.usage.total_tokens else: total_tokens_used += total_tokens # Use estimated tokens if index < len(chunks) - 1: sleep(60) # Combine the summaries final_summary = " ".join(assistant_responses) transcription_action = TranscriptionAction.objects.create( file_id=file_id, action_type_id=action_type_id, content=final_summary, kit_id=kit_id ) return transcription_action.id except Exception as e: error_message = f"An error occurred in the prompt function: {e}" print(error_message) sentry_sdk.capture_exception(e) return None ### Key Changes: 1. Model Selection: A check is added to determine if the total tokens exceed max_tokens_per_request. If they do, the model_name is set to the OpenAI model name (replace "openai-model-name" with your actual OpenAI model name). 2. Code Formatting: The code is formatted for better readability, with proper indentation and line breaks.