ChatGPT Retrieval Plugin

repository·main·Indexed 12 days ago

https://github.com/openai/chatgpt-retrieval-plugin

A standalone backend for Retrieval-Augmented Generation (RAG) that enables semantic search and document retrieval for Custom GPTs, function calling, and Assistants APIs. It provides granular control over embedding models, chunking strategies, and vector database providers, with support for Elasticsearch and Qdrant via Docker. The plugin includes /upsert and /query endpoints to manage and retrieve document chunks.

Tokens
34.7K
Snippets
89
Records
127
Agent score
96%

What's inside ChatGPT Retrieval Plugin

  1. What is the ChatGPT Retrieval Plugin?

    main

    The ChatGPT Retrieval Plugin is a standalone retrieval backend designed for Retrieval-Augmented Generation (RAG). It enables models to perform semantic search and retrieval of personal or organizational documents (files, notes, emails, etc.) by using OpenAI's embeddings models to store document chunks in a vector database.

    It can be used with:

    • ChatGPT Custom GPTs (via Actions)
    • Function Calling with the Chat Completions API
    • Assistants API
    • ChatGPT Plugins model (deprecated)

    Developers can self-host the plugin using Docker on platforms like Fly.io, Heroku, Render, or Azure Container Apps. It is particularly useful when you need more granular control over your retrieval system (e.g., embedding chunk length, specific embedding models, or self-hosting data) than ChatGPT's native file upload feature provides.

  2. Process a JSONL file into a vector database

    main

    The process_jsonl.py script is a utility designed to ingest a JSONL file dump of documents and store them in a vector database along with metadata.

    Key Features:

    • Batch Upsert: Processes documents as a generator and performs batch upserts to the database.
    • PII Screening: Optionally uses a language model to detect and skip documents containing personally identifiable information (PII).
    • Automated Metadata Extraction: Optionally uses a language model to extract additional metadata directly from the document text.
    • Customization: You can override the PII detection logic in services/pii_detection.py or the metadata extraction logic in services/extract_metadata.py to suit specific requirements.
  3. JSONL File Format Requirements

    main

    The input file must be a newline-delimited JSON (JSONL) file where each line is a valid JSON object representing a document.

    Supported Fields:

    • text (Required): The content of the document.
    • id (Optional): The document identifier. If omitted, a random UUID will be generated.
    • source (Optional): Metadata field.
    • source_id (Optional): Metadata field.
    • url (Optional): Metadata field.
    • created_at (Optional): Metadata field.
    • author (Optional): Metadata field.

    All optional fields provided in the JSON object will be used to populate the document's metadata in the vector database.

  4. Choose a LlamaIndex type

    main

    You can configure the indexing strategy via the LLAMA_INDEX_TYPE environment variable. By default, the system uses GPTVectorStoreIndex, which stores document chunks in memory and retrieves top-k nodes via embedding similarity.

    Different index types (such as tree, keyword table, or knowledge graph) are optimized for different data and query use-cases. Refer to the LlamaIndex documentation for the full list of accepted identifiers.

  5. Choose an authentication method

    main

    The plugin supports four authentication patterns. You must implement the chosen logic in server/main.py and update your /.well-known/ai-plugin.json manifest.

    1. No Authentication: No credentials required. Suitable for public data. Copy logic from /examples/authentication-methods/no-auth/main.py.
    2. HTTP Bearer (User Level): The default. Each user must provide a BEARER_TOKEN when adding the plugin. Provides better security for shared plugins. See /examples/authentication-methods/user-http/ai-plugin.json.
    3. HTTP Bearer (Service Level): A single token is used by ChatGPT to authorize requests on behalf of all users. More convenient but less secure as all users share the same token. See /examples/authentication-methods/service-http/ai-plugin.json.
    4. OAuth: The highest security level. Requires implementing an OAuth flow in server/main.py and providing parameters in the manifest. See /examples/authentication-methods/oauth/ai-plugin.json.
  6. Understand ChatGPT Retrieval Plugin limitations

    main

    Users should be aware of the following limitations when using the plugin:

    • Keyword Search: Standard embeddings might not capture exact keyword matches effectively. For better keyword performance, consider vector databases that support hybrid search (e.g., Elasticsearch, Pinecone, Weaviate, or Azure Cognitive Search).
    • Sensitive Data: The plugin does not automatically filter sensitive data; developers are responsible for ensuring data privacy compliance.
    • Scalability: Performance depends on the chosen vector database provider and dataset size.
    • Metadata Extraction Accuracy: The optional metadata extraction relies on LLMs and may not always be accurate.
    • PII Detection Reliability: The PII detection feature is not foolproof and should be used with caution.
  7. Customize PII detection and metadata extraction

    main

    The process_zip.py script uses specific service modules for PII screening and metadata extraction. You can modify these functions to suit your specific use case or security requirements:

    • PII Detection: Customize the logic in services/pii_detection.py (specifically the screen_text_for_pii function).
    • Metadata Extraction: Customize the logic in services/extract_metadata.py (specifically the extract_metadata_from_document function).
  8. Integrate the Retrieval Plugin with Function Calling

    main

    The plugin can be integrated into the Chat Completions API or the Assistants API using function calling. This allows the model to intelligently decide when to call query, fetch, or upsert based on the conversation context.

    • Chat Completions API: Define the plugin's endpoints as tools. Models like gpt-3.5-turbo-0125 and gpt-4-turbo-preview are trained to detect when a function should be called and respond with the appropriate JSON arguments.
    • Assistants API: Use the same function definitions as tools. This allows you to build custom AI assistants that leverage your specific vector database.
    • Parallel Function Calling: Both APIs support parallel function calling, meaning a model can perform multiple tasks (e.g., querying a document and then saving a new snippet via upsert) in a single message.
  9. Use Retrieval Plugin with Assistants API

    main

    The Retrieval Plugin can be used with the Assistants API via function calling in tool use.

    When to use this instead of native Assistants retrieval: While the Assistants API has native file retrieval, you should use the Retrieval Plugin with function calling if you require granular control over your retrieval system, such as:

    • Specifying embedding chunk lengths.
    • Choosing specific embedding models or sizes.
    • Customizing the retrieval logic/parameters.
  10. Quickstart: Install and run the ChatGPT Retrieval Plugin

    main

    Follow these steps to set up a local development environment for the plugin using Python 3.10 and Poetry:

    1. Install Python 3.10.
    2. Clone the repository:
      git clone https://github.com/openai/chatgpt-retrieval-plugin.git
      cd chatgpt-retrieval-plugin
    3. Set up the virtual environment:
      pip install poetry
      poetry env use python3.10
      poetry shell
      poetry install
    4. Configure Environment Variables: You must create a BEARER_TOKEN and set required variables for your datastore, OpenAI API, and embeddings (see Environment Variables Reference).
    5. Run the API:
      poetry run start
    6. Access Documentation: The FastAPI documentation will be available at http://0.0.0.0:8000/docs.
    git clone https://github.com/openai/chatgpt-retrieval-plugin.git
    cd chatgpt-retrieval-plugin
    pip install poetry
    poetry env use python3.10
    poetry shell
    poetry install
    # ... set env vars ...
    poetry run start
  11. Optimize dependencies for deployment

    main

    To reduce app size and improve performance before deploying, remove unused vector database provider packages from your pyproject.toml file. You can find specific guidance on which packages to remove for each provider in the removing-unused-dependencies.md guide.

    Note that you do not need to run poetry lock or poetry install manually after editing pyproject.toml; the provided Dockerfile uses a requirements.txt file generated via poetry export to install only the necessary dependencies.