added tokens() function to Message and Chat

This commit is contained in:
2023-09-01 08:57:54 +02:00
parent 93290da5b5
commit 7f612bfc17
2 changed files with 19 additions and 0 deletions
+12
View File
@@ -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