Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 1 addition & 53 deletions api/rag/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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()

Expand Down
27 changes: 7 additions & 20 deletions api/services/research.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<turn>\n<user>{user_msg.content}</user>\n<assistant>{assistant_msg.content}</assistant>\n</turn>\n"

if is_deep_research:
logger.info(
Expand Down Expand Up @@ -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"<turn>\n<user>{turn.user_query.query_str}</user>\n<assistant>{turn.assistant_response.response_str}</assistant>\n</turn>\n"

async def stream_and_fallback(
streamer: ChatStreamer,
prompt_func: Callable[[], str],
Expand Down
93 changes: 93 additions & 0 deletions tests/backend/services/test_research_chat.py
Original file line number Diff line number Diff line change
@@ -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 "<conversation_history>" 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("<user>question A</user>") == 1
assert prompt.count("<assistant>answer A</assistant>") == 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 (
"<conversation_history>\n"
"<turn>\n<user>Q1</user>\n<assistant>A1</assistant>\n</turn>\n"
"<turn>\n<user>Q2</user>\n<assistant>A2</assistant>\n</turn>\n"
"</conversation_history>"
) in prompt
assert "<user>Q3</user>" not in prompt
assert "<query>\nQ3\n</query>" in prompt