Budowanie łańcucha pobierania LCEL

Retrieval Augmented Generation (RAG) z LangChain

Meri Nova

Machine Learning Engineer

Przygotowanie danych do pobierania

Fragmenty dokumentów są zapisywane.

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

Dane wejściowe użytkownika.

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

Dane wejściowe wchodzące do łańcucha przez RunnablePassthrough i przypisywane do „question".

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

Dane wejściowe używane do zapytania retrievera i przypisywane do „context".

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

„context" i „question" integrowane w szablonie promptu.

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

Prompt integrowany z modelem LLM w celu wygenerowania odpowiedzi.

Retrieval Augmented Generation (RAG) z LangChain

Wprowadzenie do LCEL dla RAG

Wynik modelu przekazywany do parsera.

Retrieval Augmented Generation (RAG) z LangChain

Tworzenie instancji retrievera

vector_store = Chroma.from_documents(
    documents=chunks, 
    embedding=embedding_model
)


retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
Retrieval Augmented Generation (RAG) z LangChain

Tworzenie szablonu promptu

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template(""" Use the following pieces of context to answer the question at the end. If you don't know the answer, say that you don't know. Context: {context} Question: {question} """)
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...", temperature=0)
Retrieval Augmented Generation (RAG) z LangChain

Budowanie łańcucha pobierania LCEL

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

chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
Retrieval Augmented Generation (RAG) z LangChain

Wywoływanie łańcucha pobierania

result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Top Performance: RAG models set new records on open-domain question answering tasks...
- Better Generation: RAG models produce more specific, diverse, and factual language...
- Dynamic Knowledge Use: The non-parametric memory allows RAG models to access and ...
Retrieval Augmented Generation (RAG) z LangChain

Czas na ćwiczenia!

Retrieval Augmented Generation (RAG) z LangChain

Preparing Video For Download...