Home Knowledge Base Grounding LLM Responses

Grounding LLM Responses

What is Grounding? Grounding ensures LLM outputs are based on reliable sources rather than model parameters alone. It bridges the gap between fluent generation and factual accuracy.

Grounding Techniques

Document Grounding (RAG) Base responses on retrieved documents:

def document_grounded(query: str) -> str:
    docs = vector_store.search(query, k=5)
    context = "
".join([d.text for d in docs])

    return llm.generate(f"""
You are a helpful assistant. Answer based ONLY on the provided context.
If the context does not contain the answer, say so.

Context:
{context}

Question: {query}
Answer:
    """)

API Grounding Ground in real-time data:

def api_grounded(query: str) -> str:
    # Extract entities
    entities = extract_entities(query)

    # Fetch real data
    data = {}
    for entity in entities:
        data[entity] = api.lookup(entity)

    return llm.generate(f"""
Use ONLY this data to answer:
{json.dumps(data)}

Question: {query}
    """)

Code Execution Grounding Ground calculations in actual execution:

def code_grounded(query: str) -> str:
    # Generate code
    code = llm.generate(f"Write Python code to answer: {query}")

    # Execute
    result = execute_safely(code)

    # Generate response with result
    return llm.generate(f"""
The code executed and produced: {result}
Explain this result for: {query}
    """)

Grounding vs No Grounding

AspectUngroundedGrounded
SourceModel parametersExternal data
CurrencyTraining cutoffReal-time possible
VerifiabilityLowHigh
HallucinationHigher riskLower risk
LatencyLowerHigher

Grounding Sources

SourceUse Case
DocumentsKnowledge bases, policies
APIsReal-time data (weather, stocks)
DatabasesStructured enterprise data
Code executionCalculations, data analysis
Web searchCurrent events, broad knowledge

Grounding Prompts

# Strict grounding
Answer using ONLY the provided context. Do not use prior knowledge.
If unsure, state you cannot answer from the given context.

# Soft grounding
Use the provided context as your primary source.
Supplement with your knowledge only when context is insufficient.
Clearly distinguish between sourced and unsourced information.

Verification Always verify grounded responses:

groundingfactualknowledge

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.