diff --git a/api/rag/rag.py b/api/rag/rag.py index c9aa57d5a..53138590a 100644 --- a/api/rag/rag.py +++ b/api/rag/rag.py @@ -2,17 +2,10 @@ import os from collections import defaultdict from collections.abc import Sized -from uuid import uuid4 import adalflow as adal from adalflow.components.retriever.faiss_retriever import FAISSRetriever -from adalflow.core.types import ( - AssistantResponse, - DialogTurn, - Document, - RetrieverOutput, - UserQuery, -) +from adalflow.core.types import Document, RetrieverOutput from api.config import configs, get_embedder from api.logger import get_logger @@ -72,50 +65,6 @@ def check_ollama_model_exists(model_name: str, ollama_host: str | None = None) - return False -class CustomConversation(list[DialogTurn]): - """Custom implementation of Conversation to fix the list assignment index out of range error""" - - -class Memory(adal.core.component.DataComponent): - """Simple conversation management with a list of dialog turns.""" - - def __init__(self): - super().__init__() - # Use our custom implementation instead of the original Conversation class - self.current_conversation = CustomConversation() - - def call(self) -> dict: - """Return the conversation history as a dictionary.""" - all_dialog_turns = { - dialog_turn.id: dialog_turn for dialog_turn in self.current_conversation - } - logger.info(f"Returning {len(all_dialog_turns)} dialog turns from memory") - return all_dialog_turns - - def add_dialog_turn(self, user_query: str, assistant_response: str) -> None: - """ - Add a dialog turn to the conversation history. - - Args: - user_query: The user's query - assistant_response: The assistant's response - - """ - # Create a new dialog turn using our custom implementation - dialog_turn = DialogTurn( - id=str(uuid4()), - user_query=UserQuery(query_str=user_query), - assistant_response=AssistantResponse(response_str=assistant_response), - ) - - # Safely append the dialog turn - self.current_conversation.append(dialog_turn) - logger.info( - "Successfully added dialog turn, now have %d turns", - len(self.current_conversation), - ) - - def _get_document_vector_size(document: Document) -> int | None: if hasattr(document.vector, "shape"): embedding_size = ( @@ -171,7 +120,6 @@ def __init__(self, provider="google", model=None, use_s3: bool = False): # noqa ) # Initialize components - self.memory = Memory() self.embedder = get_embedder(embedder_type=self.embedder_type) self.initialize_db_manager() diff --git a/api/services/research.py b/api/services/research.py index 3bb1d6475..19c076332 100644 --- a/api/services/research.py +++ b/api/services/research.py @@ -125,17 +125,14 @@ async def research_chat( if last_message.role != "user": raise ValueError("Last message must be from the user") - # Process previous messages to build conversation history + # Build history from this request only: the RAG object is shared across requests. + conversation_history = "" for i in range(0, len(request.messages) - 1, 2): - if i + 1 < len(request.messages): - user_msg = request.messages[i] - assistant_msg = request.messages[i + 1] - - if user_msg.role == "user" and assistant_msg.role == "assistant": - rag.memory.add_dialog_turn( - user_query=user_msg.content, - assistant_response=assistant_msg.content, - ) + user_msg = request.messages[i] + assistant_msg = request.messages[i + 1] + + if user_msg.role == "user" and assistant_msg.role == "assistant": + conversation_history += f"\n{user_msg.content}\n{assistant_msg.content}\n\n" if is_deep_research: logger.info( @@ -260,16 +257,6 @@ async def research_chat( language_name=language_name, ) - # Format conversation history - conversation_history = "" - for turn_id, turn in rag.memory().items(): - if ( - not isinstance(turn_id, int) - and hasattr(turn, "user_query") - and hasattr(turn, "assistant_response") - ): - conversation_history += f"\n{turn.user_query.query_str}\n{turn.assistant_response.response_str}\n\n" - async def stream_and_fallback( streamer: ChatStreamer, prompt_func: Callable[[], str], diff --git a/tests/backend/services/test_research_chat.py b/tests/backend/services/test_research_chat.py new file mode 100644 index 000000000..e796f7899 --- /dev/null +++ b/tests/backend/services/test_research_chat.py @@ -0,0 +1,93 @@ +from types import SimpleNamespace + +import pytest + +from api.schemas import ChatCompletionRequest, ChatMessage +from api.services import research + +pytestmark = pytest.mark.asyncio + + +class _CapturingStreamer: + provider = "stub" + error_hint = None + + def __init__(self): + self.prompts = [] + + async def respond_stream(self, prompt): + self.prompts.append(prompt) + yield "ok" + + +@pytest.fixture +def streamer(monkeypatch): + """Run the real RAG class and block only its network and disk boundaries.""" + + async def _prepare_retriever(self, *args, **kwargs): + return None + + async def _retrieve(self, *args, **kwargs): + return [] + + capturing = _CapturingStreamer() + monkeypatch.setattr(research, "_RAG_CACHE", {}) + monkeypatch.setattr("api.config.get_embedder_type", lambda: "openai") + monkeypatch.setattr("api.rag.rag.get_embedder", lambda **kwargs: object()) + monkeypatch.setattr(research.RAG, "aprepare_retriever", _prepare_retriever) + monkeypatch.setattr(research.RAG, "acall", _retrieve) + monkeypatch.setattr(research, "Repo", lambda **kwargs: SimpleNamespace(name="r")) + monkeypatch.setattr(research, "repo_index_exist", lambda repo: True) + monkeypatch.setattr(research, "count_tokens", lambda *args, **kwargs: 1) + monkeypatch.setattr( + research, "get_model_config", lambda *args: {"model_kwargs": {}} + ) + monkeypatch.setattr(research.ChatStreamer, "create", lambda **kwargs: capturing) + return capturing + + +async def _send(streamer, *contents): + """Send alternating user and assistant messages; return the captured prompt.""" + request = ChatCompletionRequest( + repo_url="https://github.com/o/r", + messages=[ + ChatMessage(role="assistant" if i % 2 else "user", content=content) + for i, content in enumerate(contents) + ], + ) + async for _ in await research.research_chat(request): + pass + return streamer.prompts[-1] + + +async def test_request_without_history_ignores_earlier_request(streamer): + await _send(streamer, "question A", "answer A", "follow-up A") + + prompt = await _send(streamer, "question B") + + assert len(research._RAG_CACHE) == 1 # both requests shared one RAG object + assert "question A" not in prompt + assert "answer A" not in prompt + assert "" not in prompt + + +async def test_resending_a_conversation_does_not_duplicate_turns(streamer): + await _send(streamer, "question A", "answer A", "follow-up A") + + prompt = await _send(streamer, "question A", "answer A", "follow-up A") + + assert prompt.count("question A") == 1 + assert prompt.count("answer A") == 1 + + +async def test_prompt_keeps_own_turns_in_order_and_excludes_last_question(streamer): + prompt = await _send(streamer, "Q1", "A1", "Q2", "A2", "Q3") + + assert ( + "\n" + "\nQ1\nA1\n\n" + "\nQ2\nA2\n\n" + "" + ) in prompt + assert "Q3" not in prompt + assert "\nQ3\n" in prompt