以下是两种方案的核心用法,结合你的技术栈(FastAPI + Elasticsearch + Neo4j):

方案一:LangChain + Claude API

安装

pip install langchain langchain-anthropic langchain-elasticsearch

基础 RAG 链

from langchain_anthropic import ChatAnthropic
from langchain_elasticsearch import ElasticsearchStore
from langchain.chains import RetrievalQA
from langchain_core.prompts import ChatPromptTemplate

# 1. 连接 Claude
llm = ChatAnthropic(
model=”claude-sonnet-4-20250514″,
api_key=”your_api_key”,
temperature=0.3
)

# 2. 连接你的 Elasticsearch 向量库
vectorstore = ElasticsearchStore(
es_url=”http://localhost:9200,
index_name=”ministry_docs”,
embedding=your_embedding_model
)

# 3. 定义 Prompt(可嵌入你的 Q五步框架)
prompt = ChatPromptTemplate.from_template(“””
你是服事话语分析助手。根据以下上下文回答问题。

上下文:{context}
问题:{question}
“””)

# 4. 组装 RAG 链
chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever(search_kwargs={“k”: 5}),
chain_type_kwargs={“prompt”: prompt}
)

# 5. 调用
result = chain.invoke({“query”: “神圣经纶的核心是什么?”})

接入 Neo4j(GraphRAG)

from langchain_community.graphs import Neo4jGraph
from langchain.chains import GraphCypherQAChain

graph = Neo4jGraph(
url=”bolt://localhost:7687″,
username=”neo4j”,
password=”your_password”
)

graph_chain = GraphCypherQAChain.from_llm(
llm=llm,
graph=graph,
verbose=True
)

result = graph_chain.invoke(“倪柝声与李常受的神学传承关系?”)

方案二:LlamaIndex + Claude API(更适合复杂 RAG)

安装

pip install llama-index llama-index-llms-anthropic llama-index-vector-stores-elasticsearch

基础用法

from llama_index.llms.anthropic import Anthropic
from llama_index.core import VectorStoreIndex, Settings
from llama_index.vector_stores.elasticsearch import ElasticsearchStore

# 1. 全局设置 Claude 为默认 LLM
Settings.llm = Anthropic(
model=”claude-sonnet-4-20250514″,
api_key=”your_api_key”
)

# 2. 连接 Elasticsearch
vector_store = ElasticsearchStore(
index_name=”ministry_docs”,
es_url=”http://localhost:9200
)

# 3. 建立索引
index = VectorStoreIndex.from_vector_store(vector_store)

# 4. 创建查询引擎
query_engine = index.as_query_engine(similarity_top_k=5)

response = query_engine.query(“基督作为生命的分赐如何体现?”)

混合检索(向量 + 关键词)

from llama_index.core.retrievers import QueryFusionRetriever
from llama_index.retrievers.bm25 import BM25Retriever

# 融合向量检索 + BM25 关键词检索
retriever = QueryFusionRetriever(
[vector_retriever, bm25_retriever],
similarity_top_k=5,
num_queries=3, # 自动生成多角度查询
mode=”reciprocal_rerank”
)

接入 FastAPI(生产部署)

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class QueryRequest(BaseModel):
question: str
top_k: int = 5

@app.post(“/ministry/query”)
async def query_ministry(req: QueryRequest):
# 可在此切换 LangChain 或 LlamaIndex 引擎
result = await chain.ainvoke({“query”: req.question})
return {
“answer”: result[“result”],
“sources”: result.get(“source_documents”, [])
}

两者对比(针对你的项目)

|维度 |LangChain |LlamaIndex |
|——–|——————–—–|——————|
|Neo4j 集成|✅ 原生支持 GraphCypherQAChain|✅ 支持但需自定义 |
|多步 Agent|✅ 强,工具链灵活 |✅ 较强,QueryPipeline|
|文档解析/分块 |一般 |**更强**,内置多种策略 |
|学习曲线 |较陡 |相对平缓 |
|适合 Q五步框架|Agent + Tools 实现 |QueryPipeline 实现 |

建议: 用 LlamaIndex 做检索层(文档处理更精细),用 LangChain 做 Agent 层(工具编排更灵活),两者可混用,共同接 Claude API。

需要我写一个完整的 PanAI RAG 架构示例吗?