added tokens() function to Message and Chat
This commit is contained in:
@@ -132,6 +132,7 @@ class Question(str):
|
||||
"""
|
||||
A single question with a defined header.
|
||||
"""
|
||||
tokens: int = 0 # tokens used by this question
|
||||
txt_header: ClassVar[str] = '=== QUESTION ==='
|
||||
yaml_key: ClassVar[str] = 'question'
|
||||
|
||||
@@ -165,6 +166,7 @@ class Answer(str):
|
||||
"""
|
||||
A single answer with a defined header.
|
||||
"""
|
||||
tokens: int = 0 # tokens used by this answer
|
||||
txt_header: ClassVar[str] = '=== ANSWER ==='
|
||||
yaml_key: ClassVar[str] = 'answer'
|
||||
|
||||
@@ -502,3 +504,13 @@ class Message():
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
def tokens(self) -> int:
|
||||
"""
|
||||
Returns the nr. of AI language tokens used by this message.
|
||||
If unknown, 0 is returned.
|
||||
"""
|
||||
if self.answer:
|
||||
return self.question.tokens + self.answer.tokens
|
||||
else:
|
||||
return self.question.tokens
|
||||
|
||||
Reference in New Issue
Block a user