ChatGPT 플러그인 통합

ChatGPT 플러그인 통합 (ChatGPT Plugin Integrations)

ChatGPT Retrieval Plugin은 어떤 문서 저장 시스템이든 ChatGPT와 상호작용할 수 있게 해주는 통합 API예요. LlamaIndex가 이 플러그인과 어떻게 연동되는지 알아봅시다.

출처: 문서

본문

참고: 이 문서는 작업 중(work-in-progress)이며, 더 흥미로운 업데이트를 기대해 주세요!

ChatGPT Retrieval Plugin 통합

OpenAI ChatGPT Retrieval Plugin은 모든 문서 저장 시스템이 ChatGPT와 상호작용할 수 있도록 중앙화된 API 명세를 제공합니다. 어느 서비스에나 배포할 수 있기 때문에, 점점 더 많은 문서 검색 서비스가 이 명세를 구현하게 될 것이며, 이를 통해 ChatGPT뿐 아니라 검색 서비스를 사용할 수 있는 모든 LLM 툴킷과도 상호작용할 수 있게 됩니다.

LlamaIndex는 ChatGPT Retrieval Plugin과 다양한 통합을 제공합니다.

LlamaHub에서 ChatGPT Retrieval Plugin으로 데이터 로드하기

ChatGPT Retrieval Plugin은 사용자가 문서를 로드할 수 있도록 /upsert 엔드포인트를 정의합니다. 이는 다양한 API와 문서 형식에서 65개 이상의 데이터 로더를 제공하는 LlamaHub와 자연스러운 통합 지점이 됩니다.

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 Retrieval Plugin 데이터 로더

ChatGPT Retrieval Plugin 데이터 로더는 LlamaHub에서 접근할 수 있습니다.

이를 통해 플러그인 API를 구현하는 어떤 docstore에서든 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 Retrieval Plugin 인덱스

ChatGPT Retrieval Plugin Index를 사용하면 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?")

더 자세한 내용은 전체 예제 노트북을 확인하세요.

더 알아보기 (Learn more)