检索:从常识库中提炼取用户成就相干的疑息片断(如颠末背质类似性搜刮)。
retriever = vectorstore.as_retriever()增强:将检索成果取用户成就分离组成高低文。from langchain.prompts import ChatPromptTemplatetemplate = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.Question: {question} Context: {context} Answer:"""prompt = ChatPromptTemplate.from_template(template)print(prompt)天生:LLM鉴于高低文天生终极谜底。from langchain.chat_models import ChatOpenAIfrom langchain.schema.runnable import RunnablePassthroughfrom langchain.schema.output_parser import StrOutputParserllm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)rag_chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() )query = "What did the president say about the business? "rag_chain.invoke(query)