Implementation:FlagOpen FlagEmbedding Search Demo Tool
| Knowledge Sources | |
|---|---|
| Domains | Search_System, Conversational_AI, Hybrid_Retrieval |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
⚠️ LEGACY CODE: This file is in `research/old-examples/` and is superseded by the main FlagEmbedding inference APIs.
Interactive conversational search agent combining BM25, dense retrieval, and GPT-based query rewriting and answer generation.
Description
This module implements a complete conversational search system with multiple components:
LocalDatasetLoader manages corpus data:
- Loads Wikipedia documents (title + text)
- Stores pre-computed dense embeddings for FAISS search
QueryGenerator rewrites conversational queries:
- Uses GPT-3.5 to rewrite context-dependent queries into standalone questions
- Replaces pronouns and implicit references with explicit entities
- Maintains conversation history for context
AnswerGenerator produces final answers:
- Takes rewritten query and retrieved passages
- Generates comprehensive answers using GPT-3.5
- Formats responses based on provided references
BMVectorIndex implements hybrid retrieval:
- BM25 first-stage retrieval (top 1000 candidates via Pyserini)
- Dense reranking using BGE embeddings (top N from candidates)
- Combines keyword matching with semantic similarity
- Supports Chinese with optional instruction prefixes
Agent orchestrates the full pipeline:
- Maintains conversation memory with sliding window (3500 tokens via tiktoken)
- Generates queries → retrieves passages → generates answers
- Updates memory with Q&A pairs for multi-turn coherence
The system provides verbose mode for debugging, showing rewritten queries and retrieved passages.
Usage
Use this for building interactive search demos with conversational query understanding and context-aware answer generation over large document collections.
Code Reference
Source Location
- Repository: FlagOpen_FlagEmbedding
- File: research/old-examples/search_demo/tool.py
- Lines: 1-130
Signature
class LocalDatasetLoader:
def __init__(self, data_path, embedding_path)
class QueryGenerator:
def __init__(self)
def run(self, history, question)
class AnswerGenerator:
def __init__(self)
def run(self, history, question, references)
class BMVectorIndex:
def __init__(self, model_path, bm_index_path, data_loader)
def search_for_doc(self, query: str, RANKING: int = 1000, TOP_N: int = 5)
class Agent:
def __init__(self, index)
def answer(self, question, RANKING=1000, TOP_N=5, verbose=True)
Import
from research.old_examples.search_demo.tool import Agent, BMVectorIndex, LocalDatasetLoader
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data_path | str | Yes | Path to Wikipedia dataset directory |
| embedding_path | str | Yes | Path to pre-computed embeddings .npy file |
| model_path | str | Yes | Path to BGE embedding model |
| bm_index_path | str | Yes | Path to Lucene BM25 index |
| question | str | Yes | User's conversational question |
| history | str | No | Previous Q&A pairs in conversation |
| RANKING | int | No | BM25 candidates to retrieve (default: 1000) |
| TOP_N | int | No | Final passages to use (default: 5) |
Outputs
| Name | Type | Description |
|---|---|---|
| answer | str | Generated answer to the question |
| rewritten_query | str | Standalone query (if verbose=True) |
| references | str | Retrieved passages (if verbose=True) |
Usage Examples
from research.old_examples.search_demo.tool import Agent, BMVectorIndex, LocalDatasetLoader
# Initialize data loader
data_loader = LocalDatasetLoader(
data_path="./search_demo_data/dataset",
embedding_path="./search_demo_data/emb/data.npy"
)
# Initialize hybrid search index
search_index = BMVectorIndex(
model_path="BAAI/bge-base-zh-v1.5",
bm_index_path="./search_demo_data/index",
data_loader=data_loader
)
# Create conversational agent
agent = Agent(index=search_index)
# Single-turn Q&A
agent.answer("什么是人工智能?", TOP_N=3, verbose=True)
# 输出: 查询:什么是人工智能?
# 知识:[passages]
# 答:人工智能是...
# Multi-turn conversation (with context)
agent.answer("它有哪些应用?", TOP_N=3, verbose=True)
# Automatically uses conversation history to understand "它" refers to AI
# 输出: 查询:人工智能有哪些应用? (rewritten)
# 知识:[passages]
# 答:人工智能的应用包括...
# Clear memory for new conversation
agent.empty_memory()