forked from ScottLogic/InferLLM
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
223 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import json | ||
import logging | ||
from typing import TypedDict | ||
import redis | ||
|
||
from src.utils.json import try_parse_to_json | ||
from src.utils import Config | ||
|
||
class ChatResponse(TypedDict): | ||
id: str | ||
question:str | ||
answer: str | ||
reasoning: str | None | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
config = Config() | ||
|
||
redis_client = redis.Redis(host=config.redis_host, port=6379, decode_responses=True) | ||
|
||
CHAT_KEY_PREFIX = "chat_" | ||
|
||
def store_chat_message(chat:ChatResponse): | ||
redis_client.set(CHAT_KEY_PREFIX + chat["id"], json.dumps(chat)) | ||
|
||
|
||
def get_chat_message(id: str) -> ChatResponse | None: | ||
value = redis_client.get(CHAT_KEY_PREFIX + id) | ||
if value and isinstance(value, str): | ||
parsed_session_data = try_parse_to_json(value) | ||
if parsed_session_data: | ||
return parsed_session_data | ||
return None |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from src.chat_storage_service import ChatResponse, get_chat_message, store_chat_message | ||
|
||
@pytest.fixture | ||
def mock_redis(): | ||
with patch('src.chat_storage_service.redis_client') as mock_redis: | ||
mock_instance = MagicMock() | ||
mock_redis.return_value = mock_instance | ||
yield mock_instance | ||
|
||
def test_store_chat_message(mocker, mock_redis): | ||
mocker.patch('src.chat_storage_service.redis_client', mock_redis) | ||
|
||
message = ChatResponse(id="1", question="Question", answer="Answer", reasoning="Reasoning") | ||
store_chat_message(message) | ||
|
||
mock_redis.set.assert_called_once_with("chat_1", json.dumps(message)) | ||
|
||
|
||
def test_get_chat_message(mocker, mock_redis): | ||
mocker.patch('src.chat_storage_service.redis_client', mock_redis) | ||
|
||
message = ChatResponse(id="1", question="Question", answer="Answer", reasoning="Reasoning") | ||
mock_redis.get.return_value = json.dumps(message) | ||
|
||
value = get_chat_message("1") | ||
|
||
assert value == 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
Oops, something went wrong.