Retrieval Augmented Generation (RAG) với LangChain
Meri Nova
Machine Learning Engineer







vector_store = Chroma.from_documents( documents=chunks, embedding=embedding_model )retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
from langchain_core.prompts import ChatPromptTemplateprompt = 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)
from langchain_core.runnables import RunnablePassthrough from langchain_core.output_parsers import StrOutputParserchain = ({"context": retriever, "question": RunnablePassthrough()}| prompt| llm| StrOutputParser())
result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Hiệu năng hàng đầu: các mô hình RAG lập kỷ lục mới trên tác vụ trả lời câu hỏi miền mở...
- Sinh ngôn ngữ tốt hơn: mô hình RAG tạo ra ngôn ngữ cụ thể, đa dạng và chính xác hơn...
- Khai thác tri thức động: bộ nhớ phi tham số cho phép mô hình RAG truy cập và ...
Retrieval Augmented Generation (RAG) với LangChain