使用 LangChain 的 Retrieval Augmented Generation (RAG)
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)
- 顶级表现:RAG 模型在开放域问答上创下新纪录...
- 更佳生成:RAG 模型产出更具体、多样、事实更准确的语言...
- 动态知识利用:非参数化记忆让 RAG 模型能够访问并...
使用 LangChain 的 Retrieval Augmented Generation (RAG)