ollama + langchain + FAISS 向量数据库,给定知识上下文的问答

ollama + langchain + FAISS 给定知识上下文的问答

基于 langchain 框架

1 把给定的文档向量化存储为数据库

2 生成向量查询

3 基于上面查询提供语言模型 promt

4 语言模型生成答案

python 复制代码
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import Ollama
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain

# 从url导入知识作为聊天背景上下文
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")
#加载
docs = loader.load()

# 文本分词器
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
# ollama嵌入层
embeddings = OllamaEmbeddings()
# 文档向量化
vector = FAISS.from_documents(documents, embeddings)


# 创建ollama 模型 llama2
llm = Ollama(model="llama2")
output_parser = StrOutputParser()

# 创建提示词模版
prompt = ChatPromptTemplate.from_template(
        """Answer the following question based only on the provided context:
        <context>
        {context}
        </context>
        Question: {input}"""
    )
# 生成chain :   prompt | llm 
document_chain = create_stuff_documents_chain(llm, prompt)

# 向量数据库检索器
retriever = vector.as_retriever()
#向量数据库检索chain :  vector | prompt | llm  
retrieval_chain = create_retrieval_chain(retriever, document_chain)

# 调用上面的 (向量数据库检索chain)
response = retrieval_chain.invoke({"input": "how can langsmith help with testing?"})
# 打印结果
print(response["answer"])
相关推荐
GISer_Jing14 小时前
GitHub热点I
后端·ai·langchain
M--Y16 小时前
LangGraph 架构基石:持久化与运行控制
python·langchain·大模型·llm·持久化·langgraph
城北徐宫16 小时前
MySQL数据类型整理
数据库·mysql
xyphf_和派孔明17 小时前
第十章: LangGraph的人机交互Human-in-the-loop实践
langchain·llm
数据库小学妹17 小时前
数据库容量规划:四个数与扩容触发线
运维·数据库·后端
时间的拾荒人17 小时前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
纆兰20 小时前
MySQL的多表查询
数据库·mysql
七夜zippoe20 小时前
Nexent 模型配置:4 字段与 9 个槽位
数据库·人工智能·模型·nexent
进击的荆棘20 小时前
MySQL——表的操作
数据库·mysql
xyphf_和派孔明21 小时前
第六章: LangGraph的工具调用代理-Tool Calling Agent
langchain·llm