Performance: Context formatting and injection
This concept affects how quickly the language model can generate responses by controlling prompt size and complexity, impacting initial load and interaction speed.
formatted_context = format_context(large_documents, max_tokens=500) prompt = f"Answer based on context: {formatted_context}" response = llm.generate(prompt)
context = "".join(large_documents) prompt = f"Answer based on context: {context}" response = llm.generate(prompt)
| Pattern | Prompt Size | Token Count | Response Latency | Verdict |
|---|---|---|---|---|
| Inject full raw context | Large (many KB) | High (1000+ tokens) | Slow (seconds delay) | [X] Bad |
| Inject formatted, truncated context | Small (few KB) | Low (few hundred tokens) | Fast (sub-second delay) | [OK] Good |
user_name into a Langchain prompt template?format() method to fill placeholders in prompt templates.format(user_name=user_name) correctly injects the variable into the placeholder.template = "Hello, {name}! Today is {day}."
prompt = template.format(name="Alice", day="Monday")
print(prompt)format(name="Alice", day="Monday") replaces placeholders with given values.template = "Welcome, {user}!"
prompt = template.format(user)
print(prompt)format(user) passes a positional argument but the placeholder expects a named argument {user}.user_name = "Bob" and color = "blue"?Pass the test and this pattern moves unknown