ChatGPT 外掛程式
OpenAI plugins 將
ChatGPT
連接到第三方應用程式。 這些外掛程式使ChatGPT
能夠與開發人員定義的 API 互動,從而增強ChatGPT
的功能並使其能夠執行各種操作。
外掛程式允許
ChatGPT
執行以下操作:
- 檢索即時資訊;例如:體育賽事比分、股票價格、最新新聞等。
- 檢索知識庫資訊;例如:公司文件、個人筆記等。
- 代表使用者執行操作;例如:預訂航班、訂購食物等。
此筆記本示範如何在 LangChain 中使用 ChatGPT Retriever Plugin。
# STEP 1: Load
# Load documents using LangChain's DocumentLoaders
# This is from https://langchain.readthedocs.io/en/latest/modules/document_loaders/examples/csv.html
from langchain_community.document_loaders import CSVLoader
from langchain_core.documents import Document
loader = CSVLoader(
file_path="../../document_loaders/examples/example_data/mlb_teams_2012.csv"
)
data = loader.load()
# STEP 2: Convert
# Convert Document to format expected by https://github.com/openai/chatgpt-retrieval-plugin
import json
from typing import List
def write_json(path: str, documents: List[Document]) -> None:
results = [{"text": doc.page_content} for doc in documents]
with open(path, "w") as f:
json.dump(results, f, indent=2)
write_json("foo.json", data)
# STEP 3: Use
# Ingest this as you would any other json file in https://github.com/openai/chatgpt-retrieval-plugin/tree/main/scripts/process_json
使用 ChatGPT Retriever Plugin
好的,我們已經建立了 ChatGPT Retriever Plugin,但我們實際上該如何使用它呢?
以下程式碼逐步說明如何執行此操作。
我們想要使用 ChatGPTPluginRetriever
,因此我們必須取得 OpenAI API 金鑰。
import getpass
import os
if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
OpenAI API Key: ········
from langchain_community.retrievers import (
ChatGPTPluginRetriever,
)
API 參考:ChatGPTPluginRetriever
retriever = ChatGPTPluginRetriever(url="http://0.0.0.0:8000", bearer_token="foo")
retriever.invoke("alice's phone number")
[Document(page_content="This is Alice's phone number: 123-456-7890", lookup_str='', metadata={'id': '456_0', 'metadata': {'source': 'email', 'source_id': '567', 'url': None, 'created_at': '1609592400.0', 'author': 'Alice', 'document_id': '456'}, 'embedding': None, 'score': 0.925571561}, lookup_index=0),
Document(page_content='This is a document about something', lookup_str='', metadata={'id': '123_0', 'metadata': {'source': 'file', 'source_id': 'https://example.com/doc1', 'url': 'https://example.com/doc1', 'created_at': '1609502400.0', 'author': 'Alice', 'document_id': '123'}, 'embedding': None, 'score': 0.6987589}, lookup_index=0),
Document(page_content='Team: Angels "Payroll (millions)": 154.49 "Wins": 89', lookup_str='', metadata={'id': '59c2c0c1-ae3f-4272-a1da-f44a723ea631_0', 'metadata': {'source': None, 'source_id': None, 'url': None, 'created_at': None, 'author': None, 'document_id': '59c2c0c1-ae3f-4272-a1da-f44a723ea631'}, 'embedding': None, 'score': 0.697888613}, lookup_index=0)]