|
| 1 | +# |
| 2 | +# CLOUDERA APPLIED MACHINE LEARNING PROTOTYPE (AMP) |
| 3 | +# (C) Cloudera, Inc. 2025 |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Applicable Open Source License: Apache 2.0 |
| 7 | +# |
| 8 | +# NOTE: Cloudera open source products are modular software products |
| 9 | +# made up of hundreds of individual components, each of which was |
| 10 | +# individually copyrighted. Each Cloudera open source product is a |
| 11 | +# collective work under U.S. Copyright Law. Your license to use the |
| 12 | +# collective work is as provided in your written agreement with |
| 13 | +# Cloudera. Used apart from the collective work, this file is |
| 14 | +# licensed for your use pursuant to the open source license |
| 15 | +# identified above. |
| 16 | +# |
| 17 | +# This code is provided to you pursuant a written agreement with |
| 18 | +# (i) Cloudera, Inc. or (ii) a third-party authorized to distribute |
| 19 | +# this code. If you do not have a written agreement with Cloudera nor |
| 20 | +# with an authorized and properly licensed third party, you do not |
| 21 | +# have any rights to access nor to use this code. |
| 22 | +# |
| 23 | +# Absent a written agreement with Cloudera, Inc. ("Cloudera") to the |
| 24 | +# contrary, A) CLOUDERA PROVIDES THIS CODE TO YOU WITHOUT WARRANTIES OF ANY |
| 25 | +# KIND; (B) CLOUDERA DISCLAIMS ANY AND ALL EXPRESS AND IMPLIED |
| 26 | +# WARRANTIES WITH RESPECT TO THIS CODE, INCLUDING BUT NOT LIMITED TO |
| 27 | +# IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND |
| 28 | +# FITNESS FOR A PARTICULAR PURPOSE; (C) CLOUDERA IS NOT LIABLE TO YOU, |
| 29 | +# AND WILL NOT DEFEND, INDEMNIFY, NOR HOLD YOU HARMLESS FOR ANY CLAIMS |
| 30 | +# ARISING FROM OR RELATED TO THE CODE; AND (D)WITH RESPECT TO YOUR EXERCISE |
| 31 | +# OF ANY RIGHTS GRANTED TO YOU FOR THE CODE, CLOUDERA IS NOT LIABLE FOR ANY |
| 32 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR |
| 33 | +# CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, DAMAGES |
| 34 | +# RELATED TO LOST REVENUE, LOST PROFITS, LOSS OF INCOME, LOSS OF |
| 35 | +# BUSINESS ADVANTAGE OR UNAVAILABILITY, OR LOSS OR CORRUPTION OF |
| 36 | +# DATA. |
| 37 | +# |
| 38 | +import time |
| 39 | +import uuid |
| 40 | +from contextlib import AbstractContextManager |
| 41 | +from typing import Callable |
| 42 | +from unittest.mock import patch |
| 43 | + |
| 44 | +from alembic.testing.fixtures import testing_config |
| 45 | + |
| 46 | +from app.services.chat_history.chat_history_manager import ( |
| 47 | + ChatHistoryManager, |
| 48 | + RagStudioChatMessage, |
| 49 | + RagMessage, |
| 50 | +) |
| 51 | +from app.services.models import get_provider_class |
| 52 | + |
| 53 | + |
| 54 | +class TestingChatHistoryManager(ChatHistoryManager): |
| 55 | + def __init__(self): |
| 56 | + self._chat_history: dict[int, list[RagStudioChatMessage]] = dict() |
| 57 | + |
| 58 | + def retrieve_chat_history(self, session_id: int) -> list[RagStudioChatMessage]: |
| 59 | + return self._chat_history.get(session_id, []) |
| 60 | + |
| 61 | + def clear_chat_history(self, session_id: int) -> None: |
| 62 | + self._chat_history[session_id] = [] |
| 63 | + |
| 64 | + def delete_chat_history(self, session_id: int) -> None: |
| 65 | + del self._chat_history[session_id] |
| 66 | + |
| 67 | + def append_to_history( |
| 68 | + self, session_id: int, messages: list[RagStudioChatMessage] |
| 69 | + ) -> None: |
| 70 | + self._chat_history.setdefault(session_id, []).extend(messages) |
| 71 | + |
| 72 | + |
| 73 | +def patch_get_chat_history_manager() -> ( |
| 74 | + AbstractContextManager[Callable[[], TestingChatHistoryManager]] |
| 75 | +): |
| 76 | + session_id = 1 |
| 77 | + testing_chat_history_manager = TestingChatHistoryManager() |
| 78 | + testing_chat_history_manager.append_to_history( |
| 79 | + session_id, |
| 80 | + [ |
| 81 | + RagStudioChatMessage( |
| 82 | + id=str(uuid.uuid4()), |
| 83 | + session_id=session_id, |
| 84 | + source_nodes=[], |
| 85 | + inference_model=get_provider_class() |
| 86 | + .list_llm_models()[0] # TODO: randomize? |
| 87 | + .model_id, |
| 88 | + rag_message=RagMessage(user="test question", assistant="test answer"), |
| 89 | + evaluations=[], |
| 90 | + timestamp=time.time(), |
| 91 | + condensed_question=None, |
| 92 | + ) |
| 93 | + ], |
| 94 | + ) |
| 95 | + |
| 96 | + return patch( |
| 97 | + "app.services.chat_history.chat_history_manager._get_chat_history_manager", |
| 98 | + new=lambda: testing_chat_history_manager, |
| 99 | + ) |
0 commit comments