Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Intel Ipex llm LangChain RAG Chain

From Leeroopedia
Revision as of 15:12, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Intel_Ipex_llm_LangChain_RAG_Chain.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains NLP, RAG
Last Updated 2026-02-09 00:00 GMT

Overview

LangChain LCEL chain composing retriever, prompt template, LLM, and output parser for RAG on Intel XPU.

Description

This is a Wrapper Doc for LangChain's LCEL chain composition used in the IPEX-LLM RAG pipeline. The chain uses RunnablePassthrough to pass the question through, pipes retrieved documents through a format function, combines with a prompt template from LangChain Hub ("rlm/rag-prompt"), passes through the IpexLLM, and parses with StrOutputParser.

External Reference

Usage

Use after creating the retriever and LLM to assemble the complete RAG inference chain.

Code Reference

Source Location

  • Repository: IPEX-LLM
  • File: python/llm/example/GPU/LangChain/rag.py
  • Lines: 77-88

Signature

from langchain import hub
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

prompt = hub.pull("rlm/rag-prompt")

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

result = rag_chain.invoke(query: str) -> str

Import

from langchain import hub
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

I/O Contract

Inputs

Name Type Required Description
retriever VectorStoreRetriever Yes Retriever from Chroma vector store
llm IpexLLM Yes IPEX-LLM LangChain LLM instance
query str Yes User question to answer

Outputs

Name Type Description
result str Generated answer grounded in retrieved context

Usage Examples

from langchain import hub
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

# Pull RAG prompt template from LangChain Hub
prompt = hub.pull("rlm/rag-prompt")

# Assemble LCEL RAG chain
rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# Invoke the chain
answer = rag_chain.invoke("What is IPEX-LLM?")
print(answer)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment