ChatGPT 插件集成
注意: 这是一个正在开发中的项目,敬请期待更多激动人心的更新!
ChatGPT 检索插件集成
Section titled “ChatGPT Retrieval Plugin Integrations”OpenAI ChatGPT 检索插件 为任何文档存储系统提供了与ChatGPT交互的集中式API规范。由于该插件可部署在任何服务上,这意味着越来越多的文档检索服务将实现此规范;这使得它们不仅能与ChatGPT交互,还能与任何可能使用检索服务的大语言模型工具包进行交互。
LlamaIndex 提供了与 ChatGPT 检索插件的多种集成。
将数据从LlamaHub加载到ChatGPT检索插件中
Section titled “Loading Data from LlamaHub into the ChatGPT Retrieval Plugin”ChatGPT检索插件为用户定义了一个/upsert端点来加载文档。这为与LlamaHub的集成提供了天然接口,后者提供来自各种API和文档格式的超过65种数据加载器。
以下是一个示例代码片段,展示如何从 LlamaHub 加载文档并转换为 /upsert 所需的 JSON 格式:
from llama_index.core import download_loader, Documentfrom typing import Dict, Listimport json
# download loader, load documentsfrom 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 formatdef 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 检索插件数据加载器
Section titled “ChatGPT Retrieval Plugin Data Loader”ChatGPT检索插件数据加载器可在LlamaHub上访问。
它允许您轻松从任何实现插件API的文档存储中加载数据到LlamaIndex数据结构中。
示例代码:
from llama_index.readers.chatgpt_plugin import ChatGPTRetrievalPluginReaderimport os
# load documentsbearer_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 indexfrom llama_index.core import SummaryIndex
index = SummaryIndex.from_documents(documents)# set Logging to DEBUG for more detailed outputsquery_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 检索插件索引
Section titled “ChatGPT Retrieval Plugin Index”ChatGPT检索插件索引允许您轻松构建任何文档的向量索引,存储由实现ChatGPT端点的文档存储支持。
注意:此索引是一个向量索引,支持 top-k 检索。
示例代码:
from llama_index.core.indices.vector_store import ChatGPTRetrievalPluginIndexfrom llama_index.core import SimpleDirectoryReaderimport os
# load documentsdocuments = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
# build indexbearer_token = os.getenv("BEARER_TOKEN")# initialize without metadata filterindex = ChatGPTRetrievalPluginIndex( documents, endpoint_url="http://localhost:8000", bearer_token=bearer_token,)
# query indexquery_engine = vector_index.as_query_engine( similarity_top_k=3, response_mode="compact",)response = query_engine.query("What did the author do growing up?")更多详情,请查看完整示例笔记本。