使用 Langchain 的检索与 .from_llm 或定义 LLMChain 有什么区别?

问题描述 投票:0回答:1

在文档中,我看到了两种构造模式,我对两者之间的区别有点困惑。我不知道是否有任何实际差异,或者只是不同方法中的同一件事。

示例 1(使用 .from_llm):

model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)
embeddings = OpenAIEmbeddings()
vectordb = Chroma(embedding_function=embeddings, persist_directory=directory)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(
    model,
    vectordb.as_retriever(),
    condense_question_prompt=CUSTOM_QUESTION_PROMPT,
    memory=memory
)
query = "What did the president say about Ketanji Brown Jackson"
result = qa({"question": query})

示例2(无方法,使用LLMChain):

llm = OpenAI(temperature=0)
question_generator = LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT)
doc_chain = load_qa_with_sources_chain(llm, chain_type="map_reduce")

chain = ConversationalRetrievalChain(
    retriever=vectorstore.as_retriever(),
    question_generator=question_generator,
    combine_docs_chain=doc_chain,
)
chat_history = []
query = "What did the president say about Ketanji Brown Jackson"
result = chain({"question": query, "chat_history": chat_history})

我可以看到,在第二个示例中,我将模型和提示分别定义为 Question_generator,然后将其作为参数传递到链中。因此,我传递了一个 doc_chain。

但是我有一些抽象的疑问,例如下面是什么,后果是什么,我什么时候应该使用一个或另一个,如果它是相同的输出等等。大多数情况下,是第一个简单的例子以某种方式使用question_generator或doc_chain,它们是代码中省略了?

python openai-api information-retrieval langchain llm
1个回答
0
投票

=> .from_llm: 这种方法更加高级和抽象。 .from_llm 是一种工厂方法,它使用一些预定义的设置来预先配置给定 LLM(语言模型)的链。 主要优点是简单且犯错的空间较小,特别是对于不想深入了解细节的用户。

=>(无方法,使用 LLMChain): 这种方法更加细粒度和明确。 您正在显式构建链的每个部分,然后将它们组装在一起。 通过分离 llm、question_generator 和 doc_chain,您可以拥有更多控制权和灵活性。您可以根据您的要求定制每个部分。 此方法适用于希望对链的行为进行细粒度控制的用户,但可能会增加复杂性。

下图:底层机制可能相似,但两种方法之间如何使用模型、如何生成问题以及如何组合文档的具体细节可能有所不同,特别是根据所使用的配置。

© www.soinside.com 2019 - 2024. All rights reserved.