灵媒
此笔记本介绍如何从 Psychic
加载文档。有关更多详细信息,请参阅此处。
先决条件
- 请遵循此文档中的快速入门部分
- 登录Psychic 仪表板并获取您的密钥
- 将前端 react 库安装到您的 Web 应用中,并让用户验证连接。连接将使用您指定的连接 ID 创建。
加载文档
使用 PsychicLoader
类从连接中加载文档。每个连接都有一个连接器 ID(对应于已连接的 SaaS 应用)和一个连接 ID(您传递给前端库)。
# Uncomment this to install psychicapi if you don't already have it installed
!poetry run pip -q install psychicapi langchain-chroma
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
from langchain_community.document_loaders import PsychicLoader
from psychicapi import ConnectorId
# Create a document loader for google drive. We can also load from other connectors by setting the connector_id to the appropriate value e.g. ConnectorId.notion.value
# This loader uses our test credentials
google_drive_loader = PsychicLoader(
api_key="7ddb61c1-8b6a-4d31-a58e-30d1c9ea480e",
connector_id=ConnectorId.gdrive.value,
connection_id="google-test",
)
documents = google_drive_loader.load()
API 参考:PsychicLoader
将文档转换为嵌入
现在,我们可以将这些文档转换为嵌入并将其存储在像 Chroma 这样的向量数据库中。
from langchain.chains import RetrievalQAWithSourcesChain
from langchain_chroma import Chroma
from langchain_openai import OpenAI, OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings)
chain = RetrievalQAWithSourcesChain.from_chain_type(
OpenAI(temperature=0), chain_type="stuff", retriever=docsearch.as_retriever()
)
chain({"question": "what is psychic?"}, return_only_outputs=True)