LlamaIndexllama-index-core 0.14 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
20 small wins to finish your pathNext lesson

Metadata: what gets embedded and what the model sees

Each node carries metadata into two places: the text that is embedded, and the text the model reads. LlamaIndex lets you choose, per key, which it goes into.

Example
from llama_index.core import Document
from llama_index.core.schema import MetadataMode

doc = Document(text="Standard delivery takes 3 to 5 working days.", metadata={"file_name": "delivery.md", "reviewed_by": "ops-team"})
print(doc.get_content(metadata_mode=MetadataMode.EMBED))
print("---")
print(doc.get_content(metadata_mode=MetadataMode.LLM))

get_content with MetadataMode.EMBED shows what the embedding model receives, and LLM what the model reads. For a Document you create yourself, both include every metadata key, written as key: value lines above the text. SimpleDirectoryReader sets exclusions for the file keys it adds, which is why lesson 9's prompt shows none.

Choosing per key

Example
doc.excluded_embed_metadata_keys = ["reviewed_by"]
doc.excluded_llm_metadata_keys = ["reviewed_by"]
print(doc.get_content(metadata_mode=MetadataMode.EMBED))
print("---")
print(doc.get_content(metadata_mode=MetadataMode.LLM))

reviewed_by is useful to keep on the node for filtering or auditing, and useless for matching a customer's question or answering it, so it is excluded from both. file_name stays in the model's view here, so a model could name the file it answered from.

Rule of thumb: embed what helps a question find the chunk, such as a title or product name; show the model what helps it answer or cite; keep everything else out of both.

Try it yourself
  • Exclude file_name from the embed text only.
  • Add a "product": "LMP-204" key to a lamp document and check it appears in both views.
  • Print MetadataMode.NONE.

Little by little, you're building something great.