Multilingualism Detection
# 🧶︱general
s
Hello, I'm building a multilingual chatbot (Italian, English, Spanish, etc.) that acts as a travel consultant for a specific city, using semantic caching with a vector database to reduce LLM API costs and latency. ## Current Architecture Cached responses are stored with embeddings and language metadata:
Copy code
python
# English entry
{
  "embedding": [0.23, 0.45, ...],
  "metadata": {
    "question": "what are the best restaurants?",
    "answer": "The best restaurants are: Trattoria Roma, Pizzeria Napoli...",
    "language": "en"
  }
}

# Italian entry
{
  "embedding": [0.24, 0.46, ...],
  "metadata": {
    "question": "quali sono i migliori ristoranti?",
    "answer": "I migliori ristoranti sono: Trattoria Roma, Pizzeria Napoli...",
    "language": "it"
  }
}
## The Problem Since embeddings are semantic, "best restaurants" (English) and "migliori ristoranti" (Italian) have very similar vectors. Without proper filtering, an Italian user asking "ristoranti" might get the cached English response. My current approach: Filter vector search by language metadata:
Copy code
python
results = vector_db.query(
    embedding=embed(user_message),
    filter={"language": user_language},
    top_k=1
)
This works IF I can reliably detect the user's language. But: - Messages are often very short ("museums", "metro", "parking") - Language detection libraries (langdetect, fastText) are unreliable with < 20 characters - The chatbot is stateless (no conversation history for caching efficiency) - Platform is WhatsApp (no browser headers available)
m
Hmm, interesting! This might be a silly question, but why not ask a user what language they prefer and then cache that, and include in metadata per call?
s
yes I had thought about it, but, if possible, I wanted to avoid asking the user for the language. Is there no other solution? Maybe is there an embeddings model that creates non-similar embeddings if the meaning is the same but the language is different? Now I am using text-embeddings-004 ( gemini )
m
Another question: why not use a dictionary based approach if the queries are less than 20 characters? Check the words in the query, check in spanish/english/italian etc dict, majority means language assignment? Just wanna understand if this is a engineering problem, or an embedding problem. Sounds like the former, I'm not sure if there are embedding models that can distinguise languages that precisely. Part of the appeal of multilingual embedding models IS that they estimate similarity somewhat independently of language, allowing for crosslingual application