跳到主要內容
Open In ColabOpen on GitHub

Psychic

本筆記本涵蓋如何從 Psychic 載入文件。請參閱此處以瞭解更多詳細資訊。

先決條件

  1. 請依照本文件中的「快速入門」章節
  2. 登入 Psychic 儀表板並取得您的密鑰
  3. 將前端 react 函式庫安裝到您的 Web 應用程式中,並讓使用者驗證連線。連線將使用您指定的連線 ID 建立。

載入文件

使用 PsychicLoader 類別從連線中載入文件。每個連線都有一個連接器 ID(對應於已連線的 SaaS 應用程式)和一個連線 ID(您傳遞到前端函式庫中的 ID)。

# Uncomment this to install psychicapi if you don't already have it installed
!poetry run pip -q install psychicapi langchain-chroma

[notice] A new release of pip is available: 23.0.1 -> 23.1.2
[notice] To update, run: pip install --upgrade pip
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)

此頁面是否有幫助?