ChatGPT 插件集成#
注意:此功能正在开发中,敬请期待更多激动人心的更新!
ChatGPT 检索插件集成#
OpenAI ChatGPT 检索插件为文档存储系统与 ChatGPT 交互提供了标准化的 API 规范。由于该插件可部署在任何服务上,这意味着越来越多的文档检索服务将实现此规范,使其不仅能与 ChatGPT 交互,还能与任何可能使用检索服务的大语言模型工具包兼容。
LlamaIndex 提供了多种与 ChatGPT 检索插件的集成方式。
从 LlamaHub 加载数据到 ChatGPT 检索插件#
ChatGPT 检索插件定义了 /upsert 端点用于加载文档。这为 LlamaHub 提供了天然的集成点——LlamaHub 提供超过 65 种支持不同 API 和文档格式的数据加载器。
以下示例代码展示如何将 LlamaHub 中的文档转换为 /upsert 端点所需的 JSON 格式:
from llama_index.core import download_loader, Document
from typing import Dict, List
import json
# download loader, load documents
from llama_index.readers.web import SimpleWebPageReader
loader = SimpleWebPageReader(html_to_text=True)
url = "http://www.paulgraham.com/worked.html"
documents = loader.load_data(urls=[url])
# Convert LlamaIndex Documents to JSON format
def dump_docs_to_json(documents: List[Document], out_path: str) -> Dict:
"""Convert LlamaIndex Documents to JSON format and save it."""
result_json = []
for doc in documents:
cur_dict = {
"text": doc.get_text(),
"id": doc.get_doc_id(),
# NOTE: feel free to customize the other fields as you wish
# fields taken from https://github.com/openai/chatgpt-retrieval-plugin/tree/main/scripts/process_json#usage
# "source": ...,
# "source_id": ...,
# "url": url,
# "created_at": ...,
# "author": "Paul Graham",
}
result_json.append(cur_dict)
json.dump(result_json, open(out_path, "w"))
更多细节请查看完整示例笔记本。
ChatGPT 检索插件数据加载器#
ChatGPT 检索插件数据加载器可在 LlamaHub 获取,它能将任何实现该插件 API 的文档存储数据轻松加载到 LlamaIndex 数据结构中。
示例代码:
from llama_index.readers.chatgpt_plugin import ChatGPTRetrievalPluginReader
import os
# load documents
bearer_token = os.getenv("BEARER_TOKEN")
reader = ChatGPTRetrievalPluginReader(
endpoint_url="http://localhost:8000", bearer_token=bearer_token
)
documents = reader.load_data("What did the author do growing up?")
# build and query index
from llama_index.core import SummaryIndex
index = SummaryIndex.from_documents(documents)
# set Logging to DEBUG for more detailed outputs
query_engine = vector_index.as_query_engine(response_mode="compact")
response = query_engine.query(
"Summarize the retrieved content and describe what the author did growing up",
)
更多细节请查看完整示例笔记本。
ChatGPT 检索插件索引#
ChatGPT 检索插件索引可让您轻松构建文档向量索引,其存储后端由实现 ChatGPT 端点的文档存储支持。
注:该索引为向量索引,支持 top-k 检索。
示例代码:
from llama_index.core.indices.vector_store import ChatGPTRetrievalPluginIndex
from llama_index.core import SimpleDirectoryReader
import os
# load documents
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
# build index
bearer_token = os.getenv("BEARER_TOKEN")
# initialize without metadata filter
index = ChatGPTRetrievalPluginIndex(
documents,
endpoint_url="http://localhost:8000",
bearer_token=bearer_token,
)
# query index
query_engine = vector_index.as_query_engine(
similarity_top_k=3,
response_mode="compact",
)
response = query_engine.query("What did the author do growing up?")
更多细节请查看完整示例笔记本。