跳转到内容

ChatGPT 插件集成

注意: 这是一个正在开发中的项目,敬请期待更多激动人心的更新!

OpenAI ChatGPT 检索插件 为任何文档存储系统提供了与ChatGPT交互的集中式API规范。由于该插件可部署在任何服务上,这意味着越来越多的文档检索服务将实现此规范;这使得它们不仅能与ChatGPT交互,还能与任何可能使用检索服务的大语言模型工具包进行交互。

LlamaIndex 提供了与 ChatGPT 检索插件的多种集成。

ChatGPT检索插件为用户定义了一个/upsert端点来加载文档。这为与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"))

更多详情,请查看完整示例笔记本

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端点的文档存储支持。

注意:此索引是一个向量索引,支持 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?")

更多详情,请查看完整示例笔记本