# Closing the Loop: DeepResearch & Q&A

> Understanding the final stage of user interaction: the "Ask" feature and DeepResearch module for multi-turn repository investigations.

- Repository: AsyncFuncAI/deepwiki-open
- GitHub: https://github.com/AsyncFuncAI/deepwiki-open
- Human wiki: https://grok-wiki.com/public/wiki/asyncfuncai-deepwiki-open-4d1f22320747
- Complete Markdown: https://grok-wiki.com/public/wiki/asyncfuncai-deepwiki-open-4d1f22320747/llms-full.txt

## Source Files

- `api/websocket_wiki.py`
- `api/simple_chat.py`
- `src/components/Ask.tsx`
- `Ollama-instruction.md`
- `api/logging_config.py`
- `pytest.ini`

---

<details>
<summary>Relevant source files</summary>
The following files were used as context for generating this wiki page:
- [api/websocket_wiki.py](api/websocket_wiki.py)
- [api/simple_chat.py](api/simple_chat.py)
- [src/components/Ask.tsx](src/components/Ask.tsx)
- [api/rag.py](api/rag.py)
- [api/prompts.py](api/prompts.py)
- [Ollama-instruction.md](Ollama-instruction.md)
</details>

# Closing the Loop: DeepResearch & Q&A

The final stage of user interaction in DeepWiki is centered around the **Ask** feature and the **DeepResearch** module. While the initial wiki generation provides a broad architectural overview, these features enable developers to perform surgical, multi-turn investigations into specific components, logic flows, or bug root causes. By "closing the loop," DeepWiki transforms from a static documentation generator into an active, conversational research partner.

This page explores the technical implementation of the retrieval-augmented Q&A engine and the autonomous multi-iteration research loop that powers complex repository investigations.

## The Q&A Engine: "Ask" Feature

The **Ask** feature provides a direct interface for querying the repository using natural language. It leverages a Retrieval-Augmented Generation (RAG) pipeline to ground every answer in the actual source code, ensuring technical accuracy and citing specific files.

### RAG Integration and Retrieval
When a user submits a query, the backend utilizes the `RAG` component to identify the most relevant code snippets. This process involves:
1.  **Vector Search**: Using `FAISSRetriever` to find documents whose embeddings align with the query.
2.  **Context Construction**: Grouping retrieved snippets by file path to provide structured context to the LLM.
3.  **Memory Management**: Maintaining a `DialogTurn` history to support follow-up questions within the same session.

Sources: [api/rag.py:153-243](api/rag.py#L153-L243), [api/websocket_wiki.py:192-234](api/websocket_wiki.py#L192-L234)

### Streaming and WebSockets
To provide a responsive user experience, the Ask feature uses real-time streaming. The primary communication channel is WebSockets, which allows the AI to stream its reasoning and final answer as it is generated. An HTTP streaming fallback is implemented in `api/simple_chat.py` for environments where WebSockets are restricted.

Sources: [api/websocket_wiki.py:63-131](api/websocket_wiki.py#L63-L131), [src/components/Ask.tsx:578-620](src/components/Ask.tsx#L578-L620)

## DeepResearch: Multi-Turn Investigations

The **DeepResearch** module is an advanced mode that automates multi-iteration investigations. Instead of a single-shot answer, it executes a structured research loop to "dive deep" into complex topics.

### The Iterative Research Loop
DeepResearch follows a programmed progression, typically spanning up to 5 iterations. Each iteration is guided by a specific system prompt tailored to the research stage:

1.  **Research Plan**: The first iteration identifies key aspects to investigate and outlines an approach.
2.  **Research Updates**: Intermediate iterations (2-4) explore gaps, verify findings, and provide new technical insights.
3.  **Final Conclusion**: The final iteration synthesizes all previous findings into a comprehensive, definitive answer.

Sources: [api/prompts.py:60-151](api/prompts.py#L60-L151), [api/websocket_wiki.py:258-357](api/websocket_wiki.py#L258-L357)

### Autonomous Continuation logic
The frontend component (`Ask.tsx`) manages the state of the research process. It detects when the AI has finished an iteration and, if the research is not yet complete, automatically triggers the next iteration by sending a hidden `[DEEP RESEARCH] Continue the research` prompt to the backend.

```mermaid
sequenceDiagram
    participant User
    participant UI as Ask.tsx
    participant API as websocket_wiki.py
    participant LLM as LLM Provider

    User->>UI: Enable DeepResearch & Ask Question
    UI->>API: [DEEP RESEARCH] Question
    API->>LLM: Apply "Research Plan" Prompt
    LLM-->>UI: Streaming "## Research Plan"
    Note over UI: Check completion markers
    UI->>API: [DEEP RESEARCH] Continue
    API->>LLM: Apply "Research Update" Prompt
    LLM-->>UI: Streaming "## Research Update"
    Note over UI: Repeat up to Iteration 5
    UI->>API: [DEEP RESEARCH] Continue (Final)
    API->>LLM: Apply "Final Conclusion" Prompt
    LLM-->>UI: Streaming "## Final Conclusion"
```
Sources: [src/components/Ask.tsx:281-403](src/components/Ask.tsx#L281-L403), [src/components/Ask.tsx:483-498](src/components/Ask.tsx#L483-L498)

## Architecture and Data Flow

The Ask and DeepResearch features are designed to be provider-neutral, supporting local models like Ollama alongside cloud providers such as Google Gemini, OpenAI, and AWS Bedrock.

### Provider Neutrality
The system abstracts model interactions through a modular client architecture. Users can switch between providers in the UI, and the backend handles the specific API requirements (token counting, context window management, and response parsing) for each.

| Component | Responsibility | Key Files |
| :--- | :--- | :--- |
| **Frontend UI** | State management, research navigation, and markdown rendering. | [Ask.tsx](src/components/Ask.tsx) |
| **Orchestration** | Handling WebSocket/HTTP sessions and DeepResearch iteration logic. | [websocket_wiki.py](api/websocket_wiki.py) |
| **RAG Pipeline** | Embedding generation and document retrieval from FAISS. | [rag.py](api/rag.py) |
| **Prompt Library** | Standardized templates for Q&A and DeepResearch stages. | [prompts.py](api/prompts.py) |

Sources: [api/websocket_wiki.py:440-562](api/websocket_wiki.py#L440-L562), [Ollama-instruction.md:135-152](Ollama-instruction.md#L135-L152)

## Summary

The Q&A and DeepResearch modules provide the final, most granular layer of the DeepWiki investigation process. By combining a robust RAG pipeline with an autonomous multi-turn research loop, DeepWiki enables developers to transition from high-level understanding to deep technical mastery of a codebase. The system's provider-neutral architecture ensures this capability is accessible whether running locally with Ollama or leveraging massive cloud context windows.

Sources: [api/websocket_wiki.py:156-188](api/websocket_wiki.py#L156-L188), [src/components/Ask.tsx:712-723](src/components/Ask.tsx#L712-L723)
