跳过内容

ChatGPT 插件集成#

注意:这是一个正在进行的工作,请继续关注此方面的更多精彩更新!

ChatGPT 检索插件集成#

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

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