-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ChatMemory and BaseConfig (#375)
* feat: add chat memory * fix: path * fix: lifecycle * feat: add base config * fix: missed config.py * fix: syntax * feat: simplify usage
- Loading branch information
1 parent
928c24f
commit 05d97ec
Showing
9 changed files
with
139 additions
and
15 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
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
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
42 changes: 42 additions & 0 deletions
42
agents/ten_packages/system/ten_ai_base/interface/ten_ai_base/chat_memory.py
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,42 @@ | ||
# | ||
# This file is part of TEN Framework, an open source project. | ||
# Licensed under the Apache License, Version 2.0. | ||
# See the LICENSE file for more information. | ||
# | ||
import threading | ||
|
||
|
||
class ChatMemory: | ||
def __init__(self, max_history_length): | ||
self.max_history_length = max_history_length | ||
self.history = [] | ||
self.mutex = threading.Lock() # TODO: no need lock for asyncio | ||
|
||
def put(self, message): | ||
with self.mutex: | ||
self.history.append(message) | ||
|
||
while True: | ||
history_count = len(self.history) | ||
if history_count > 0 and history_count > self.max_history_length: | ||
self.history.pop(0) | ||
continue | ||
if history_count > 0 and self.history[0]["role"] == "assistant": | ||
# we cannot have an assistant message at the start of the chat history | ||
# if after removal of the first, we have an assistant message, | ||
# we need to remove the assistant message too | ||
self.history.pop(0) | ||
continue | ||
break | ||
|
||
def get(self): | ||
with self.mutex: | ||
return self.history | ||
|
||
def count(self): | ||
with self.mutex: | ||
return len(self.history) | ||
|
||
def clear(self): | ||
with self.mutex: | ||
self.history = [] |
48 changes: 48 additions & 0 deletions
48
agents/ten_packages/system/ten_ai_base/interface/ten_ai_base/config.py
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,48 @@ | ||
from dataclasses import dataclass, fields | ||
import builtins | ||
from typing import TypeVar, Type | ||
from ten import TenEnv | ||
|
||
T = TypeVar('T', bound='BaseConfig') | ||
|
||
|
||
@dataclass | ||
class BaseConfig: | ||
""" | ||
Base class for implementing configuration. | ||
Extra configuration fields can be added in inherited class. | ||
""" | ||
|
||
@classmethod | ||
def create(cls: Type[T], ten_env: TenEnv) -> T: | ||
c = cls() | ||
c._init(ten_env) | ||
return c | ||
|
||
def _init(obj, ten_env: TenEnv): | ||
""" | ||
Get property from ten_env to initialize the dataclass config. | ||
""" | ||
for field in fields(obj): | ||
# TODO: 'is_property_exist' has a bug that can not be used in async extension currently, use it instead of try .. except once fixed | ||
# if not ten_env.is_property_exist(field.name): | ||
# continue | ||
try: | ||
match field.type: | ||
case builtins.str: | ||
val = ten_env.get_property_string(field.name) | ||
if val: | ||
setattr(obj, field.name, val) | ||
case builtins.int: | ||
val = ten_env.get_property_int(field.name) | ||
setattr(obj, field.name, val) | ||
case builtins.bool: | ||
val = ten_env.get_property_bool(field.name) | ||
setattr(obj, field.name, val) | ||
case builtins.float: | ||
val = ten_env.get_property_float(field.name) | ||
setattr(obj, field.name, val) | ||
case _: | ||
pass | ||
except Exception as e: | ||
pass |
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