You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将LangChain的ConversationalRetrievalChain集成到llama_index聊天机器人

解决方案:集成LangChain ConversationalRetrievalChain与llama_index向量存储

核心思路

要实现集成,关键是将llama_index构建的向量索引转换为LangChain兼容的检索器(Retriever),然后基于该检索器构建ConversationalRetrievalChain,同时保留原有对话记忆、语言检测与翻译逻辑。

修改步骤与完整代码

1. 安装依赖

确保安装LangChain与llama_index的集成包:

pip install langchain-llama-index

2. 修改后的完整代码

import os
import openai
import gradio as gr
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationalRetrievalChain
from llama_index import StorageContext, load_index_from_storage
from langchain.llms import OpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import LlamaIndex
from translate import Translator
from langdetect import detect

openai.api_key = os.environ['OPENAI_API_KEY']

# 初始化LLM与Embedding模型
llm = OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=200)
embeddings = OpenAIEmbeddings()

def create_langchain_retriever():
    # 加载llama_index的存储上下文与索引
    storage_context = StorageContext.from_defaults(persist_dir="store_final")
    index = load_index_from_storage(storage_context)
    # 将llama_index索引转换为LangChain兼容的向量存储
    langchain_vectorstore = LlamaIndex(index, embeddings=embeddings)
    # 转换为Retriever,设置每次检索返回的文档数量(可按需调整)
    retriever = langchain_vectorstore.as_retriever(search_kwargs={"k": 3})
    return retriever

# 初始化Retriever与对话链
retriever = create_langchain_retriever()
memory = ConversationBufferWindowMemory(k=15, memory_key="chat_history", return_messages=True)
qa_chain = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=retriever,
    memory=memory,
    verbose=True
)

has_replied = False

def detect_and_translate(text):
    truncated_text = text[:500]
    detected_lang = detect(truncated_text)
    if detected_lang == 'en':
        return truncated_text
    translator = Translator(from_lang="en", to_lang=detected_lang)
    return translator.translate(truncated_text)

def click_response(message, history):
    if not message.strip():
        return " "
    
    greetings = ["hi", "hello", "hey", "greetings", "good day", "good morning", "good evening", "good afternoon", "hola", "hallo", "bonjour", "ciao", "witaj", "hej", "howdy", "cześć", "hejo"]
    if any(greeting in message.lower() for greeting in greetings) and len(message.split()) <= 2:
        return "Hi! Thank you for contacting Company. I am a Company bot created to answer your product questions. How can I help you today?"
    
    # 替换人称代词,避免LLM混淆指代
    message = message.replace(" you ", " Company ")
    message = message.replace(" your ", " Company's ")
    message = message.replace(" yours ", " Company's ")
    
    global has_replied
    initial_greeting = ""
    if not has_replied:
        initial_greeting = "Hi! Thank you for contacting Company. I am a Company bot created to answer your product questions. "
        has_replied = True
    
    # 使用ConversationalRetrievalChain获取带检索的对话回答
    result = qa_chain({"question": message})
    retrieved_response = result["answer"]
    
    # 处理无效/空响应
    if len(retrieved_response.strip()) < 20:
        retrieved_response = ("I’m sorry, I don’t know the answer to your question.😔 Please send an email to "
                             "support@Company.com with your query and our team will get back to you as soon as "
                             "possible.📧 Thank you for your patience and understanding. ")
    
    # 替换内部邮箱为官方支持邮箱
    retrieved_response = retrieved_response.replace("mciszewska@Company.com", "support@Company.com")
    
    # 语言检测与翻译处理
    detected_lang = detect(retrieved_response)
    translated_response = detect_and_translate(retrieved_response)
    
    # 添加结束语
    closing_statement = " Please let me know if you have any further questions at this moment?"
    if detected_lang != 'en':
        translated_closing = Translator(from_lang="en", to_lang=detected_lang).translate(closing_statement)
        final_response = initial_greeting + translated_response + translated_closing
    else:
        final_response = initial_greeting + retrieved_response + closing_statement
    
    return final_response

demo = gr.ChatInterface(
    click_response,
    title="Company Bot",
    theme="JohnSmith9982/small_and_pretty",
    retry_btn=None,
    description="Hi! I am a Company bot created to answer your product questions. How can I help you today?",
)

if __name__ == "__main__":
    demo.launch()

关键修改点说明

  • 向量存储转换:通过LlamaIndex类将llama_index的索引转成LangChain兼容的向量存储,再生成Retriever供对话链调用。
  • 对话链替换:用ConversationalRetrievalChain替代原ConversationChain,该链自动结合对话记忆和检索到的文档生成精准回答。
  • 记忆优化:设置return_messages=True让记忆以消息对象形式存储,适配ConversationalRetrievalChain的输入要求。
  • 逻辑兼容:保留原有问候语处理、人称替换、翻译和无效响应处理逻辑,确保原有功能不受影响。

注意事项

  • 确保store_final目录下的四个json文件路径正确。
  • 若遇版本兼容问题,可指定langchain-llama-index==0.0.2适配新llama_index版本。

内容的提问来源于stack exchange,提问作者Renata Ka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 03:09:54