Langchain中向量数据库FAISS的使用
Langchain中向量数据库FAISS的使用
·
Embeddings 使用的是 JinaEmbeddings。
1 第一次存入数据库:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.embeddings import JinaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.llms import Tongyi
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
import os
os.environ["DASHSCOPE_API_KEY"] = "sk-cc1c8314fdbd43ceaf26ec1824d5dd3b"
llm = Tongyi()
from langchain_community.document_loaders import UnstructuredURLLoader
urls = [
"https://en.wikipedia.org/wiki/Android_(operating_system)"
]
loader = UnstructuredURLLoader(urls=urls)
documents = loader.load_and_split()
print(documents)
embeddings = JinaEmbeddings(
jina_api_key="jina_c5d02a61c97d4d79b88234362726e94aVLMTvF38wvrElYqpGYSxFtC5Ifhj", model_name="jina-embeddings-v2-base-en"
)
# # 第一次存入本地
vectorstore = FAISS.from_documents(documents, embeddings)
vectorstore.save_local("faiss_index")
# # 从本地加载
# vectorstore = FAISS.load_local("faiss_index", embeddings)
retriever = vectorstore.as_retriever()
template = """Answer the question based on the context below. If the
question cannot be answered using the information provided answer
with "I don't know"
Context: {context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
output_parser = StrOutputParser()
setup_and_retrieval = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
)
chain = setup_and_retrieval | prompt | llm | output_parser
print(chain.invoke("what is android"))
2 第二次从本地加载:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.embeddings import JinaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.llms import Tongyi
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
import os
os.environ["DASHSCOPE_API_KEY"] = "sk-cc1c8314fdbd43ceaf26ec1824d5dd3b"
llm = Tongyi()
from langchain_community.document_loaders import UnstructuredURLLoader
# urls = [
# "https://en.wikipedia.org/wiki/Android_(operating_system)"
# ]
# loader = UnstructuredURLLoader(urls=urls)
# documents = loader.load_and_split()
# print(documents)
embeddings = JinaEmbeddings(
jina_api_key="jina_c5d02a61c97d4d79b88234362726e94aVLMTvF38wvrElYqpGYSxFtC5Ifhj", model_name="jina-embeddings-v2-base-en"
)
# # 第一次存入本地
# vectorstore = FAISS.from_documents(documents, embeddings)
# vectorstore.save_local("faiss_index")
# # 从本地加载
vectorstore = FAISS.load_local("faiss_index", embeddings)
retriever = vectorstore.as_retriever()
template = """Answer the question based on the context below. If the
question cannot be answered using the information provided answer
with "I don't know"
Context: {context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
output_parser = StrOutputParser()
setup_and_retrieval = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
)
chain = setup_and_retrieval | prompt | llm | output_parser
print(chain.invoke("what is android"))
更多推荐
已为社区贡献2条内容
所有评论(0)