Prompt
How do I build a multilingual question answering search app?
Latest observation
Building a multilingual question answering search app usually means combining search / retrieval with question answering / generation in a way that works across languages. A practical way to build it is:
- Ingest documents
- Detect and store language
- Index for retrieval
- Translate or use multilingual embeddings
- Retrieve relevant passages
- Generate an answer in the user’s language
- Return citations and confidence
Here’s a solid architecture and implementation approach.
1) Define the product behavior
Decide what “multilingual” means for your app:
- User asks in any language
- Documents can be in multiple languages
- Answers are returned in the user’s language
- Optionally:
- Search only within one language
- Cross-lingual search, e.g. ask in English and find Spanish docs
- Answer with citations from original-language sources
A common and useful behavior is:
- User query language → detected automatically
- Retrieval across all indexed languages
- Answer generated in the query language
- Citations show original snippets + translated summaries if needed
2) Recommended architecture
A good production architecture looks like this:
Ingestion pipeline
- Document upload / crawl
- Text extraction from PDFs, HTML, DOCX, etc.
- Language detection
- Chunking into passages
- Embedding each chunk
- Store:
- raw text
- language
- metadata
- embeddings
- keywords / BM25 index
Query pipeline
- Detect query language
- Optional query translation
- Hybrid retrieval:
- lexical search (BM25)
- semantic search (embeddings)
- Rerank top results
- Pass top passages to LLM
- Generate answer in query language
- Return answer with citations
3) Choose your multilingual retrieval strategy
There are three main approaches:
A. Translate everything to one language
How it works
- Translate docs into a pivot language, usually English
- Build your search index on translated text
- Translate user queries to English
- Generate answer and translate back if needed
Pros
- Simple to reason about
- Works with monolingual models
- Easy to debug
Cons
- Translation errors hurt recall and answer quality
- Expensive at scale
- Loses nuance
- Two translation steps can add latency
Best for
- Small-medium systems
- When you already have strong translation infrastructure
B. Use multilingual embeddings
How it works
- Keep documents in original languages
- Embed chunks using a multilingual embedding model
- Query in any language and retrieve semantically similar chunks across languages
Pros
- No need to translate everything
- Better cross-lingual retrieval
- Usually lower latency than translate-all
Cons
- Needs good multilingual embeddings
- You still need answer generation in the user’s language
- Lexical signals may be weaker than language-specific search
Best for
- Modern QA apps
- Multilingual knowledge bases
- Cross-lingual search
C. Hybrid: multilingual embeddings + BM25 + translation
How it works
- Use embeddings for semantic retrieval
- Use BM25 for exact term matching
- Optionally translate query for better lexical coverage
- Rerank results with a cross-encoder or LLM
Pros
- Best overall relevance
- Handles names, acronyms, and exact terms better
- Usually strongest production option
Cons
- More engineering complexity
- More moving parts
Best for
- Production-grade QA systems
4) Indexing documents
Step 1: Extract text
Use parsers for:
- PDF:
pypdf,pdfplumber,unstructured - HTML:
BeautifulSoup,trafilatura - DOCX:
python-docx - OCR for scanned docs: Tesseract or cloud OCR
Step 2: Detect language
Use:
fastTextlanguage IDlangdetectCLD3
Store:
- document language
- chunk language
- source language confidence
Step 3: Chunk documents
Split into semantic chunks:
- 200–500 tokens is common
- preserve heading, section, and page metadata
- overlap chunks slightly to avoid cutting answers in half
Example metadata:
{
"doc_id": "policy_123",
"chunk_id": "policy_123_07",
"language": "es",
"source": "employee_handbook.pdf",
"page": 12,
"section": "Vacation policy"
}
Step 4: Embed chunks
Use a multilingual embedding model such as:
text-embedding-3-large(good multilingual performance)bge-m3e5-multilingualLaBSEmGTE
Store embeddings in:
- FAISS
- pgvector
- Pinecone
- Weaviate
- Milvus
- Elasticsearch / OpenSearch vector support
5) Retrieval design
Hybrid retrieval is usually best
Semantic retrieval
Find passages similar in meaning, even across languages.
Lexical retrieval
Find exact terms, names, and numbers.
Reranking
Take the top 20–100 retrieved chunks and rerank them with:
- a cross-encoder
- multilingual reranker
- LLM-based scoring
This improves answer quality a lot.
6) Question answering flow
A robust QA flow:
- Detect query language
- Search in all languages
- Collect top evidence passages
- Rerank evidence
- Ask an LLM to answer using only evidence
- Generate answer in the user’s language
- Attach citations
Important prompt pattern
Tell the model:
- only use retrieved evidence
- answer in the query language
- say “I don’t know” if evidence is insufficient
- cite sources
Example prompt idea:
Answer the question using only the provided context.
If the answer is not in the context, say you cannot determine it.
Respond in the same language as the user’s question.
Cite the source chunk IDs for each factual claim.
7) Multilingual answer generation
You have two choices:
Option 1: Generate directly in user language
Use a multilingual LLM and instruct it to answer in the same language as the query.
Option 2: Retrieve in any language, translate evidence, then answer
If the LLM is weaker in some languages, you can:
- retrieve original chunks
- translate retrieved chunks into the query language
- ask the model to answer using translated evidence
For best quality, many systems:
- retrieve in original language
- keep citations to original source
- feed either original text or translated text depending on the model
8) Handling cross-lingual search
Example:
- User asks in French
- Relevant document is in German
With multilingual embeddings, this can work directly.
Still, for better quality:
- detect query language
- optionally translate query into a pivot language
- perform both multilingual semantic search and translated lexical search
- merge and rerank results
This improves recall.
9) Suggested technology stack
Backend
- Python + FastAPI
- Celery / RQ / Kafka for indexing jobs
- PostgreSQL for metadata
- Vector DB:
- pgvector for simpler setups
- Pinecone / Milvus / Weaviate for scale
NLP
- Multilingual embeddings:
bge-m3,e5-multilingual, OpenAI embeddings - Language detection: fastText
- Reranker: multilingual cross-encoder or LLM rerank
- LLM: GPT-style or another multilingual instruction model
Frontend
- React / Next.js
- Search input with auto language detection
- Answer panel with citations
- Source document previews
10) Minimal example architecture
Ingestion
Docs -> text extraction -> language detection -> chunking -> embeddings -> vector DB
Query
User question -> detect language -> retrieve chunks -> rerank -> LLM answer -> citations
11) Pseudocode example
def answer_question(query):
query_lang = detect_language(query)
query_embedding = embed(query)
candidates = vector_search(query_embedding, top_k=50)
# Optional: lexical search + merge
lexical_hits = bm25_search(query, top_k=50)
candidates = merge_results(candidates, lexical_hits)
reranked = rerank(query, candidates, top_k=5)
context = "\n\n".join(
f"[{c['chunk_id']}] {c['text']}" for c in reranked
)
prompt = f"""
Answer the question using only the context.
Respond in {query_lang}.
Cite chunk IDs.
Question: {query}
Context:
{context}
"""
return llm_generate(prompt)
12) Common pitfalls
Poor chunking
If chunks are too large:
- embeddings get noisy
- retrieval is less precise
If chunks are too small:
- answers lose context
Ignoring language metadata
You may want to know:
- original language
- translated version
- confidence score
- locale-specific formatting
Overreliance on translation
Translation can distort legal, medical, or technical text.
No reranking
Without reranking, retrieval quality is often noticeably worse.
Not returning citations
Users trust the system more when sources are visible.
Hallucinations
Always constrain the model to retrieved evidence.
13) Evaluation
You should test:
- retrieval recall
- answer correctness
- cross-lingual performance
- citation accuracy
- latency
Useful metrics:
- Recall@k
- MRR
- exact match / F1 for QA
- human evaluation by language
- citation precision
Create a multilingual test set with:
- questions in different languages
- documents in different languages
- answers known ahead of time
14) If you want a simple MVP
Start with this:
- Use multilingual embeddings
- Store chunks in pgvector or Pinecone
- Add BM25 lexical search
- Rerank top results
- Use an LLM to answer in the query language
- Show citations
This gets you a strong first version without complex translation pipelines.
15) Practical MVP stack
If you want a quick build:
- Frontend: Next.js
- API: FastAPI
- Chunking: LangChain or LlamaIndex
- Embeddings:
bge-m3or OpenAI multilingual embeddings - Vector store: pgvector
- Search: hybrid vector + BM25
- LLM: multilingual instruction model
- Language detection: fastText
If you want, I can also provide:
- a reference architecture diagram,
- a step-by-step implementation plan, or
- a working code sample using FastAPI + pgvector + multilingual embeddings.