-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: limit addons dialogue size (#31)
* Limit addons dialogue size.
- Loading branch information
1 parent
5f0bee0
commit fedb23f
Showing
16 changed files
with
460 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing_extensions import override | ||
|
||
from aidial_assistant.chain.command_chain import ( | ||
LimitExceededException, | ||
ModelRequestLimiter, | ||
) | ||
from aidial_assistant.model.model_client import Message, ModelClient | ||
|
||
|
||
class AddonsDialogueLimiter(ModelRequestLimiter): | ||
def __init__(self, max_dialogue_tokens: int, model_client: ModelClient): | ||
self.max_dialogue_tokens = max_dialogue_tokens | ||
self.model_client = model_client | ||
|
||
self._dialogue_tokens = 0 | ||
self._initial_tokens: int | None = None | ||
|
||
@override | ||
async def verify_limit(self, messages: list[Message]): | ||
if self._initial_tokens is None: | ||
self._initial_tokens = await self.model_client.count_tokens( | ||
messages | ||
) | ||
return | ||
|
||
self._dialogue_tokens = ( | ||
await self.model_client.count_tokens(messages) | ||
- self._initial_tokens | ||
) | ||
|
||
if self._dialogue_tokens > self.max_dialogue_tokens: | ||
raise LimitExceededException( | ||
f"Addons dialogue limit exceeded. Max tokens: {self.max_dialogue_tokens}," | ||
f" actual tokens: {self._dialogue_tokens}." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
from aidial_assistant.chain.model_client import Message | ||
from pydantic import BaseModel | ||
|
||
from aidial_assistant.model.model_client import Message | ||
|
||
|
||
class DialogueTurn(BaseModel): | ||
assistant_message: str | ||
user_message: str | ||
|
||
|
||
class Dialogue: | ||
def __init__(self): | ||
self.messages: list[Message] = [] | ||
|
||
def append(self, assistant_message: str, user_message: str): | ||
self.messages.append(Message.assistant(assistant_message)) | ||
self.messages.append(Message.user(user_message)) | ||
def append(self, dialogue_turn: DialogueTurn): | ||
self.messages.append(Message.assistant(dialogue_turn.assistant_message)) | ||
self.messages.append(Message.user(dialogue_turn.user_message)) | ||
|
||
def pop(self): | ||
self.messages.pop() | ||
self.messages.pop() | ||
|
||
def is_empty(self): | ||
return not self.messages | ||
|
||
def dialogue_turn_count(self): | ||
return len(self.messages) // 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.