11# Copyright (C) 2024. Hao Zheng
22# All rights reserved.
3- from typing import Union
43
54from anthropic .types import Message
65from openai .types .chat import ChatCompletion
@@ -13,8 +12,8 @@ class SameLanguageException(Exception):
1312
1413 def __init__ (self ):
1514 super ().__init__ (
16- ' Source language and target language are the same, no need to translate. '
17- ' If you want to translate, set force_translate=True.'
15+ " Source language and target language are the same, no need to translate. "
16+ " If you want to translate, set force_translate=True."
1817 )
1918
2019
@@ -32,8 +31,9 @@ class LengthExceedException(ChatBotException):
3231 Raised when the length of generated response exceeds the limit.
3332 """
3433
35- def __init__ (self , response : Union [ ChatCompletion , Message ] ):
34+ def __init__ (self , response : ChatCompletion | Message ):
3635 if isinstance (response , ChatCompletion ):
36+ assert response .usage is not None , "ChatCompletion.usage is None"
3737 prompt_tokens = response .usage .prompt_tokens
3838 completion_tokens = response .usage .completion_tokens
3939 total_tokens = response .usage .total_tokens
@@ -42,14 +42,14 @@ def __init__(self, response: Union[ChatCompletion, Message]):
4242 completion_tokens = response .usage .output_tokens
4343 total_tokens = prompt_tokens + completion_tokens
4444 else :
45- raise ValueError (f' Invalid response type: { type (response )} ' )
45+ raise ValueError (f" Invalid response type: { type (response )} " )
4646
4747 super ().__init__ (
48- f' Failed to get completion. Exceed max token length. '
49- f' Prompt tokens: { prompt_tokens } , '
50- f' Completion tokens: { completion_tokens } , '
51- f' Total tokens: { total_tokens } '
52- f' Reduce chunk_size may help.'
48+ f" Failed to get completion. Exceed max token length. "
49+ f" Prompt tokens: { prompt_tokens } , "
50+ f" Completion tokens: { completion_tokens } , "
51+ f" Total tokens: { total_tokens } "
52+ f" Reduce chunk_size may help."
5353 )
5454
5555
@@ -59,7 +59,7 @@ class OpenaiFailureException(Exception):
5959 """
6060
6161 def __init__ (self ):
62- super ().__init__ (' OpenAI API failed to generate response.' )
62+ super ().__init__ (" OpenAI API failed to generate response." )
6363
6464
6565class FfmpegException (Exception ):
@@ -74,4 +74,4 @@ def __init__(self, message):
7474
7575class DependencyException (Exception ):
7676 def __init__ (self , message ):
77- super ().__init__ (f' Dependency not correctly installed: { message } ' )
77+ super ().__init__ (f" Dependency not correctly installed: { message } " )
0 commit comments