-
-
Save vicpada/c6e9226d010cfc45cbdfc395c3e06f7b to your computer and use it in GitHub Desktop.
Standard RAG pipeline with LangChain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from langchain_community.document_loaders import WebBaseLoader | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_openai import OpenAIEmbeddings, ChatOpenAI | |
| from langchain_chroma import Chroma from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_core.runnables import RunnablePassthrough | |
| from langchain_core.output_parsers import StrOutputParser | |
| # 1. Load and chunk documents loader = WebBaseLoader('https://docs.example.com') | |
| docs = loader.load() | |
| Splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) | |
| chunks = splitter.split_documents(docs) | |
| # 2. Embed and store vectorstore = | |
| Chroma.from_documents(chunks, OpenAIEmbeddings()) | |
| retriever = vectorstore.as_retriever(search_kwargs={'k': 4}) | |
| # 3. RAG chain via LCEL prompt = | |
| prompt = ChatPromptTemplate.from_template( | |
| 'Answer based on context:\n{context}\n\nQuestion: {question}' | |
| ) | |
| rag_chain = ( | |
| {'context': retriever, 'question': RunnablePassthrough()} | |
| | prompt | |
| | ChatOpenAI(model='gpt-4o-mini') | |
| | StrOutputParser() | |
| ) | |
| response = rag_chain.invoke('What is the deployment process?') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment