汪图南
  • RAG

    • RAG
  • 快速入门
  • 高级技巧
前端面试之道
  • 打包工具

    • Webpack
    • Rollup
  • TypeScript

    • TypeScript基础
    • TypeScript类型挑战
  • CSS预编译器

    • SASS
  • 自动化测试

    • Vue应用测试
  • Vue2.0源码分析
  • Vue3.0源码分析
  • 数据结构和算法(基础)
  • LeetCode(刷题)
  • JavaScript书籍

    • 你不知道的JavaScript(上)
    • 你不知道的JavaScript(中下)
    • JavaScript数据结构和算法
    • JavaScript设计模式与开发实践
    • 深入理解ES6
  • Git书籍

    • 精通Git
Github
  • RAG

    • RAG
  • 快速入门
  • 高级技巧
前端面试之道
  • 打包工具

    • Webpack
    • Rollup
  • TypeScript

    • TypeScript基础
    • TypeScript类型挑战
  • CSS预编译器

    • SASS
  • 自动化测试

    • Vue应用测试
  • Vue2.0源码分析
  • Vue3.0源码分析
  • 数据结构和算法(基础)
  • LeetCode(刷题)
  • JavaScript书籍

    • 你不知道的JavaScript(上)
    • 你不知道的JavaScript(中下)
    • JavaScript数据结构和算法
    • JavaScript设计模式与开发实践
    • 深入理解ES6
  • Git书籍

    • 精通Git
Github
  • RAG基础

    • 介绍
  • 简易RAG

    • 前置准备
    • LangChain实现RAG
    • LangGraph实现RAG
    • LangChain vs LangGraph
  • 数据导入(Loader)

    • 介绍
    • Document类型
    • 简单文本导入
    • JSON
    • HTML网页数据
    • Markdown
    • CSV
    • OCR
    • PDF
    • DataBase
  • 文本切块(Chunking)

    • 介绍
    • 分块方法
  • 数据嵌入(Embedding)

    • 介绍
    • 向量相似度计算
    • 稀疏嵌入
    • 密集嵌入
    • 混合嵌入
  • 向量存储(VectorStore)

    • 介绍
    • Chroma
    • Milvus

LangChain实现RAG

安装langchain_community包,并使用loader加载网页数据。

from langchain_community.document_loaders import WebBaseLoader

# 加载网页数据
web_data = WebBaseLoader(
  web_path="https://zh.wikipedia.org/wiki/黑神话:悟空"
).load()

print(web_data)

安装langchain_text_splitters包,并使用分割器分割网页数据。

from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
  chunk_size=1000,
  chunk_overlap=200
)
text_split_data = text_splitter.split_documents(web_data)

print(text_split_data)

安装langchain-huggingface,并设置embedding。

from langchain_huggingface import HuggingFaceEmbeddings

embedding = HuggingFaceEmbeddings(
  model_name="BAAI/bge-small-zh-v1.5"
)

安装langchain_core,并设置向量存储。

from langchain_core.vectorstores import InMemoryVectorStore

vector_store = InMemoryVectorStore(embeddings)
vector_store.add_documents(text_split_data)

至此,网页数据已经向量化存入到我们的内存中。

存入内存以后,需要做检索相关的工作。

# retriever
question = "黑神话是哪家公司开发的,是什么类型的游戏,什么时间发布的?"
retriever_docs = vector_store.similarity_search(
  query=question,
  k=2
)
retriever_content = "\n\n".join(doc.page_content for doc in retriever_docs)

print(retriever_content)

向量存储检索完毕后,接下来要准备检索内容放置到提示词中。

# prompt
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("""
  请你依据上下文来回答问题\n
  如果上下文中没有,你直接回答"抱歉,我无法从上下文中提供相关信息。"
  上下文:{context}
  问题:{question}
  回答:
""")
query = prompt.format(
  context=retriever_content,
  question=question
)

print(query)

最后,安装langchain_deepseek,并调用大模型api得到结果。

llm = ChatDeepSeek(
  model="deepseek-chat",
  api_key=api_key,
  temperature=0.5
)
response = llm.invoke(
  input=query
)

print(response.content)

最后回答:

1. **开发公司**:游戏科学(Game Science)  
2. **游戏类型**:动作角色扮演(ARPG)  
3. **发布时间**:  
   - Microsoft Windows 和 PlayStation 5 平台:**2024年8月20日**  
   - Xbox Series X/S 版:**待定**(暂未发售)  

其他补充:  
- 中国新华社称其为“中国首款3A游戏”,改编自《西游记》。  
- 首发3天销量破1000万份,一个月内销量超2000万份。
最后更新时间: 2025/5/31 05:05
贡献者: wangtunan
Prev
前置准备
Next
LangGraph实现RAG