-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap history manager based on settings
Signed-off-by: Ygal Blum <[email protected]>
- Loading branch information
Showing
8 changed files
with
91 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" Manage history """ | ||
from injector import inject, singleton | ||
|
||
from knowledge_base_gpt.libs.history.base import HistoryBase | ||
from knowledge_base_gpt.libs.history.redis import HistoryRedis | ||
from knowledge_base_gpt.libs.history.memory import HistoryMemory | ||
from knowledge_base_gpt.libs.settings.settings import Settings | ||
|
||
|
||
@singleton | ||
class History(): # pylint:disable=R0903 | ||
""" Wrap history manager based on settings """ | ||
@inject | ||
def __init__(self, settings: Settings) -> None: | ||
match settings.history.mode: | ||
case 'memory': | ||
self._history = HistoryMemory() | ||
case 'redis': | ||
self._history = HistoryRedis(settings.redis) | ||
case _: | ||
pass | ||
|
||
@property | ||
def history(self) -> HistoryBase: | ||
""" Get history manager """ | ||
return self._history |
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 @@ | ||
""" Manage history in Memory """ | ||
from typing import List, Dict, Any, Union | ||
|
||
from langchain_core.messages import BaseMessage, AIMessage, HumanMessage | ||
|
||
from knowledge_base_gpt.libs.history.base import HistoryBase | ||
|
||
|
||
class HistoryMemory(HistoryBase): | ||
""" Manage history in memory """ | ||
def __init__(self): | ||
self._messages: Dict[str, List[BaseMessage]] = {} | ||
|
||
def get_messages(self, session_id: str) -> List[BaseMessage]: | ||
return self._messages.get(session_id, []) | ||
|
||
def add_to_history(self, session_id: str, answer: Dict[str, Any]): | ||
if self._messages.get(session_id) is None: | ||
self._messages[session_id] = [] | ||
self._messages[session_id].extend( | ||
[ | ||
self._get_human_message(answer['question']), | ||
self._get_ai_message(answer['answer']) | ||
] | ||
|
||
) | ||
|
||
def reset(self, session_id: str): | ||
if self._messages.get(session_id) is not None: | ||
del self._messages[session_id] | ||
|
||
@staticmethod | ||
def _get_human_message(message: Union[HumanMessage, str]) -> HumanMessage: | ||
if not isinstance(message, HumanMessage): | ||
message = HumanMessage(content=message) | ||
return message | ||
|
||
@staticmethod | ||
def _get_ai_message(message: Union[AIMessage, str]) -> AIMessage: | ||
if not isinstance(message, AIMessage): | ||
message = AIMessage(content=message) | ||
return message |
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