主题
注意:这部分仍在完善中,敬请留意后续更新!
ChatGPT 检索插件集成
OpenAI ChatGPT Retrieval Plugin 为任意文档存储系统与 ChatGPT 交互提供了中心化的 API 规范。由于它可以部署在任意服务上,这意味着越来越多的文档检索服务会实现这一规范;这不仅让它们能与 ChatGPT 交互,也能与任何使用检索服务的 LLM 工具库交互。
LlamaIndex 提供了与 ChatGPT 检索插件的多种集成方式。
把 LlamaHub 的数据加载进 ChatGPT 检索插件
ChatGPT 检索插件定义了供用户加载文档的 /upsert 端点。这与 LlamaHub 天然可以对接——LlamaHub 为各种 API 与文档格式提供了 65 个以上的数据加载器。
下面这段代码演示如何把 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"))更多细节请查看完整示例 notebook。
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",
)更多细节请查看完整示例 notebook。
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?")更多细节请查看完整示例 notebook。