Full Code of zilliztech/GPTCache for AI

main c59fb3a6152a cached
290 files
3.0 MB
813.5k tokens
1038 symbols
1 requests
Download .txt
Showing preview only (3,248K chars total). Download the full file or copy to clipboard to get everything.
Repository: zilliztech/GPTCache
Branch: main
Commit: c59fb3a6152a
Files: 290
Total size: 3.0 MB

Directory structure:
gitextract_233_9gzq/

├── .github/
│   ├── ISSUE_TEMPLATE/
│   │   ├── bug_report.yaml
│   │   ├── documentation_improvement.yaml
│   │   ├── enhancement.yaml
│   │   └── feature_request.yaml
│   ├── mergify.yml
│   └── workflows/
│       ├── Nightly_CI_main.yaml
│       ├── build_dev_python_package.yaml
│       ├── publish_dev_package.yaml
│       ├── publish_release_image.yaml
│       ├── publish_release_package.yaml
│       ├── pylint.yaml
│       └── unit_test_main.yaml
├── .gitignore
├── LICENSE
├── MANIFEST.in
├── Makefile
├── OWNERS
├── README.md
├── cache_config_template.yml
├── codecov.yml
├── docs/
│   ├── .readthedocs.yaml
│   ├── Makefile
│   ├── _exts/
│   │   ├── docgen2.py
│   │   └── index_con.py
│   ├── _templates/
│   │   ├── author.html
│   │   ├── copyright.html
│   │   ├── function.rst
│   │   └── index.rst
│   ├── bootcamp/
│   │   ├── langchain/
│   │   │   ├── baby_agi.ipynb
│   │   │   ├── index.rst
│   │   │   ├── qa_generation.ipynb
│   │   │   ├── question_answering.ipynb
│   │   │   └── sqlite.ipynb
│   │   ├── llama_index/
│   │   │   ├── index.rst
│   │   │   └── webpage_qa.ipynb
│   │   ├── openai/
│   │   │   ├── chat.ipynb
│   │   │   ├── image_generation.ipynb
│   │   │   ├── index.rst
│   │   │   ├── language_translate.ipynb
│   │   │   ├── speech_to_text.ipynb
│   │   │   ├── sql_translate.ipynb
│   │   │   └── tweet_classifier.ipynb
│   │   ├── replicate/
│   │   │   ├── index.rst
│   │   │   └── visual_question_answering.ipynb
│   │   ├── streamlit/
│   │   │   ├── gptcache-streamlit-audio/
│   │   │   │   ├── .streamlit/
│   │   │   │   │   └── config.toml
│   │   │   │   ├── README.md
│   │   │   │   ├── audio.py
│   │   │   │   └── requirements.txt
│   │   │   └── gptcache-streamlit-image/
│   │   │       ├── README.md
│   │   │       ├── imagen.py
│   │   │       └── requirements.txt
│   │   ├── temperature/
│   │   │   ├── chat.ipynb
│   │   │   ├── create_image.ipynb
│   │   │   └── index.rst
│   │   └── vertex/
│   │       ├── index.rst
│   │       └── vertexai_caching.ipynb
│   ├── conf.py
│   ├── configure_it.md
│   ├── contributing.md
│   ├── feature.md
│   ├── horizontal-scaling-usage.md
│   ├── index.rst
│   ├── make.bat
│   ├── references/
│   │   └── index.rst
│   ├── release_note.md
│   ├── requirements.txt
│   ├── toc.bak
│   └── usage.md
├── examples/
│   ├── README.md
│   ├── adapter/
│   │   ├── api.py
│   │   ├── langchain_llms.py
│   │   └── openai_chatgpt.py
│   ├── benchmark/
│   │   ├── benchmark_sqlite_faiss_onnx.py
│   │   └── mock_data.json
│   ├── context_process/
│   │   ├── selective_context.py
│   │   └── summarization_context.py
│   ├── data_manager/
│   │   ├── map_manager.py
│   │   ├── scalar_store.py
│   │   └── vector_store.py
│   ├── embedding/
│   │   ├── default.py
│   │   ├── onnx.py
│   │   ├── paddlenlp.py
│   │   └── random.py
│   ├── eviction/
│   │   └── distributed_eviction.py
│   ├── integrate/
│   │   ├── diffusers/
│   │   │   └── stable_diffusion.py
│   │   ├── dolly/
│   │   │   └── basic_usage.py
│   │   ├── langchain/
│   │   │   ├── langchain_llms_mock.py
│   │   │   ├── langchain_prompt_openai.py
│   │   │   ├── langchain_qa_chain.py
│   │   │   └── langchain_similaritycache_openai.py
│   │   ├── llama_cpp/
│   │   │   └── basic_usage.py
│   │   ├── openai/
│   │   │   ├── basic_usage.py
│   │   │   ├── create_image.py
│   │   │   ├── qa.py
│   │   │   ├── readme.py
│   │   │   └── summarize.py
│   │   ├── replicate/
│   │   │   └── vqa.py
│   │   └── stability/
│   │       └── text_to_image.py
│   ├── processor/
│   │   ├── llm_verifier_example.py
│   │   └── temperature_example.py
│   ├── session/
│   │   └── session.py
│   ├── similarity_evaluation/
│   │   ├── exact_match.py
│   │   ├── onnx.py
│   │   ├── search_distance.py
│   │   └── sequence_match.py
│   └── vqa_demo.py
├── gptcache/
│   ├── __init__.py
│   ├── adapter/
│   │   ├── __init__.py
│   │   ├── adapter.py
│   │   ├── api.py
│   │   ├── base.py
│   │   ├── diffusers.py
│   │   ├── dolly.py
│   │   ├── langchain_models.py
│   │   ├── llama_cpp.py
│   │   ├── minigpt4.py
│   │   ├── openai.py
│   │   ├── replicate.py
│   │   └── stability_sdk.py
│   ├── client.py
│   ├── config.py
│   ├── core.py
│   ├── embedding/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── cohere.py
│   │   ├── data2vec.py
│   │   ├── fasttext.py
│   │   ├── huggingface.py
│   │   ├── langchain.py
│   │   ├── onnx.py
│   │   ├── openai.py
│   │   ├── paddlenlp.py
│   │   ├── rwkv.py
│   │   ├── sbert.py
│   │   ├── string.py
│   │   ├── timm.py
│   │   ├── uform.py
│   │   └── vit.py
│   ├── manager/
│   │   ├── __init__.py
│   │   ├── data_manager.py
│   │   ├── eviction/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── distributed_cache.py
│   │   │   ├── manager.py
│   │   │   ├── memory_cache.py
│   │   │   └── redis_eviction.py
│   │   ├── eviction_manager.py
│   │   ├── factory.py
│   │   ├── object_data/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── local_storage.py
│   │   │   ├── manager.py
│   │   │   └── s3_storage.py
│   │   ├── scalar_data/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── dynamo_storage.py
│   │   │   ├── manager.py
│   │   │   ├── mongo.py
│   │   │   ├── redis_storage.py
│   │   │   └── sql_storage.py
│   │   └── vector_data/
│   │       ├── __init__.py
│   │       ├── base.py
│   │       ├── chroma.py
│   │       ├── docarray_index.py
│   │       ├── faiss.py
│   │       ├── hnswlib_store.py
│   │       ├── manager.py
│   │       ├── milvus.py
│   │       ├── pgvector.py
│   │       ├── qdrant.py
│   │       ├── redis_vectorstore.py
│   │       ├── usearch.py
│   │       └── weaviate.py
│   ├── processor/
│   │   ├── __init__.py
│   │   ├── check_hit.py
│   │   ├── context/
│   │   │   ├── __init__.py
│   │   │   ├── concat_context.py
│   │   │   ├── context.py
│   │   │   ├── selective_context.py
│   │   │   └── summarization_context.py
│   │   ├── post.py
│   │   └── pre.py
│   ├── report.py
│   ├── session.py
│   ├── similarity_evaluation/
│   │   ├── __init__.py
│   │   ├── cohere_rerank.py
│   │   ├── distance.py
│   │   ├── exact_match.py
│   │   ├── kreciprocal.py
│   │   ├── np.py
│   │   ├── onnx.py
│   │   ├── sbert_crossencoder.py
│   │   ├── sequence_match.py
│   │   ├── similarity_evaluation.py
│   │   └── time.py
│   └── utils/
│       ├── __init__.py
│       ├── cache_func.py
│       ├── dependency_control.py
│       ├── error.py
│       ├── lazy_import.py
│       ├── log.py
│       ├── response.py
│       ├── softmax.py
│       ├── time.py
│       └── token.py
├── gptcache_server/
│   ├── __init__.py
│   ├── dockerfiles/
│   │   └── Dockerfile
│   └── server.py
├── pylint.conf
├── requirements.txt
├── scripts/
│   ├── manage_conda_env.sh
│   └── remove_example_cache.sh
├── setup.py
└── tests/
    ├── integration_tests/
    │   ├── base/
    │   │   └── client_base.py
    │   ├── common/
    │   │   ├── common_func.py
    │   │   └── common_type.py
    │   ├── config/
    │   │   └── log_config.py
    │   ├── examples/
    │   │   ├── map/
    │   │   │   └── test_example_map.py
    │   │   ├── sqlite_faiss_mock/
    │   │   │   └── test_example_sqlite_faiss.py
    │   │   └── sqlite_faiss_onnx/
    │   │       └── test_example_sqlite_faiss_onnx.py
    │   ├── processor/
    │   │   └── pre/
    │   │       └── test_pre_without_prompt.py
    │   ├── test_redis_onnx.py
    │   ├── test_sqlite_faiss_onnx.py
    │   ├── test_sqlite_milvus_sbert.py
    │   └── utils/
    │       └── util_log.py
    ├── pytest.ini
    ├── requirements.txt
    └── unit_tests/
        ├── adapter/
        │   ├── test_adapter.py
        │   ├── test_api.py
        │   ├── test_diffusers.py
        │   ├── test_dolly.py
        │   ├── test_langchain_models.py
        │   ├── test_llama_cpp.py
        │   ├── test_openai.py
        │   ├── test_replicate.py
        │   └── test_stability.py
        ├── embedding/
        │   ├── test_cohere.py
        │   ├── test_data2vec.py
        │   ├── test_embedding_openai.py
        │   ├── test_embedding_string.py
        │   ├── test_fasttext.py
        │   ├── test_huggingface.py
        │   ├── test_langchain.py
        │   ├── test_onnx.py
        │   ├── test_paddlenlp.py
        │   ├── test_rwkv.py
        │   ├── test_sbert.py
        │   ├── test_timm.py
        │   ├── test_uform.py
        │   └── test_vit.py
        ├── eviction/
        │   ├── test_distributed_cache.py
        │   └── test_memory_cache.py
        ├── manager/
        │   ├── test_base.py
        │   ├── test_chromadb.py
        │   ├── test_dynamo_storage.py
        │   ├── test_eviction.py
        │   ├── test_factory.py
        │   ├── test_local_index.py
        │   ├── test_map.py
        │   ├── test_milvusdb.py
        │   ├── test_mongo.py
        │   ├── test_object_storage.py
        │   ├── test_pgvector.py
        │   ├── test_qdrant.py
        │   ├── test_redis.py
        │   ├── test_redis_cache_storage.py
        │   ├── test_sql_scalar.py
        │   ├── test_usearch.py
        │   └── test_weaviate.py
        ├── processor/
        │   ├── test_concat_context.py
        │   ├── test_context.py
        │   ├── test_post.py
        │   ├── test_pre.py
        │   ├── test_selective_context.py
        │   └── test_summarize_context.py
        ├── similarity_evaluation/
        │   ├── test_cohere_rerank.py
        │   ├── test_evaluation_kreciprocal.py
        │   ├── test_evaluation_onnx.py
        │   ├── test_evaluation_sbert.py
        │   ├── test_evaluation_sequence.py
        │   ├── test_evaluation_string.py
        │   ├── test_evalution_time.py
        │   ├── test_np.py
        │   └── test_simple.py
        ├── test_client.py
        ├── test_core.py
        ├── test_session.py
        └── utils/
            ├── test_error.py
            ├── test_log.py
            └── test_response.py

================================================
FILE CONTENTS
================================================

================================================
FILE: .github/ISSUE_TEMPLATE/bug_report.yaml
================================================
name: 🐞 Bug Report
description: Create a bug report to help us improve GPTCache
title: "[Bug]: "
body:
- type: markdown
  attributes:
    value: |
      Thanks for taking the time to fill out this bug report! Please fill the form in English!
- type: textarea
  attributes:
    label: Current Behavior
    description: A concise description of what you're experiencing.
    placeholder: |
      When I do <X>, <Y> happens and I see the error message attached below:
      ```...```
  validations:
    required: true
- type: textarea
  attributes:
    label: Expected Behavior
    description: A concise description of what you expected to happen.
    placeholder: When I do <X>, <Z> should happen instead.
  validations:
    required: false
- type: textarea
  attributes:
    label: Steps To Reproduce
    description: Steps to reproduce the behavior.
    placeholder: |
      1. In this environment...
      2. With this config...
      3. Run '...'
      4. See error...
    render: markdown
  validations:
    required: false
- type: textarea
  attributes:
    label: Environment
    description: |
      Enter the Environment Details:
    render: markdown
  validations:
    required: false
- type: textarea
  attributes:
    label: Anything else?
    description: |
      Links? References? Anything that will give us more context about the issue you are encountering!
  validations:
    required: false

================================================
FILE: .github/ISSUE_TEMPLATE/documentation_improvement.yaml
================================================
name: 📖 Documentation Improvement
description: Suggest improvements to our documentation
title: "[DOCS]: "
labels: [Documentation]
body:
- type: markdown
  attributes:
    value: |
      Thanks for taking the time to fill out this documentation improvement request!
- type: textarea
  attributes:
    label: Documentation Link
    description: Add a link to the page which needs improvement (if relevant)
  validations:
    required: false
- type: textarea
  attributes:
    label: Describe the problem
    description: Is the documentation missing? Or is it confusing? Why is it confusing?
  validations:
    required: false
- type: textarea
  attributes:
    label: Describe the improvement
    description: A clear and concise description of the improvement.
  validations:
    required: false
- type: textarea
  attributes:
    label: Anything else?
    description: |
      Links? References? Anything that will give us more context about the issue you are encountering!
  validations:
    required: false

================================================
FILE: .github/ISSUE_TEMPLATE/enhancement.yaml
================================================
name: Enhancement Request
description: As a developer, I want to make an enhancement for GPTCache
title: "[Enhancement]:"
labels: [kind/enhancement]
body:
- type: markdown
  attributes:
    value: |
      Thanks for taking the time to request/suggest an enhancement for GPTCache! Please fill the form in English!
- type: textarea
  attributes:
    label: What would you like to be added?
    description: A concise description of what you're expecting/suggesting.
    placeholder: |
      I would like to suggest/request a feature that's like...
  validations:
    required: false
- type: textarea
  attributes:
    label: Why is this needed?
    description: A concise description of the reason/motivation
  validations:
    required: false
- type: textarea
  attributes:
    label: Anything else?
    description: |
      Links? References? Anything that will give us more context about this!
  validations:
    required: false

================================================
FILE: .github/ISSUE_TEMPLATE/feature_request.yaml
================================================
name: 🚀 Feature Request
description: As a user, I want to request a feature for GPTCache
title: "[Feature]:"
labels: [kind/feature]
body:
- type: markdown
  attributes:
    value: |
      Thanks for taking the time to request a feature for GPTCache! Please fill the form in English!
- type: textarea
  attributes:
    label: Is your feature request related to a problem? Please describe.
    description: A concise description of the problem you are facing or the motivetion behind this feature request.
    placeholder: |
      I faced a problem due to which ...
  validations:
    required: false
- type: textarea
  attributes:
    label: Describe the solution you'd like.
    description: A concise description of the solution for the issue.
  validations:
    required: false
- type: textarea
  attributes:
    label: Describe an alternate solution.
    description: Is there any other approack to solve the problem?
  validations:
    required: false
- type: textarea
  attributes:
    label: Anything else? (Additional Context)
    description: |
      Links? References? Anything that will give us more context about this!
  validations:
    required: false

================================================
FILE: .github/mergify.yml
================================================
pull_request_rules:
  - name: Add needs-dco label when DCO check failed
    conditions:
      - or:
        - base=main
        - base=dev
      - -status-success=DCO
    actions:
      label:
        remove:
          - dco-passed
        add:
          - needs-dco

  - name: Add dco-passed label when DCO check passed
    conditions:
      - or:
        - base=main
        - base=dev
      - status-success=DCO
    actions:
      label:
        remove:
          - needs-dco
        add:
          - dco-passed

  # - name: Blocking PR if missing a related issue or PR doesn't have kind/improvement label
  #   conditions:
  #     - base=main
  #     - and:
  #       - -body~=\#[0-9]{1,6}(\s+|$)
  #       - -body~=https://github.com/zilliztech/GPTCache/issues/[0-9]{1,6}(\s+|$)
  #     - -label=kind/improvement
  #     - -title~=\[automated\]
  #   actions:
  #     label:
  #       add:
  #         - do-not-merge/missing-related-issue
  #     comment:
  #       message: |
  #         @{{author}} Please associate the related issue to the body of your Pull Request.  (eg. “issue: #<xyz>”)

  # - name: Dismiss block label if related issue be added into PR
  #   conditions:
  #     - base=main
  #     - or:
  #       - or:
  #         - body~=\#[0-9]{1,6}(\s+|$)
  #         - body~=https://github.com/milvus-io/knowhere/issues/[0-9]{1,6}(\s+|$)
  #       - label=kind/improvement
  #   actions:
  #     label:
  #       remove:
  #         - do-not-merge/missing-related-issue

  - name: Test passed for code changed
    conditions:
      - or:
        - base=main
        - base=dev
      - "status-success=pylint"
      - "status-success=build-pypi"
      - "status-success=unit-test-main"
      - "status-success=codecov/patch"
      - "status-success=codecov/project"
    actions:
      label:
        add:
          - ci-passed

  - name: Test passed for title skip ci
    conditions:
      - or:
        - base=main
        - base=dev
      - title~=\[skip ci\]
      - -files~=^(?=.*((\.(sh|py)|Makefile))).*$
    actions:
      label:
        add:
          - ci-passed

  - name: Test passed for markdown file
    conditions:
      - or:
        - base=main
        - base=dev
      - files=^.*\.md$
    actions:
      label:
        add:
          - ci-passed

  - name: Remove ci-passed label when ut failed for main
    conditions:
      - or:
        - base=main
        - base=dev
      - or:
        - "status-success!=pylint"
        - "status-success!=build-pypi"
        - "status-success!=unit-test-main"
      - files~=^(?=.*((\.(sh|py)|Makefile))).*$
    actions:
      label:
        remove:
          - ci-passed

================================================
FILE: .github/workflows/Nightly_CI_main.yaml
================================================
name: Nightly CI

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    #        ┌───────────── minute (0 - 59)
    #        │ ┌───────────── hour (0 - 23)
    #        │ │ ┌───────────── day of the month (1 - 31)
    #        │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
    #        │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
    #        │ │ │ │ │
    #        │ │ │ │ │
    #        │ │ │ │ │
    - cron: "0 22 * * *"
  pull_request:
    branches:
      - main
      - dev
    paths:
      - '.github/workflows/Nightly_CI_main.yaml'

jobs:
  nightly-CI-gpt-cache-cli-main:
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
    services:
      postgres:
        image: ankane/pgvector
        ports:
          - 5432:5432
        env:
          POSTGRES_PASSWORD: postgres
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - uses: actions/checkout@main

      - name: Set up Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: pip

      - name: Install dependency
        shell: bash
        run: |
          pip install -r requirements.txt

      - name: Build and install
        shell: bash
        run: |
          python setup.py install

      - name: Install test dependency
        shell: bash
        working-directory: tests
        run: |
          pip install -r requirements.txt

      - name: Download the `en_core_web_sm` model
        shell: bash
        working-directory: tests
        run: |
          python3 -m spacy download en_core_web_sm

      - name: Nightly CI Tests
        timeout-minutes: 30
        shell: bash
        working-directory: tests
        run: |
          export IS_CI=true
          export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
          python3 -m pytest ./ --tags L2


================================================
FILE: .github/workflows/build_dev_python_package.yaml
================================================
name: Build dev package
on:
  push:
    branches:
      - main
      - dev
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - '!**.md'
      - '.github/workflows/**'
  pull_request:
    branches:
      - main
      - dev
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - '!**.md'
      - '.github/workflows/**'
  workflow_dispatch:

jobs:
  build-pypi:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@master
    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
    # - name: Get history and tags for SCM versioning
    #   run: |
    #       git fetch --prune --unshallow
    #       git fetch --depth=1 origin +refs/tags/*:refs/tags/*
    - name: Install pypa/build
      run: >-
        python -m
        pip install
        build
        --user
    - name: Build a binary wheel and a source tarball
      run: >-
        python -m
        build
        --sdist
        --wheel
        --outdir dist/
        .
    - name: Archive production artifacts
      uses: actions/upload-artifact@v3
      with:
        name: dist
        path: |
          dist
          !dist/**/*.md


================================================
FILE: .github/workflows/publish_dev_package.yaml
================================================
name: Publish package to TestPyPI
# on:
#   push:
#     branches:
#       - 'main'
#     paths:
#       - 'gptcache/**'
#       - '!**.md'
#       - '.github/workflows/publish_dev_package.yaml'
#   pull_request:
#     branches:
#       - main
#     paths:
#       - '.github/workflows/publish_dev_package.yaml'
on:
  workflow_dispatch:

  schedule:
    # * is a special character in YAML so you have to quote this string
    #         ┌───────────── minute (0 - 59)
    #         │ ┌───────────── hour (0 - 23)
    #         │ │ ┌───────────── day of the month (1 - 31)
    #         │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
    #         │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
    #         │ │ │ │ │
    #         │ │ │ │ │
    #         │ │ │ │ │
    - cron:  '30 9 * * 1,4'


jobs:
  build-and-publish-dev:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@master
    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
    # - name: Get history and tags for SCM versioning
    #   run: |
    #       git fetch --prune --unshallow
    #       git fetch --depth=1 origin +refs/tags/*:refs/tags/*
    - name: Install pypi/build
      run: >-
        python -m
        pip install
        build
        --user
    - name: Build a binary wheel and a source tarball
      run: >-
        python -m
        build
        --sdist
        --wheel
        --outdir dist/
        .
    - name: Publish distribution 📦 to Test PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: ${{ secrets.PYPI_TEST_TOKEN }}
        repository_url: https://test.pypi.org/legacy/


================================================
FILE: .github/workflows/publish_release_image.yaml
================================================
name: Publish image to dockerhub
on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build-and-publish-image:
    runs-on: ubuntu-20.04
    steps:
    - name: Checkout 
      uses: actions/checkout@master

    - name: Get version
      id: get_version
      run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

    - name: Docker Build
      shell: bash
      run: |
        docker build "./gptcache_server/dockerfiles" -t "zilliz/gptcache:${{ steps.get_version.outputs.VERSION }}"

    - name: Docker login 
      if: success()
      uses: azure/docker-login@v1
      with:
        username: ${{ secrets.ZILLIZ_DOCKERHUB_USERNAME }} 
        password: ${{ secrets.ZILLIZ_DOCKERHUB_PASSWORD }}

    - name: Docker Push
      if: success()
      continue-on-error: false
      shell: bash
      run: |
        docker push zilliz/gptcache:${{ steps.get_version.outputs.VERSION }}
        docker tag zilliz/gptcache:${{ steps.get_version.outputs.VERSION }} zilliz/gptcache:latest
        docker push zilliz/gptcache:latest

================================================
FILE: .github/workflows/publish_release_package.yaml
================================================
name: Publish package to PyPI
# on:
#   push:
#     branches:
#       - 'main'
#     paths:
#       - 'gptcache/**'
#       - '!**.md'
#       - '.github/workflows/publish_dev_package.yaml'
#   pull_request:
#     branches:
#       - main
#     paths:
#       - '.github/workflows/publish_dev_package.yaml'
on:
  workflow_dispatch:

  release:
    types: [published]
      # tags:
      #   description: 'Test scenario tags'
      #   required: false
      #   type: boolean

jobs:
  build-and-publish-release:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@master
    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
    # - name: Get history and tags for SCM versioning
    #   run: |
    #       git fetch --prune --unshallow
    #       git fetch --depth=1 origin +refs/tags/*:refs/tags/*
    - name: Install pypi/build
      run: >-
        python -m
        pip install
        build
        --user
    - name: Build a binary wheel and a source tarball
      run: >-
        python -m
        build
        --sdist
        --wheel
        --outdir dist/
        .
    - name: Publish distribution 📦 to Test PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: ${{ secrets.PYPI_TOKEN }}

================================================
FILE: .github/workflows/pylint.yaml
================================================
name: Pylint
on:
  push:
    branches:
      - main
      - dev
      # file paths to consider in the event
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - 'docs/**'
      - '!**.md'
      - '.github/workflows/**'
  pull_request:
    branches:
      - main
      - dev
      # file paths to consider in the event
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - 'docs/**'
      - '!**.md'
      - '.github/workflows/**'
  workflow_dispatch:

jobs:
  pylint:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3.0.0
    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
    - name: Python pylint
      run: |
        pip install pylint==2.10.2
        make pylint_check
    - name: Make the readthedoc html
      shell: bash
      working-directory: docs
      run: |
        pip install -r requirements.txt
        make html


================================================
FILE: .github/workflows/unit_test_main.yaml
================================================
name: Unit test

on:
  push:
    branches:
      - main
      - dev
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - '!**.md'
      - '.github/workflows/**'
  pull_request:
    branches:
      - main
      - dev
    paths:
      - 'examples/**'
      - 'gptcache/**'
      - 'tests/**'
      - '!**.md'
      - '.github/workflows/**'
  workflow_dispatch:

jobs:
  unit-test-main:
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
    services:
      postgres:
        image: ankane/pgvector
        ports:
          - 5432:5432
        env:
          POSTGRES_PASSWORD: postgres
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      redis:
        image: redis/redis-stack-server
        ports:
          - 6379:6379
      dynamodb:
        image: amazon/dynamodb-local:2.0.0
        # rebinding to a different port since 8000 is a commonly used port
        ports:
          - 9999:8000
      mongo:
        image: mongo
        ports:
          - 27017:27017
    steps:
      - uses: actions/checkout@main

      - name: Set up Python 3.8
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: pip

      - name: Install dependency
        shell: bash
        run: |
          pip install -r requirements.txt

      - name: Build and install
        shell: bash
        timeout-minutes: 10
        run: |
          python setup.py install

      - name: Install test dependency
        shell: bash
        working-directory: tests
        run: |
          pip install -r requirements.txt

      - name: Download the `en_core_web_sm` model
        shell: bash
        working-directory: tests
        run: |
          python3 -m spacy download en_core_web_sm

      - name: Remove coverage xml
        run: |
          rm -rf ./coverage.xml

      - name: Normal Unit Tests
        timeout-minutes: 30
        shell: bash
        run: |
          export IS_CI=true
          export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
          python3 -m pytest -k "not embedding and not processor" --cov=gptcache --cov-report xml:coverage.xml --cov-append ./tests/

      - name: Processor Unit Tests
        timeout-minutes: 30
        shell: bash
        run: |
          export IS_CI=true
          export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
          python3 -m pytest --cov=gptcache --cov-append --cov-report xml:coverage.xml ./tests/unit_tests/processor/

      - name: Embedding Unit Tests
        timeout-minutes: 30
        shell: bash
        run: |
          export IS_CI=true
          export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
          python3 -m pytest --cov=gptcache --cov-report xml:coverage.xml --cov-append ./tests/unit_tests/embedding/

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3.1.0
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage.xml
          fail_ci_if_error: true


================================================
FILE: .gitignore
================================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
*.DS_Store
# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
*.db

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.idea
**/data_map**.txt
**/faiss**.index
**/sqlite**.db
**/gpt_cache**.db
**/example.py
**/example.db
**/.chroma
docs/references/*
!docs/references/index.rst

================================================
FILE: LICENSE
================================================
The MIT License (MIT)

Copyright (c) Zilliz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: MANIFEST.in
================================================
include requirements.txt


================================================
FILE: Makefile
================================================
install:
	@pip install -r requirements.txt
	@python setup.py install

pip_upgrade:
	@python -m pip install --upgrade pip

package:
	@python setup.py sdist bdist_wheel

upload:
	@python -m twine upload dist/*

upload_test:
	@python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

remove_example_cache:
	@bash ./scripts/remove_example_cache.sh

create_conda_env:
	@bash ./scripts/manage_conda_env.sh create

remove_conda_env:
	@bash ./scripts/manage_conda_env.sh remove

pylint_check:
	pylint --rcfile=pylint.conf --output-format=colorized gptcache

pytest:
	pytest tests/

================================================
FILE: OWNERS
================================================
filters:
  ".*":
    reviewers:
      - SimFG
      - xiaofan-luan
      - cxie
    approvers:
      - SimFG
      - xiaofan-luan
      - cxie


================================================
FILE: README.md
================================================
# GPTCache : A Library for Creating Semantic Cache for LLM Queries
Slash Your LLM API Costs by 10x 💰, Boost Speed by 100x ⚡ 

[![Release](https://img.shields.io/pypi/v/gptcache?label=Release&color&logo=Python)](https://pypi.org/project/gptcache/)
[![pip download](https://img.shields.io/pypi/dm/gptcache.svg?color=bright-green&logo=Pypi)](https://pypi.org/project/gptcache/)
[![Codecov](https://img.shields.io/codecov/c/github/zilliztech/GPTCache/dev?label=Codecov&logo=codecov&token=E30WxqBeJJ)](https://codecov.io/gh/zilliztech/GPTCache)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/license/mit/)
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/zilliz_universe.svg?style=social&label=Follow%20%40Zilliz)](https://twitter.com/zilliz_universe)
[![Discord](https://img.shields.io/discord/1092648432495251507?label=Discord&logo=discord)](https://discord.gg/Q8C6WEjSWV)

🎉 GPTCache has been fully integrated with 🦜️🔗[LangChain](https://github.com/hwchase17/langchain) ! Here are detailed [usage instructions](https://python.langchain.com/docs/modules/model_io/models/llms/integrations/llm_caching#gptcache).

🐳 [The GPTCache server docker image](https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md#Use-GPTCache-server) has been released, which means that **any language** will be able to use GPTCache!

📔 This project is undergoing swift development, and as such, the API may be subject to change at any time. For the most up-to-date information, please refer to the latest [documentation]( https://gptcache.readthedocs.io/en/latest/) and [release note](https://github.com/zilliztech/GPTCache/blob/main/docs/release_note.md).

**NOTE:** As the number of large models is growing explosively and their API shape is constantly evolving, we no longer add support for new API or models. We encourage the usage of using the get and set API in gptcache, here is the demo code: https://github.com/zilliztech/GPTCache/blob/main/examples/adapter/api.py

## Quick Install

`pip install gptcache`

## 🚀 What is GPTCache?

ChatGPT and various large language models (LLMs) boast incredible versatility, enabling the development of a wide range of applications. However, as your application grows in popularity and encounters higher traffic levels, the expenses related to LLM API calls can become substantial. Additionally, LLM services might exhibit slow response times, especially when dealing with a significant number of requests.

To tackle this challenge, we have created GPTCache, a project dedicated to building a semantic cache for storing LLM responses. 

## 😊 Quick Start

**Note**:

- You can quickly try GPTCache and put it into a production environment without heavy development. However, please note that the repository is still under heavy development.
- By default, only a limited number of libraries are installed to support the basic cache functionalities. When you need to use additional features, the related libraries will be **automatically installed**.
- Make sure that the Python version is **3.8.1 or higher**, check: `python --version`
- If you encounter issues installing a library due to a low pip version, run: `python -m pip install --upgrade pip`.

### dev install

```bash
# clone GPTCache repo
git clone -b dev https://github.com/zilliztech/GPTCache.git
cd GPTCache

# install the repo
pip install -r requirements.txt
python setup.py install
```

### example usage

These examples will help you understand how to use exact and similar matching with caching. You can also run the example on [Colab](https://colab.research.google.com/drive/1m1s-iTDfLDk-UwUAQ_L8j1C-gzkcr2Sk?usp=share_link). And more examples you can refer to the [Bootcamp](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/chat.html)

Before running the example, **make sure** the OPENAI_API_KEY environment variable is set by executing `echo $OPENAI_API_KEY`. 

If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems. 

> It is important to note that this method is only effective temporarily, so if you want a permanent effect, you'll need to modify the environment variable configuration file. For instance, on a Mac, you can modify the file located at `/etc/profile`.

<details>

<summary> Click to <strong>SHOW</strong> example code </summary>

#### OpenAI API original usage

```python
import os
import time

import openai


def response_text(openai_resp):
    return openai_resp['choices'][0]['message']['content']


question = 'what‘s chatgpt'

# OpenAI API original usage
openai.api_key = os.getenv("OPENAI_API_KEY")
start_time = time.time()
response = openai.ChatCompletion.create(
  model='gpt-3.5-turbo',
  messages=[
    {
        'role': 'user',
        'content': question
    }
  ],
)
print(f'Question: {question}')
print("Time consuming: {:.2f}s".format(time.time() - start_time))
print(f'Answer: {response_text(response)}\n')

```

#### OpenAI API + GPTCache, exact match cache

> If you ask ChatGPT the exact same two questions, the answer to the second question will be obtained from the cache without requesting ChatGPT again.

```python
import time


def response_text(openai_resp):
    return openai_resp['choices'][0]['message']['content']

print("Cache loading.....")

# To use GPTCache, that's all you need
# -------------------------------------------------
from gptcache import cache
from gptcache.adapter import openai

cache.init()
cache.set_openai_key()
# -------------------------------------------------

question = "what's github"
for _ in range(2):
    start_time = time.time()
    response = openai.ChatCompletion.create(
      model='gpt-3.5-turbo',
      messages=[
        {
            'role': 'user',
            'content': question
        }
      ],
    )
    print(f'Question: {question}')
    print("Time consuming: {:.2f}s".format(time.time() - start_time))
    print(f'Answer: {response_text(response)}\n')
```

#### OpenAI API + GPTCache, similar search cache

> After obtaining an answer from ChatGPT in response to several similar questions, the answers to subsequent questions can be retrieved from the cache without the need to request ChatGPT again.

```python
import time


def response_text(openai_resp):
    return openai_resp['choices'][0]['message']['content']

from gptcache import cache
from gptcache.adapter import openai
from gptcache.embedding import Onnx
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation

print("Cache loading.....")

onnx = Onnx()
data_manager = get_data_manager(CacheBase("sqlite"), VectorBase("faiss", dimension=onnx.dimension))
cache.init(
    embedding_func=onnx.to_embeddings,
    data_manager=data_manager,
    similarity_evaluation=SearchDistanceEvaluation(),
    )
cache.set_openai_key()

questions = [
    "what's github",
    "can you explain what GitHub is",
    "can you tell me more about GitHub",
    "what is the purpose of GitHub"
]

for question in questions:
    start_time = time.time()
    response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo',
        messages=[
            {
                'role': 'user',
                'content': question
            }
        ],
    )
    print(f'Question: {question}')
    print("Time consuming: {:.2f}s".format(time.time() - start_time))
    print(f'Answer: {response_text(response)}\n')
```

#### OpenAI API + GPTCache, use temperature

> You can always pass a parameter of temperature while requesting the API service or model.
> 
> The range of `temperature` is [0, 2], default value is 0.0.
> 
> A higher temperature means a higher possibility of skipping cache search and requesting large model directly.
> When temperature is 2, it will skip cache and send request to large model directly for sure. When temperature is 0, it will search cache before requesting large model service.
> 
> The default `post_process_messages_func` is `temperature_softmax`. In this case, refer to [API reference](https://gptcache.readthedocs.io/en/latest/references/processor.html#module-gptcache.processor.post) to learn about how `temperature` affects output.

```python
import time

from gptcache import cache, Config
from gptcache.manager import manager_factory
from gptcache.embedding import Onnx
from gptcache.processor.post import temperature_softmax
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation
from gptcache.adapter import openai

cache.set_openai_key()

onnx = Onnx()
data_manager = manager_factory("sqlite,faiss", vector_params={"dimension": onnx.dimension})

cache.init(
    embedding_func=onnx.to_embeddings,
    data_manager=data_manager,
    similarity_evaluation=SearchDistanceEvaluation(),
    post_process_messages_func=temperature_softmax
    )
# cache.config = Config(similarity_threshold=0.2)

question = "what's github"

for _ in range(3):
    start = time.time()
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature = 1.0,  # Change temperature here
        messages=[{
            "role": "user",
            "content": question
        }],
    )
    print("Time elapsed:", round(time.time() - start, 3))
    print("Answer:", response["choices"][0]["message"]["content"])
```

</details>

To use GPTCache exclusively, only the following lines of code are required, and there is no need to modify any existing code.

```python
from gptcache import cache
from gptcache.adapter import openai

cache.init()
cache.set_openai_key()
```

More Docs:

- [Usage, how to use GPTCache better](docs/usage.md)
- [Features, all features currently supported by the cache](docs/feature.md)
- [Examples, learn better custom caching](examples/README.md)
- [Distributed Caching and Horizontal Scaling ](docs/horizontal-scaling-usage.md)

## 🎓 Bootcamp

- GPTCache with **LangChain**
  - [QA Generation](https://gptcache.readthedocs.io/en/latest/bootcamp/langchain/qa_generation.html)
  - [Question Answering](https://gptcache.readthedocs.io/en/latest/bootcamp/langchain/question_answering.html)
  - [SQL Chain](https://gptcache.readthedocs.io/en/latest/bootcamp/langchain/sqlite.html)
  - [BabyAGI User Guide](https://gptcache.readthedocs.io/en/latest/bootcamp/langchain/baby_agi.html)
- GPTCache with **Llama_index**
  - [WebPage QA](https://gptcache.readthedocs.io/en/latest/bootcamp/llama_index/webpage_qa.html)
- GPTCache with **OpenAI**
  - [Chat completion](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/chat.html)
  - [Language Translation](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/language_translate.html)
  - [SQL Translate](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/sql_translate.html)
  - [Twitter Classifier](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/tweet_classifier.html)
  - [Multimodal: Image Generation](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/image_generation.html)
  - [Multimodal: Speech to Text](https://gptcache.readthedocs.io/en/latest/bootcamp/openai/speech_to_text.html)
- GPTCache with **Replicate**
  - [Visual Question Answering](https://gptcache.readthedocs.io/en/latest/bootcamp/replicate/visual_question_answering.html)
- GPTCache with **Temperature Param**
  - [OpenAI Chat](https://gptcache.readthedocs.io/en/latest/bootcamp/temperature/chat.html)
  - [OpenAI Image Creation](https://gptcache.readthedocs.io/en/latest/bootcamp/temperature/create_image.html)

## 😎 What can this help with?
GPTCache offers the following primary benefits:

- **Decreased expenses**: Most LLM services charge fees based on a combination of number of requests and [token count](https://openai.com/pricing). GPTCache effectively minimizes your expenses by caching query results, which in turn reduces the number of requests and tokens sent to the LLM service. As a result, you can enjoy a more cost-efficient experience when using the service.
- **Enhanced performance**: LLMs employ generative AI algorithms to generate responses in real-time, a process that can sometimes be time-consuming. However, when a similar query is cached, the response time significantly improves, as the result is fetched directly from the cache, eliminating the need to interact with the LLM service. In most situations, GPTCache can also provide superior query throughput compared to standard LLM services.
- **Adaptable development and testing environment**: As a developer working on LLM applications, you're aware that connecting to LLM APIs is generally necessary, and comprehensive testing of your application is crucial before moving it to a production environment. GPTCache provides an interface that mirrors LLM APIs and accommodates storage of both LLM-generated and mocked data. This feature enables you to effortlessly develop and test your application, eliminating the need to connect to the LLM service.
- **Improved scalability and availability**: LLM services frequently enforce [rate limits](https://platform.openai.com/docs/guides/rate-limits), which are constraints that APIs place on the number of times a user or client can access the server within a given timeframe. Hitting a rate limit means that additional requests will be blocked until a certain period has elapsed, leading to a service outage. With GPTCache, you can easily scale to accommodate an increasing volume of queries, ensuring consistent performance as your application's user base expands.

## 🤔 How does it work?

Online services often exhibit data locality, with users frequently accessing popular or trending content. Cache systems take advantage of this behavior by storing commonly accessed data, which in turn reduces data retrieval time, improves response times, and eases the burden on backend servers. Traditional cache systems typically utilize an exact match between a new query and a cached query to determine if the requested content is available in the cache before fetching the data.

However, using an exact match approach for LLM caches is less effective due to the complexity and variability of LLM queries, resulting in a low cache hit rate. To address this issue, GPTCache adopt alternative strategies like semantic caching. Semantic caching identifies and stores similar or related queries, thereby increasing cache hit probability and enhancing overall caching efficiency. 

GPTCache employs embedding algorithms to convert queries into embeddings and uses a vector store for similarity search on these embeddings. This process allows GPTCache to identify and retrieve similar or related queries from the cache storage, as illustrated in the [Modules section](https://github.com/zilliztech/GPTCache#-modules). 

Featuring a modular design, GPTCache makes it easy for users to customize their own semantic cache. The system offers various implementations for each module, and users can even develop their own implementations to suit their specific needs.

In a semantic cache, you may encounter false positives during cache hits and false negatives during cache misses. GPTCache offers three metrics to gauge its performance, which are helpful for developers to optimize their caching systems:

- **Hit Ratio**: This metric quantifies the cache's ability to fulfill content requests successfully, compared to the total number of requests it receives. A higher hit ratio indicates a more effective cache.
- **Latency**: This metric measures the time it takes for a query to be processed and the corresponding data to be retrieved from the cache. Lower latency signifies a more efficient and responsive caching system.
- **Recall**: This metric represents the proportion of queries served by the cache out of the total number of queries that should have been served by the cache. Higher recall percentages indicate that the cache is effectively serving the appropriate content.

A [sample benchmark](https://github.com/zilliztech/gpt-cache/blob/main/examples/benchmark/benchmark_sqlite_faiss_onnx.py) is included for users to start with assessing the performance of their semantic cache.

## 🤗 Modules

![GPTCache Struct](docs/GPTCacheStructure.png)

- **LLM Adapter**: 
The LLM Adapter is designed to integrate different LLM models by unifying their APIs and request protocols. GPTCache offers a standardized interface for this purpose, with current support for ChatGPT integration.
  - [x] Support OpenAI ChatGPT API.
  - [x] Support [langchain](https://github.com/hwchase17/langchain).
  - [x] Support [minigpt4](https://github.com/Vision-CAIR/MiniGPT-4.git).
  - [x] Support [Llamacpp](https://github.com/ggerganov/llama.cpp.git).
  - [x] Support [dolly](https://github.com/databrickslabs/dolly.git).
  - [ ] Support other LLMs, such as Hugging Face Hub, Bard, Anthropic.
- **Multimodal Adapter (experimental)**: 
The Multimodal Adapter is designed to integrate different large multimodal models by unifying their APIs and request protocols. GPTCache offers a standardized interface for this purpose, with current support for integrations of image generation, audio transcription.
  - [x] Support OpenAI Image Create API.
  - [x] Support OpenAI Audio Transcribe API.
  - [x] Support Replicate BLIP API.
  - [x] Support Stability Inference API.
  - [x] Support Hugging Face Stable Diffusion Pipeline (local inference).
  - [ ] Support other multimodal services or self-hosted large multimodal models.
- **Embedding Generator**: 
This module is created to extract embeddings from requests for similarity search. GPTCache offers a generic interface that supports multiple embedding APIs, and presents a range of solutions to choose from. 
  - [x] Disable embedding. This will turn GPTCache into a keyword-matching cache.
  - [x] Support OpenAI embedding API.
  - [x] Support [ONNX](https://onnx.ai/) with the GPTCache/paraphrase-albert-onnx model.
  - [x] Support [Hugging Face](https://huggingface.co/) embedding with transformers, ViTModel, Data2VecAudio.
  - [x] Support [Cohere](https://docs.cohere.ai/reference/embed) embedding API.
  - [x] Support [fastText](https://fasttext.cc) embedding.
  - [x] Support [SentenceTransformers](https://www.sbert.net) embedding.
  - [x] Support [Timm](https://timm.fast.ai/) models for image embedding.
  - [ ] Support other embedding APIs.
- **Cache Storage**:
**Cache Storage** is where the response from LLMs, such as ChatGPT, is stored. Cached responses are retrieved to assist in evaluating similarity and are returned to the requester if there is a good semantic match. At present, GPTCache supports SQLite and offers a universally accessible interface for extension of this module.
  - [x] Support [SQLite](https://sqlite.org/docs.html).
  - [x] Support [DuckDB](https://duckdb.org/).
  - [x] Support [PostgreSQL](https://www.postgresql.org/).
  - [x] Support [MySQL](https://www.mysql.com/).
  - [x] Support [MariaDB](https://mariadb.org/).
  - [x] Support [SQL Server](https://www.microsoft.com/en-us/sql-server/).
  - [x] Support [Oracle](https://www.oracle.com/).
  - [x] Support [DynamoDB](https://aws.amazon.com/dynamodb/).
  - [ ] Support [MongoDB](https://www.mongodb.com/).
  - [ ] Support [Redis](https://redis.io/).
  - [ ] Support [Minio](https://min.io/).
  - [ ] Support [HBase](https://hbase.apache.org/).
  - [ ] Support [ElasticSearch](https://www.elastic.co/).
  - [ ] Support other storages.
- **Vector Store**:
The **Vector Store** module helps find the K most similar requests from the input request's extracted embedding. The results can help assess similarity. GPTCache provides a user-friendly interface that supports various vector stores, including Milvus, Zilliz Cloud, and FAISS. More options will be available in the future.
  - [x] Support [Milvus](https://milvus.io/), an open-source vector database for production-ready AI/LLM applications. 
  - [x] Support [Zilliz Cloud](https://cloud.zilliz.com/), a fully-managed cloud vector database based on Milvus.
  - [x] Support [Milvus Lite](https://github.com/milvus-io/milvus-lite), a lightweight version of Milvus that can be embedded into your Python application.
  - [x] Support [FAISS](https://faiss.ai/), a library for efficient similarity search and clustering of dense vectors.
  - [x] Support [Hnswlib](https://github.com/nmslib/hnswlib), header-only C++/python library for fast approximate nearest neighbors.
  - [x] Support [PGVector](https://github.com/pgvector/pgvector), open-source vector similarity search for Postgres.
  - [x] Support [Chroma](https://github.com/chroma-core/chroma), the AI-native open-source embedding database.
  - [x] Support [DocArray](https://github.com/docarray/docarray), DocArray is a library for representing, sending and storing multi-modal data, perfect for Machine Learning applications.
  - [x] Support qdrant
  - [x] Support weaviate
  - [ ] Support other vector databases.
- **Cache Manager**:
The **Cache Manager** is responsible for controlling the operation of both the **Cache Storage** and **Vector Store**.
  - **Eviction Policy**:
  Cache eviction can be managed in memory using python's `cachetools` or in a distributed fashion using Redis as a key-value store. 
  - **In-Memory Caching**
  
  Currently, GPTCache makes decisions about evictions based solely on the number of lines. This approach can result in inaccurate resource evaluation and may cause out-of-memory (OOM) errors. We are actively investigating and developing a more sophisticated strategy.
    - [x] Support LRU eviction policy.
    - [x] Support FIFO eviction policy.
    - [x] Support LFU eviction policy.
    - [x] Support RR eviction policy.
    - [ ] Support more complicated eviction policies.
  - **Distributed Caching**
  
  If you were to scale your GPTCache deployment horizontally using in-memory caching, it won't be possible. Since the cached information would be limited to the single pod.
  
  With Distributed Caching, cache information consistent across all replicas we can use Distributed Cache stores like Redis. 
    - [x] Support Redis distributed cache
    - [x] Support memcached distributed cache
  
- **Similarity Evaluator**: 
This module collects data from both the **Cache Storage** and **Vector Store**, and uses various strategies to determine the similarity between the input request and the requests from the **Vector Store**. Based on this similarity, it determines whether a request matches the cache. GPTCache provides a standardized interface for integrating various strategies, along with a collection of implementations to use. The following similarity definitions are currently supported or will be supported in the future:
  - [x] The distance we obtain from the **Vector Store**.
  - [x] A model-based similarity determined using the GPTCache/albert-duplicate-onnx model from [ONNX](https://onnx.ai/).
  - [x] Exact matches between the input request and the requests obtained from the **Vector Store**.
  - [x] Distance represented by applying linalg.norm from numpy to the embeddings.
  - [ ] BM25 and other similarity measurements.
  - [ ] Support other model serving framework such as PyTorch.
 
  
  **Note**:Not all combinations of different modules may be compatible with each other. For instance, if we disable the **Embedding Extractor**, the **Vector Store** may not function as intended. We are currently working on implementing a combination sanity check for **GPTCache**.

## 😇 Roadmap
Coming soon! [Stay tuned!](https://twitter.com/zilliz_universe)

## 😍 Contributing
We are extremely open to contributions, be it through new features, enhanced infrastructure, or improved documentation.

For comprehensive instructions on how to contribute, please refer to our [contribution guide](docs/contributing.md).


================================================
FILE: cache_config_template.yml
================================================
# For `model_src`, `evaluation`, `post_function`, `pre_function`,
# `storage_config` options, Check README for more.

embedding:
    onnx
embedding_config:
    # Set model kws here including `model`, `api_key` if needed
storage_config:
    data_dir:
        gptcache_data
    manager:
        sqlite,faiss
    vector_params:
        # Set vector storage related params here
evaluation: 
    distance
evaluation_config:
    # Set evaluation metric kws here
pre_function:
    get_prompt
post_function:
    first
config:
    similarity_threshold: 0.8
    # Set other config here


================================================
FILE: codecov.yml
================================================
# Configuration File for CodeCov
codecov:
  require_ci_to_pass: no
  notify:
    require_ci_to_pass: no
    wait_for_ci: false

coverage:
  precision: 2
  round: down
  range: "70...100"

  status:
    project:
      default:
        target: 90%
        threshold: 0% #Allow the coverage to drop by threshold%, and posting a success status.
    patch:
      default:
        target: 90%   #target of patch diff
        threshold: 0%
        if_ci_failed: error #success, failure, error, ignore

comment:
  layout: "reach, diff, flags, files"
  behavior: default
  require_changes: false
  branches: # branch names that can post comment
    - main
    - dev

ignore:
  - "LICENSES"
  - ".git"
  - "*.yml"
  - "*.md"
  - "**/minigpt4.py"


================================================
FILE: docs/.readthedocs.yaml
================================================
version: 2

python:
   version: 3.8
   system_packages: true

================================================
FILE: docs/Makefile
================================================
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
SPHINXAUTOBUILD   ?= sphinx-autobuild
SOURCEDIR     = .
BUILDDIR      = _build

# Put it first so that "make" without argument is like "make help".
help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)


================================================
FILE: docs/_exts/docgen2.py
================================================
import os
import sys

from jinja2 import Environment, FileSystemLoader, select_autoescape

_default_skip_file_list = ["__init__.py"]
_default_skip_dir_list = ["__pycache__"]
_conflict_name_dict = {
            "manager": ["eviction", "object_data", "scalar_data", "vector_data"]
        }


class DocGen:
    def __init__(
        self,
        lib_name="gptcache",
        source_dir="../gptcache",
        output_dir="references",
        skip_list=[],
    ):
        self.lib_name = lib_name
        self.output_dir = os.path.abspath(output_dir)
        self.source_dir = os.path.abspath(source_dir)
        self.skip_list = skip_list

    @staticmethod
    def title_bar(input_str):
        return "=" * len(input_str)

    @staticmethod
    def section_bar(input_str):
        return "-" * len(input_str)

    @staticmethod
    def get_filename(input_str):
        if input_str == "gptcache":
            return input_str
        suffix = os.path.splitext(input_str)[1][1:]
        for conflict_dir, conflict_names in _conflict_name_dict.items():
            for conflict_name in conflict_names:
                if f"{conflict_dir}.{conflict_name}" in input_str:
                    return f"{conflict_name}.{suffix}"

        return suffix

    @staticmethod
    def cap(input_str):
        input_str = DocGen.get_filename(input_str)
        if input_str == "gptcache":
            return "GPTCache"
        return str.join(" ", [i.capitalize() for i in input_str.split("_")])

    def model_name(self, input_str: str):
        return self.lib_name + input_str[len(self.source_dir) :].replace("/", ".")

    def get_module_and_libs(self, module_dir, is_root):
        module = self.model_name(module_dir)
        libs = []
        for file in os.listdir(module_dir):
            if (
                os.path.isfile(os.path.join(module_dir, file))
                and file not in _default_skip_file_list
            ):
                libs.append(module + "." + os.path.splitext(file)[0])
            if not is_root:
                if (
                    os.path.isdir(os.path.join(module_dir, file))
                    and file not in _default_skip_dir_list
                ):
                    _, child_libs = self.get_module_and_libs(
                        os.path.join(module_dir, file), False
                    )
                    libs.extend(child_libs)
        if len(libs) > 0:
            sorted(libs)
            return module, libs
        return "", []

    def generate(self):
        # Set the output directory
        env = Environment(
            loader=FileSystemLoader(os.path.join(self.output_dir, "../_templates")),
            autoescape=select_autoescape(),
        )

        # Add custom filters
        env.filters["title_bar"] = DocGen.title_bar
        env.filters["section_bar"] = DocGen.section_bar
        env.filters["cap"] = DocGen.cap

        # Add the target path to the system path
        sys.path.insert(0, os.path.abspath(".."))

        # Load the modules
        modules = []
        libs = []

        a, b = self.get_module_and_libs(self.source_dir, True)
        if a:
            modules.append(a)
            libs.append(b)

        for file in os.listdir(self.source_dir):
            tmp_dir = os.path.join(self.source_dir, file)
            if os.path.isdir(tmp_dir) and file not in _default_skip_dir_list:
                a, b = self.get_module_and_libs(tmp_dir, False)
                if a:
                    modules.append(a)
                    libs.append(b)

        # Render the index templates and write rendered output to files
        index_temp = env.get_template("index.rst")

        with open(os.path.join(self.output_dir, "index.rst"), "w") as f:
            t = index_temp.render(
                {"modules": [DocGen.get_filename(module) for module in modules]}
            )
            f.write(t)

        # Render the function templates and write rendered output to files
        func_temp = env.get_template("function.rst")

        for index, module in enumerate(modules):
            with open(
                os.path.join(self.output_dir, f"{DocGen.get_filename(module)}.rst"), "w"
            ) as f:
                t = func_temp.render(
                    {
                        "module_name": module,
                        "funcs": [
                            (DocGen.get_filename(lib), lib) for lib in libs[index]
                        ],
                    }
                )
                f.write(t)


# if __name__ == "__main__":
#     gen = DocGen(source_dir="/Users/derek/fubang/gptcache/gptcache", output_dir="/Users/derek/fubang/gptcache/docs/references")
#     gen.generate()


================================================
FILE: docs/_exts/index_con.py
================================================
from m2r2 import convert
import os

class IndexCon:
    
  def __init__(self, source, output):
    self.source = source
    self.output = output
    self.preprocess()

  def preprocess(self):
    with open(self.source, 'r') as f:
      
      # remove the CI link from the file
      lines = f.readlines()
      lines = [line for line in lines if '[CI]' not in line]

      # change local links to the ones related to the _build/html directory and extension to .html
      lines = [line.replace('](docs/', '](') for line in lines] 
      lines = [line.replace('.md)', '.html)') for line in lines]

      # get the raw text within the <details> tag
      start_details_tag = [line for line in lines if '<details>' in line]
      summary_tag = [line for line in lines if '<summary>' in line]
      end_details_tag = [line for line in lines if '</details>' in line]
      start_details = lines.index(start_details_tag[0])
      summary_line = lines.index(summary_tag[0])
      end_details = lines.index(end_details_tag[0])

      before = convert(''.join(lines[:start_details-1]))
      end = convert(''.join(lines[end_details+1:]))

      collapse_rst = lines[summary_line+1:end_details]
      collapse_rst = [ "**" + x.split("# ")[1][:-1] + "**\n" if '# ' in x else x for x in collapse_rst]

      # print(collapse_rst)

      collapse_rst = convert(''.join(collapse_rst))
      collapse_rst = collapse_rst.split("\n")
      collapse_rst = [ '    ' + x for x in collapse_rst]

      collapse_rst = [f'\n.. collapse:: Click to SHOW examples\n'] + collapse_rst
      os.remove(self.output)

      with open(self.output, 'a') as f:
        f.write(before)
        f.write('\n'.join(collapse_rst))
        f.write(end)
        f.write('\n\n')
        
        with open('toc.bak', 'r') as t:
          f.write(t.read())

if __name__ == '__main__':
  index = IndexCon('../../README.md')



================================================
FILE: docs/_templates/author.html
================================================
By <a href="https://zilliz.com/cloud">Zilliz Inc.</a>

================================================
FILE: docs/_templates/copyright.html
================================================
<p>© Copyright 2023, <a href="https://zilliz.com/cloud">Zilliz Inc.</a></p>

================================================
FILE: docs/_templates/function.rst
================================================
{{ module_name | cap }}
{{ module_name | title_bar }}

.. contents:: Index

{% for func in funcs  -%}
{{func[0]}}
{{ func[0] | section_bar }}
.. automodule:: {{func[1]}}
   :members:
   :undoc-members:
   :show-inheritance:

{% endfor %}

================================================
FILE: docs/_templates/index.rst
================================================
🥸 API References
=================

.. toctree::
   :maxdepth: 1
   :caption: Contents:

{% for module in modules %}
   {{ module }}
{%- endfor -%}

================================================
FILE: docs/bootcamp/langchain/baby_agi.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "517a9fd4",
   "metadata": {},
   "source": [
    "# BabyAGI User Guide\n",
    "\n",
    "This notebook demonstrates how to implement [BabyAGI](https://github.com/yoheinakajima/babyagi/tree/main) by [Yohei Nakajima](https://twitter.com/yoheinakajima). BabyAGI is an AI agent that can generate and pretend to execute tasks based on a given objective, and you can find the origin notebook in [LangChain example](https://github.com/hwchase17/langchain/blob/master/docs/use_cases/autonomous_agents/baby_agi.ipynb).\n",
    "\n",
    "This guide will help you understand the components to create your own recursive agents, And it will also show how to use GPTCache to cache the response. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1WTvWIujioZtpwwVz7GGDzYhooul-rBeG?usp=sharing).\n",
    "\n",
    "Although BabyAGI uses specific vectorstores/model providers (Pinecone, OpenAI), one of the benefits of implementing it with LangChain is that you can easily swap those out for different options. In this implementation we use a Milvus vector datavase (because it runs locally and is free).\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47ef6fe7",
   "metadata": {},
   "source": [
    "## Go into GPTCache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache.There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the DataBse cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems. And there is `get_content_func` for the cache settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cbf1dbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the content(only question) form the prompt to cache\n",
    "def get_content_func(data, **_):\n",
    "    return data.get(\"prompt\").split(\"Question\")[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a8cb06",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "daebf19a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# cache.init(pre_embedding_func=get_content_func)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1f328a8",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors, it will use the `get_content_func` method\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to audio feature vector, response text in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23197b36",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "\n",
    "onnx = Onnx()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='127.0.0.1', port='19530', dimension=onnx.dimension)\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "cache.init(\n",
    "    pre_embedding_func=get_content_func,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2092f7e6",
   "metadata": {},
   "source": [
    "After initializing the cache, you can use the LangChain LLMs with `gptcache.adapter.langchain_models`. At this point **gptcache** will cache the answer, the only difference from the original example is to change `llm = OpenAI(temperature=0)` to `llm = LangChainLLMs(llm=OpenAI(temperature=0))`, which will be commented in the code block. And you can also set the `session` to set the session settings with `llm = LangChainLLMs(llm=OpenAI(temperature=0), session=session)`, more details refer to [session example](https://github.com/zilliztech/GPTCache/tree/main/examples#how-to-run-with-session).\n",
    "\n",
    "Then you will find that it will be more fast when search the similar content, let's play with it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "556af556",
   "metadata": {},
   "source": [
    "## Install and Import Required Modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8a354b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from collections import deque\n",
    "from typing import Dict, List, Optional, Any\n",
    "\n",
    "from langchain import LLMChain, OpenAI, PromptTemplate\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.llms import BaseLLM\n",
    "from langchain.vectorstores.base import VectorStore\n",
    "from pydantic import BaseModel, Field\n",
    "from langchain.chains.base import Chain\n",
    "from langchain.experimental import BabyAGI\n",
    "\n",
    "from gptcache.adapter.langchain_models import LangChainLLMs\n",
    "from gptcache.session import Session\n",
    "session = Session(name=\"baby_agi\") # set session for LangChainLLMs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09f70772",
   "metadata": {},
   "source": [
    "## Connect to the Vector Store\n",
    "\n",
    "Depending on what vectorstore you use, this step may look different."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "794045d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from langchain.vectorstores import FAISS\n",
    "from langchain.vectorstores import Milvus\n",
    "from langchain.docstore import InMemoryDocstore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6e0305eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define your embedding model\n",
    "embeddings_model = OpenAIEmbeddings()\n",
    "embedding_size = 1536\n",
    "vectorstore = Milvus(embeddings_model,\n",
    "                     collection_name=\"baby_agi\",\n",
    "                     connection_args={\"host\": \"127.0.0.1\", \"port\": \"19530\"},\n",
    "                    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05ba762e",
   "metadata": {},
   "source": [
    "### Run the BabyAGI\n",
    "\n",
    "Now it's time to create the BabyAGI controller and watch it try to accomplish your objective."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3d220b69",
   "metadata": {},
   "outputs": [],
   "source": [
    "OBJECTIVE = \"Write a weather report for SF today\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8a8e5543",
   "metadata": {},
   "outputs": [],
   "source": [
    "# llm = OpenAI(temperature=0)\n",
    "llm = LangChainLLMs(llm=OpenAI(temperature=0), session=session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3d69899b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Logging of LLMChains\n",
    "verbose = False\n",
    "# If None, will keep on going forever\n",
    "max_iterations: Optional[int] = 3\n",
    "baby_agi = BabyAGI.from_llm(\n",
    "    llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f7957b51",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[95m\u001b[1m\n",
      "*****TASK LIST*****\n",
      "\u001b[0m\u001b[0m\n",
      "1: Make a todo list\n",
      "\u001b[92m\u001b[1m\n",
      "*****NEXT TASK*****\n",
      "\u001b[0m\u001b[0m\n",
      "1: Make a todo list\n",
      "\u001b[93m\u001b[1m\n",
      "*****TASK RESULT*****\n",
      "\u001b[0m\u001b[0m\n",
      "\n",
      "\n",
      "1. Check the weather forecast for San Francisco today\n",
      "2. Make note of the temperature, humidity, wind speed, and other relevant weather conditions\n",
      "3. Write a weather report summarizing the forecast\n",
      "4. Check for any weather alerts or warnings\n",
      "5. Share the report with the relevant stakeholders\n",
      "\u001b[95m\u001b[1m\n",
      "*****TASK LIST*****\n",
      "\u001b[0m\u001b[0m\n",
      "2: Check the current temperature in San Francisco\n",
      "3: Check the current humidity in San Francisco\n",
      "4: Check the current wind speed in San Francisco\n",
      "5: Check for any weather alerts or warnings in San Francisco\n",
      "6: Check the forecast for the next 24 hours in San Francisco\n",
      "7: Check the forecast for the next 48 hours in San Francisco\n",
      "8: Check the forecast for the next 72 hours in San Francisco\n",
      "9: Check the forecast for the next week in San Francisco\n",
      "10: Check the forecast for the next month in San Francisco\n",
      "11: Check the forecast for the next 3 months in San Francisco\n",
      "1: Write a weather report for SF today\n",
      "\u001b[92m\u001b[1m\n",
      "*****NEXT TASK*****\n",
      "\u001b[0m\u001b[0m\n",
      "2: Check the current temperature in San Francisco\n",
      "\u001b[93m\u001b[1m\n",
      "*****TASK RESULT*****\n",
      "\u001b[0m\u001b[0m\n",
      "\n",
      "\n",
      "I will check the current temperature in San Francisco. I will use an online weather service to get the most up-to-date information.\n",
      "\u001b[95m\u001b[1m\n",
      "*****TASK LIST*****\n",
      "\u001b[0m\u001b[0m\n",
      "3: Check the current UV index in San Francisco.\n",
      "4: Check the current air quality in San Francisco.\n",
      "5: Check the current precipitation levels in San Francisco.\n",
      "6: Check the current cloud cover in San Francisco.\n",
      "7: Check the current barometric pressure in San Francisco.\n",
      "8: Check the current dew point in San Francisco.\n",
      "9: Check the current wind direction in San Francisco.\n",
      "10: Check the current humidity levels in San Francisco.\n",
      "1: Check the current temperature in San Francisco to the average temperature for this time of year.\n",
      "2: Check the current visibility in San Francisco.\n",
      "11: Write a weather report for SF today.\n",
      "\u001b[92m\u001b[1m\n",
      "*****NEXT TASK*****\n",
      "\u001b[0m\u001b[0m\n",
      "3: Check the current UV index in San Francisco.\n",
      "\u001b[93m\u001b[1m\n",
      "*****TASK RESULT*****\n",
      "\u001b[0m\u001b[0m\n",
      "\n",
      "\n",
      "I have checked the current UV index in San Francisco and it is currently at a moderate level of 5. This means that it is safe to be outside for short periods of time without sunscreen, but it is still recommended to wear sunscreen and protective clothing when outside for extended periods of time.\n",
      "\u001b[91m\u001b[1m\n",
      "*****TASK ENDING*****\n",
      "\u001b[0m\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'objective': 'Write a weather report for SF today'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baby_agi({\"objective\": OBJECTIVE})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "898a210b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/langchain/index.rst
================================================
LangChain
=================

.. toctree::
   :maxdepth: 1
   :caption: Contents:


   qa_generation
   question_answering
   sqlite
   baby_agi

================================================
FILE: docs/bootcamp/langchain/qa_generation.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ee2a3a21",
   "metadata": {},
   "source": [
    "# QA Generation\n",
    "This notebook shows how to use the `QAGenerationChain` to come up with question-answer pairs over a specific document.\n",
    "This is important because often times you may not have data to evaluate your question-answer system over, so this is a cheap and lightweight way to generate it!\n",
    "\n",
    "\n",
    "And You can find the origin notebook in [LangChain example](https://github.com/hwchase17/langchain/blob/master/docs/use_cases/evaluation/qa_generation.ipynb), and this example will show you how to set the LLM with GPTCache so that you can cache the data with LLM. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1avQR-ANehLmyfDcKR_2A_IycxwCpTa-k?usp=sharing)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb3f83a2",
   "metadata": {},
   "source": [
    "## Go into GPTCache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache.There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the DataBse cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems. And there is `get_msg_func` for the cache settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "29ea2668",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the content(only question) form the prompt to cache\n",
    "def get_msg_func(data, **_):\n",
    "    return data.get(\"messages\")[-1].content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae0efe00",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7892a80f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# cache.init(pre_embedding_func=get_msg_func)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc2c7a7",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors, it will use the `get_msg_func` method\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to audio feature vector, response text in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c00c0422",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "\n",
    "onnx = Onnx()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='127.0.0.1', port='19530', dimension=onnx.dimension)\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "cache.init(\n",
    "    pre_embedding_func=get_msg_func,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "083c33dd",
   "metadata": {},
   "source": [
    "After initializing the cache, you can use the LangChain Chat Models with `gptcache.adapter.langchain_models`. At this point **gptcache** will cache the answer, the only difference from the original example is to change `chat=ChatOpenAI(temperature=0)` to `chat = LangChainChat(chat=ChatOpenAI(temperature=0))`, which will be commented in the code block.\n",
    "\n",
    "Then you will find that it will be more fast when search the similar content, let's play with it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a0b2c33",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "33d3f0b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter, TextSplitter\n",
    "\n",
    "text_splitter =  RecursiveCharacterTextSplitter(chunk_overlap=500, chunk_size=2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2029a29c",
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = TextLoader(\"./state_of_the_union.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "87edb84c",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc = loader.load()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "04125b6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.chains import QAGenerationChain\n",
    "from gptcache.adapter.langchain_models import LangChainChat\n",
    "\n",
    "# chat = ChatOpenAI(temperature=0) # using the following code to cache with gptcache\n",
    "chat = LangChainChat(chat=ChatOpenAI(temperature=0))\n",
    "\n",
    "chain = QAGenerationChain.from_llm(chat, text_splitter=text_splitter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4f1593e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = chain.run(doc.page_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ee831f92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'question': 'What did President Zelenskyy say in his speech to the European Parliament?',\n",
       " 'answer': \"President Zelenskyy said in his speech to the European Parliament that 'Light will win over darkness.'\"}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7028754e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/langchain/question_answering.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "05859721",
   "metadata": {},
   "source": [
    "# Question Answering\n",
    "\n",
    "This notebook walks through how to use LangChain for question answering over a list of documents. It covers four different types of chains: `stuff`, `map_reduce`, `refine`, `map_rerank`. And You can find the origin notebook in [LangChain example](https://github.com/hwchase17/langchain/blob/master/docs/modules/chains/index_examples/question_answering.ipynb), and this example will show you how to set the LLM with GPTCache so that you can cache the data with LLM. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1C3VBKn3L38opEyaOH-SlGQrANzHkR_5f?usp=sharing)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea19ae7e",
   "metadata": {},
   "source": [
    "## Go into GPTCache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache.There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the DataBse cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems. And there is `get_content_func` for the cache settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04759af1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the content(only question) form the prompt to cache\n",
    "def get_content_func(data, **_):\n",
    "    return data.get(\"prompt\").split(\"Question\")[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0740dd59",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3f19b149",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# cache.init(pre_embedding_func=get_content_func)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a06c2c25",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors, it will use the `get_file_name` method\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to audio feature vector, response text in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "169b0d11",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "\n",
    "onnx = Onnx()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='127.0.0.1', port='19530', dimension=onnx.dimension, collection_name='chatbot')\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "cache.init(\n",
    "    pre_embedding_func=get_content_func,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44538b8b",
   "metadata": {},
   "source": [
    "After initializing the cache, you can use the LangChain LLMs with `gptcache.adapter.langchain_models`. At this point **gptcache** will cache the answer, the only difference from the original example is to change `llm = OpenAI(temperature=0)` to `llm = LangChainLLMs(llm=OpenAI(temperature=0))`, which will be commented in the code block.\n",
    "\n",
    "Then you will find that it will be more fast when search the similar content, let's play with it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "726f4996",
   "metadata": {},
   "source": [
    "## Prepare Data\n",
    "First we [prepare the data](https://raw.githubusercontent.com/hwchase17/langchain/master/docs/extras/modules/state_of_the_union.txt). For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents). You can learn more detail about Milvus in Langchain refer to [it](https://python.langchain.com/en/latest/modules/indexes/vectorstores/examples/milvus.html?highlight=milvus)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "17fcbc0f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import Milvus\n",
    "from langchain.document_loaders import TextLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef9305cc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "loader = TextLoader('./state_of_the_union.txt')\n",
    "documents = loader.load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "docs = text_splitter.split_documents(documents)\n",
    "\n",
    "embeddings = OpenAIEmbeddings()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "99ebc875",
   "metadata": {},
   "outputs": [],
   "source": [
    "vector_db = Milvus.from_documents(\n",
    "    docs,\n",
    "    embeddings,\n",
    "    connection_args={\"host\": \"127.0.0.1\", \"port\": \"19530\"},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d1eaf6e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "query = \"What did the president say about Justice Breyer\"\n",
    "docs = vector_db.similarity_search(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a16e3453",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.chains.question_answering import load_qa_chain\n",
    "from langchain.llms import OpenAI\n",
    "\n",
    "from gptcache.adapter.langchain_models import LangChainLLMs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f64b7f8",
   "metadata": {},
   "source": [
    "## Quickstart\n",
    "If you just want to get started as quickly as possible, this is the recommended way to do it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd9e6190",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# llm = OpenAI(temperature=0) # using the following code to cache with gptcache\n",
    "llm = LangChainLLMs(llm=OpenAI(temperature=0))\n",
    "chain = load_qa_chain(llm, chain_type=\"stuff\")\n",
    "query = \"What did the president say about Justice Breyer\"\n",
    "chain.run(input_documents=docs, question=query)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea01309",
   "metadata": {},
   "source": [
    "If you want more control and understanding over what is happening, please see the information below."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f78787a0",
   "metadata": {},
   "source": [
    "## The `stuff` Chain\n",
    "\n",
    "This sections shows results of using the `stuff` Chain to do question answering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "180fd4c1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain = load_qa_chain(llm, chain_type=\"stuff\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "77fdf1aa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output_text': ' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"What did the president say about Justice Breyer\"\n",
    "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ea50ad0",
   "metadata": {},
   "source": [
    "## The `refine` Chain\n",
    "\n",
    "This sections shows results of using the `refine` Chain to do question answering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fb167057",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain = load_qa_chain(llm, chain_type=\"refine\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d8b5286e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output_text': '\\nThe president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service. He also praised Justice Breyer for his dedication to the rule of law and his commitment to justice and fairness.'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"What did the president say about Justice Breyer\"\n",
    "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f95dfb2e",
   "metadata": {},
   "source": [
    "**Intermediate Steps**\n",
    "\n",
    "We can also return the intermediate steps for `refine` chains, should we want to inspect them. This is done with the `return_refine_steps` variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a5c64200",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chain = load_qa_chain(llm, chain_type=\"refine\", return_refine_steps=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "817546ac",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'intermediate_steps': [' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.',\n",
       "  ' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.',\n",
       "  ' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.',\n",
       "  '\\nThe president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service. He also praised Justice Breyer for his dedication to the rule of law and his commitment to justice and fairness.'],\n",
       " 'output_text': '\\nThe president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service. He also praised Justice Breyer for his dedication to the rule of law and his commitment to justice and fairness.'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0f0bbdf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "vscode": {
   "interpreter": {
    "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/langchain/sqlite.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ed6aab1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# SQLite Example\n",
    "\n",
    "This example showcases hooking up an LLM to answer questions over a database, and you can find the origin notebook in [LangChain example](https://github.com/hwchase17/langchain/blob/master/docs/modules/chains/examples/sqlite.ipynb), and this example will show you how to set the LLM with [GPTCache](https://github.com/zilliztech/GPTCache) so that you can cache the data with LLM. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1iWvezcPpnHEo_NF4wfzT5Mo5yk0dhxUC?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b149ab99",
   "metadata": {},
   "source": [
    "## Go into GPTCache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache.There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the DataBse cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems. And there is `get_content_func` for the cache settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13b9ad2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the content(only question) form the prompt to cache\n",
    "def get_content_func(data, **_):\n",
    "    return data.get(\"prompt\").split(\"Question\")[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff6051f",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c1d8b12f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# cache.init(pre_embedding_func=get_content_func)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfc5ab39",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors, it will use the `get_content_func` method\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to audio feature vector, response text in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3173048a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "\n",
    "onnx = Onnx()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='127.0.0.1', port='19530', dimension=onnx.dimension)\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "cache.init(\n",
    "    pre_embedding_func=get_content_func,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0afdbe86",
   "metadata": {},
   "source": [
    "After initializing the cache, you can use the LangChain LLMs with `gptcache.adapter.langchain_models`. At this point **gptcache** will cache the answer, the only difference from the original example is to change `llm = OpenAI(temperature=0)` to `llm = LangChainLLMs(llm=OpenAI(temperature=0))`, which will be commented in the code block. And you can also set the `session` to set the session settings with `llm = LangChainLLMs(llm=OpenAI(temperature=0), session=session)`, more details refer to [session example](https://github.com/zilliztech/GPTCache/tree/main/examples#how-to-run-with-session).\n",
    "\n",
    "Then you will find that it will be more fast when search the similar content, let's play with it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a37a26f4",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2f66479",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "This uses the example Chinook database.\n",
    "To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the `.db` file in a notebooks folder at the root of this repository."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0e27d88",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from langchain import OpenAI, SQLDatabase, SQLDatabaseChain\n",
    "\n",
    "from gptcache.adapter.langchain_models import LangChainLLMs\n",
    "from gptcache.session import Session\n",
    "session = Session(name=\"sqlite-example\") # set session for LangChainLLMs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72ede462",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "db = SQLDatabase.from_uri(\"sqlite:///./Chinook.db\")\n",
    "# llm = OpenAI(temperature=0) # using the following code to cache with gptcache\n",
    "llm = LangChainLLMs(llm=OpenAI(temperature=0), session=session)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d1e692e",
   "metadata": {},
   "source": [
    "**NOTE:** For data-sensitive projects, you can specify `return_direct=True` in the `SQLDatabaseChain` initialization to directly return the output of the SQL query without any additional formatting. This prevents the LLM from seeing any contents within the database. Note, however, the LLM still has access to the database scheme (i.e. dialect, table and key names) by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a8fc8f23",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "15ff81df",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "How many employees are there? \n",
      "SQLQuery:"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/chenshiyu/miniconda3/envs/shiyu/lib/python3.9/site-packages/langchain/sql_database.py:191: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage.\n",
      "  sample_rows = connection.execute(command)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[(8,)]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m There are 8 employees.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' There are 8 employees.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_chain.run(\"How many employees are there?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad2cba6",
   "metadata": {},
   "source": [
    "## Customize Prompt\n",
    "You can also customize the prompt that is used. Here is an example prompting it to understand that foobar is the same as the Employee table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8ca7bafb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts.prompt import PromptTemplate\n",
    "\n",
    "_DEFAULT_TEMPLATE = \"\"\"Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.\n",
    "Use the following format:\n",
    "\n",
    "Question: \"Question here\"\n",
    "SQLQuery: \"SQL Query to run\"\n",
    "SQLResult: \"Result of the SQLQuery\"\n",
    "Answer: \"Final answer here\"\n",
    "\n",
    "Only use the following tables:\n",
    "\n",
    "{table_info}\n",
    "\n",
    "If someone asks for the table foobar, they really mean the employee table.\n",
    "\n",
    "Question: {input}\"\"\"\n",
    "PROMPT = PromptTemplate(\n",
    "    input_variables=[\"input\", \"table_info\", \"dialect\"], template=_DEFAULT_TEMPLATE\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ec47a2bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, prompt=PROMPT, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ebb0674e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "How many employees are there in the foobar table? \n",
      "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[(8,)]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m There are 8 employees in the foobar table.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' There are 8 employees in the foobar table.'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_chain.run(\"How many employees are there in the foobar table?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88d8b969",
   "metadata": {},
   "source": [
    "## Return Intermediate Steps\n",
    "\n",
    "You can also return the intermediate steps of the SQLDatabaseChain. This allows you to access the SQL statement that was generated, as well as the result of running that against the SQL Database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "38559487",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, prompt=PROMPT, verbose=True, return_intermediate_steps=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "78b6af4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "How many employees are there in the foobar table? \n",
      "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[(8,)]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m There are 8 employees in the foobar table.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[' SELECT COUNT(*) FROM Employee;', '[(8,)]']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = db_chain(\"How many employees are there in the foobar table?\")\n",
    "result[\"intermediate_steps\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b408f800",
   "metadata": {},
   "source": [
    "## Choosing how to limit the number of rows returned\n",
    "If you are querying for several rows of a table you can select the maximum number of results you want to get by using the 'top_k' parameter (default is 10). This is useful for avoiding query results that exceed the prompt max length or consume tokens unnecessarily."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6adaa799",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True, top_k=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "edfc8a8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "What are some example tracks by composer Johann Sebastian Bach? \n",
      "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Name, Composer FROM Track WHERE Composer LIKE '%Johann Sebastian Bach%' LIMIT 3;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach')]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m Some example tracks by composer Johann Sebastian Bach are 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', and 'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude'.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' Some example tracks by composer Johann Sebastian Bach are \\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\\', \\'Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria\\', and \\'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\\'.'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_chain.run(\"What are some example tracks by composer Johann Sebastian Bach?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcc5e936",
   "metadata": {},
   "source": [
    "## Adding example rows from each table\n",
    "Sometimes, the format of the data is not obvious and it is optimal to include a sample of rows from the tables in the prompt to allow the LLM to understand the data before providing a final query. Here we will use this feature to let the LLM know that artists are saved with their full names by providing two rows from the `Track` table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9a22ee47",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = SQLDatabase.from_uri(\n",
    "    \"sqlite:///./Chinook.db\",\n",
    "    include_tables=['Track'], # we include only one table to save tokens in the prompt :)\n",
    "    sample_rows_in_table_info=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "952c0b4d",
   "metadata": {},
   "source": [
    "The sample rows are added to the prompt after each corresponding table's column information:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9de86267",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "CREATE TABLE \"Track\" (\n",
      "\t\"TrackId\" INTEGER NOT NULL, \n",
      "\t\"Name\" NVARCHAR(200) NOT NULL, \n",
      "\t\"AlbumId\" INTEGER, \n",
      "\t\"MediaTypeId\" INTEGER NOT NULL, \n",
      "\t\"GenreId\" INTEGER, \n",
      "\t\"Composer\" NVARCHAR(220), \n",
      "\t\"Milliseconds\" INTEGER NOT NULL, \n",
      "\t\"Bytes\" INTEGER, \n",
      "\t\"UnitPrice\" NUMERIC(10, 2) NOT NULL, \n",
      "\tPRIMARY KEY (\"TrackId\"), \n",
      "\tFOREIGN KEY(\"MediaTypeId\") REFERENCES \"MediaType\" (\"MediaTypeId\"), \n",
      "\tFOREIGN KEY(\"GenreId\") REFERENCES \"Genre\" (\"GenreId\"), \n",
      "\tFOREIGN KEY(\"AlbumId\") REFERENCES \"Album\" (\"AlbumId\")\n",
      ")\n",
      "\n",
      "/*\n",
      "2 rows from Track table:\n",
      "TrackId\tName\tAlbumId\tMediaTypeId\tGenreId\tComposer\tMilliseconds\tBytes\tUnitPrice\n",
      "1\tFor Those About To Rock (We Salute You)\t1\t1\t1\tAngus Young, Malcolm Young, Brian Johnson\t343719\t11170334\t0.99\n",
      "2\tBalls to the Wall\t2\t2\t1\tNone\t342562\t5510424\t0.99\n",
      "*/\n"
     ]
    }
   ],
   "source": [
    "print(db.table_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bcb7a489",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "81e05d82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "What are some example tracks by Bach? \n",
      "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Name, Composer FROM Track WHERE Composer LIKE '%Johann Sebastian Bach%' LIMIT 3;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach')]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m Examples of tracks by Johann Sebastian Bach include Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' Examples of tracks by Johann Sebastian Bach include Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_chain.run(\"What are some example tracks by Bach?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef94e948",
   "metadata": {},
   "source": [
    "### Custom Table Info\n",
    "In some cases, it can be useful to provide custom table information instead of using the automatically generated table definitions and the first `sample_rows_in_table_info` sample rows. For example, if you know that the first few rows of a table are uninformative, it could help to manually provide example rows that are more diverse or provide more information to the model. It is also possible to limit the columns that will be visible to the model if there are unnecessary columns. \n",
    "\n",
    "This information can be provided as a dictionary with table names as the keys and table information as the values. For example, let's provide a custom definition and sample rows for the Track table with only a few columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2ad33ab1",
   "metadata": {},
   "outputs": [],
   "source": [
    "custom_table_info = {\n",
    "    \"Track\": \"\"\"CREATE TABLE Track (\n",
    "\t\"TrackId\" INTEGER NOT NULL, \n",
    "\t\"Name\" NVARCHAR(200) NOT NULL,\n",
    "\t\"Composer\" NVARCHAR(220),\n",
    "\tPRIMARY KEY (\"TrackId\")\n",
    ")\n",
    "/*\n",
    "3 rows from Track table:\n",
    "TrackId\tName\tComposer\n",
    "1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n",
    "2\tBalls to the Wall\tNone\n",
    "3\tMy favorite song ever\tThe coolest composer of all time\n",
    "*/\"\"\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "db144352",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "CREATE TABLE \"Playlist\" (\n",
      "\t\"PlaylistId\" INTEGER NOT NULL, \n",
      "\t\"Name\" NVARCHAR(120), \n",
      "\tPRIMARY KEY (\"PlaylistId\")\n",
      ")\n",
      "\n",
      "/*\n",
      "2 rows from Playlist table:\n",
      "PlaylistId\tName\n",
      "1\tMusic\n",
      "2\tMovies\n",
      "*/\n",
      "\n",
      "CREATE TABLE Track (\n",
      "\t\"TrackId\" INTEGER NOT NULL, \n",
      "\t\"Name\" NVARCHAR(200) NOT NULL,\n",
      "\t\"Composer\" NVARCHAR(220),\n",
      "\tPRIMARY KEY (\"TrackId\")\n",
      ")\n",
      "/*\n",
      "3 rows from Track table:\n",
      "TrackId\tName\tComposer\n",
      "1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n",
      "2\tBalls to the Wall\tNone\n",
      "3\tMy favorite song ever\tThe coolest composer of all time\n",
      "*/\n"
     ]
    }
   ],
   "source": [
    "db = SQLDatabase.from_uri(\n",
    "    \"sqlite:///./Chinook.db\",\n",
    "    include_tables=['Track', 'Playlist'],\n",
    "    sample_rows_in_table_info=2,\n",
    "    custom_table_info=custom_table_info)\n",
    "\n",
    "print(db.table_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fc6f507",
   "metadata": {},
   "source": [
    "Note how our custom table definition and sample rows for `Track` overrides the `sample_rows_in_table_info` parameter. Tables that are not overridden by `custom_table_info`, in this example `Playlist`, will have their table info gathered automatically as usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dfbda4e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
      "What are some example tracks by Bach? \n",
      "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Name, Composer FROM Track WHERE Composer LIKE '%Johann Sebastian Bach%' LIMIT 3;\u001b[0m\n",
      "SQLResult: \u001b[33;1m\u001b[1;3m[('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach')]\u001b[0m\n",
      "Answer:\u001b[32;1m\u001b[1;3m Some example tracks by Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' Some example tracks by Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 \"Goldberg Variations\": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n",
    "db_chain.run(\"What are some example tracks by Bach?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eb39db6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/llama_index/index.rst
================================================
Llama Index
=================

.. toctree::
   :maxdepth: 1
   :caption: Contents:


   webpage_qa

================================================
FILE: docs/bootcamp/llama_index/webpage_qa.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d39937ca",
   "metadata": {},
   "source": [
    "# WebPage QA\n",
    "\n",
    "This is an example of a QA example, reference [llama_index example](https://gpt-index.readthedocs.io/en/latest/examples/data_connectors/WebPageDemo.html#using-simplewebpagereader). \n",
    "\n",
    "It works by first obtaining a list of URLs from the user, then extracting relevant information from the pages associated with those URLs.Next, a vector index is created based on this information, and finally, the program is able to answer questions using the indexed information.\n",
    "\n",
    "Before running this example, please set **OPENAI_API_KEY** environment param."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a411d62e",
   "metadata": {},
   "source": [
    "## Init GPTCache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c2022a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "from gptcache import Cache\n",
    "from gptcache.adapter.api import init_similar_cache\n",
    "\n",
    "\n",
    "def get_hashed_name(name):\n",
    "    return hashlib.sha256(name.encode()).hexdigest()\n",
    "\n",
    "\n",
    "def init_gptcache(cache_obj: Cache, llm: str):\n",
    "    hashed_llm = get_hashed_name(llm)\n",
    "    init_similar_cache(cache_obj=cache_obj, data_dir=f\"similar_cache_{hashed_llm}\")\n",
    "\n",
    "gptcache_obj = GPTCache(init_gptcache)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c24b1889",
   "metadata": {},
   "source": [
    "## Load WebPage Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "36f5198f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import (\n",
    "    GPTVectorStoreIndex,\n",
    "    ServiceContext,\n",
    "    LLMPredictor,\n",
    "    SimpleWebPageReader,\n",
    ")\n",
    "\n",
    "loader = SimpleWebPageReader(html_to_text=True)\n",
    "documents = loader.load_data(urls=[\"https://milvus.io/docs/overview.md\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff637b81",
   "metadata": {},
   "source": [
    "## Build Index and Get Query Engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c3753032",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = GPTVectorStoreIndex.from_documents(\n",
    "    documents,\n",
    "    service_context=ServiceContext.from_defaults(\n",
    "        llm_predictor=LLMPredictor(cache=gptcache_obj)\n",
    "    ),\n",
    ")\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "200c03c5",
   "metadata": {},
   "source": [
    "## Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b933dd69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Milvus is an open source vector database for building and managing large-scale AI applications. It provides fast and accurate vector search capabilities, enabling users to quickly search and retrieve vectors from large datasets.\n",
      "CPU times: user 1.21 s, sys: 206 ms, total: 1.42 s\n",
      "Wall time: 9.69 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "print(query_engine.query(\"What is milvus?\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2dae9741",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Milvus is an open source vector database for building and managing large-scale AI applications. It provides fast and accurate vector search capabilities, enabling users to quickly search and retrieve vectors from large datasets.\n",
      "CPU times: user 784 ms, sys: 17.8 ms, total: 801 ms\n",
      "Wall time: 940 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "print(query_engine.query(\"What's milvus?\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/chat.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b3ba1cc",
   "metadata": {},
   "source": [
    "# Chat\n",
    "\n",
    "This example will show you how to chat with GPT, the original example is on [OpenAI Example](https://platform.openai.com/docs/guides/chat/introduction), the difference is that we will teach you how to cache the  response for exact and similar matches with **gptcache**, it will be very simple, you just need to add an extra step to initialize the cache.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.\n",
    "\n",
    "Then we can learn the usage and acceleration effect of gptcache by the following code, which consists of three parts, the original openai way, the exact search and the similar search. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1m1s-iTDfLDk-UwUAQ_L8j1C-gzkcr2Sk?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa0ba70e",
   "metadata": {},
   "source": [
    "## OpenAI API original usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80e9dae2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: what‘s github\n",
      "Time consuming: 6.04s\n",
      "Answer: GitHub is a web-based platform used for version control and collaboration of coding projects. It allows individuals and teams to store, share, and collaborate on changes to code, software, and applications. It also provides features such as issue tracking, project management tools, and code review. It is one of the most popular and widely used online platforms for open-source projects.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import openai\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "\n",
    "question = 'what‘s github'\n",
    "\n",
    "# OpenAI API original usage\n",
    "start_time = time.time()\n",
    "response = openai.ChatCompletion.create(\n",
    "  model='gpt-3.5-turbo',\n",
    "  messages=[\n",
    "    {\n",
    "        'role': 'user',\n",
    "        'content': question\n",
    "    }\n",
    "  ],\n",
    ")\n",
    "print(f'Question: {question}')\n",
    "print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d871550",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, exact match cache\n",
    "\n",
    "Initalize the cache to run GPTCache and import `openai` form `gptcache.adapter`, which will automatically set the map data manager to match the exact cahe, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "And if you ask ChatGPT the exact same two questions, the answer to the second question will be obtained from the cache without requesting ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "024484f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Question: what's github\n",
      "Time consuming: 6.88s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, share, and collaborate on programming projects. It is primarily used for version control, where developers can work on different features and changes of a project simultaneously without overwriting each other's work. GitHub also provides tools for issue tracking, code review, and project management. It is widely used in the open-source community and by software development teams in organizations of all sizes.\n",
      "\n",
      "Question: what's github\n",
      "Time consuming: 0.00s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, share, and collaborate on programming projects. It is primarily used for version control, where developers can work on different features and changes of a project simultaneously without overwriting each other's work. GitHub also provides tools for issue tracking, code review, and project management. It is widely used in the open-source community and by software development teams in organizations of all sizes.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "# To use GPTCache, that's all you need\n",
    "# -------------------------------------------------\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "\n",
    "cache.init()\n",
    "cache.set_openai_key()\n",
    "# -------------------------------------------------\n",
    "\n",
    "question = \"what's github\"\n",
    "for _ in range(2):\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "      model='gpt-3.5-turbo',\n",
    "      messages=[\n",
    "        {\n",
    "            'role': 'user',\n",
    "            'content': question\n",
    "        }\n",
    "      ],\n",
    "    )\n",
    "    print(f'Question: {question}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f2ff699",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, similar search cache\n",
    "\n",
    "Set the cache with `embedding_func` to generate embedding for the text, and `data_manager` to manager the cache data, `similarity_evaluation` to evaluate the similarities, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "After obtaining an answer from ChatGPT in response to several similar questions, the answers to subsequent questions can be retrieved from the cache without the need to request ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd1ff06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Question: what's github\n",
      "Time consuming: 7.11s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: can you explain what GitHub is\n",
      "Time consuming: 0.19s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: can you tell me more about GitHub\n",
      "Time consuming: 0.23s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: what is the purpose of GitHub\n",
      "Time consuming: 0.21s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "onnx = Onnx()\n",
    "data_manager = get_data_manager(CacheBase(\"sqlite\"), VectorBase(\"faiss\", dimension=onnx.dimension))\n",
    "cache.init(\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()\n",
    "\n",
    "questions = [\n",
    "    \"what's github\",\n",
    "    \"can you explain what GitHub is\",\n",
    "    \"can you tell me more about GitHub\",\n",
    "    \"what is the purpose of GitHub\"\n",
    "]\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model='gpt-3.5-turbo',\n",
    "        messages=[\n",
    "            {\n",
    "                'role': 'user',\n",
    "                'content': question\n",
    "            }\n",
    "        ],\n",
    "    )\n",
    "    print(f'Question: {question}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07d92eae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/image_generation.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8b0e0905",
   "metadata": {},
   "source": [
    "# Image Generation\n",
    "\n",
    "This example will show you how to use GPTCache and OpenAI to implement image generation, i.e. to generate relevant images based on text descriptions. Where the OpenAI model will be used to generate the images, and GPTCache will cache the generated images so that the next time the same or similar text description is requested, it can be returned directly from the cache, which can improve efficiency and reduce costs.\n",
    "\n",
    "\n",
    "This bootcamp is divided into three parts: how to initialize gptcache, running the openai model to generate images, and finally showing how to start the service with gradio. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1YRNR5MLux9UbbWs0SgGI3OSPfkPqIBrM?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b0f837",
   "metadata": {},
   "source": [
    "## Initialize the gptcache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache. There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the database cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46326449",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache\n",
    "\n",
    "`cache.init` is used to initialize gptcache, the default is to use map to search for cached data, `pre_embedding_func` is used to pre-process the data inserted into the cache, more configuration refer to [initialize Cache](https://gptcache.readthedocs.io/en/latest/references/gptcache.html#module-gptcache.Cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bbd4d14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# from gptcache.adapter import openai\n",
    "# from gptcache.processor.pre import get_prompt\n",
    "\n",
    "# cache.init(pre_embedding_func=get_prompt)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ebda74",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to store text, feature vector, and image object data, in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data). Also you can [set ObjectBase](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.object_data) to configure which method to use to save the generated image, this example will be stored locally, you can also set it to S3 storage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db7e64ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.processor.pre import get_prompt\n",
    "\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase\n",
    "\n",
    "\n",
    "onnx = Onnx()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='localhost', port='19530', dimension=onnx.dimension)\n",
    "object_base = ObjectBase('local', path='./images')\n",
    "data_manager = get_data_manager(cache_base, vector_base, object_base)\n",
    "\n",
    "cache.init(\n",
    "    pre_embedding_func=get_prompt,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4201d301",
   "metadata": {},
   "source": [
    "## Run openai image generation \n",
    "\n",
    "Then run `openai.Image.create` to generate the image. The generated images can have a size of \"256x256\", \"512x512\", or \"1024x1024\" pixels, and smaller sizes are faster to generate.\n",
    "\n",
    "\n",
    "Note that `openai` here is imported from `gptcache.adapter.openai`, which can be used to cache with gptcache at request time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7555c46b",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = openai.Image.create(\n",
    "  prompt=\"a white siamese cat\",\n",
    "  n=1,\n",
    "  size=\"256x256\"\n",
    ")\n",
    "image_url = response['data'][0]['url']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d816d5",
   "metadata": {},
   "source": [
    "## Start with gradio\n",
    "\n",
    "Finally, we can start a gradio application for image generation.\n",
    "\n",
    "First define the `image_generation` method, which is used to generate an image based on the input text and also return whether the cache hit or not. Then start the service with gradio, as shown below:\n",
    "\n",
    "![](../assets/image_generation_gradio.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b160e01f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def image_generation(prompt):\n",
    "    response = openai.Image.create(\n",
    "      prompt=prompt,\n",
    "      n=1,\n",
    "      size=\"256x256\"\n",
    "    )\n",
    "    return response['data'][0]['url'], response.get(\"gptcache\", False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5f12582",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio\n",
    "\n",
    "interface = gradio.Interface(image_generation, \n",
    "                             gradio.Textbox(lines=1, placeholder=\"Description Here...\"),\n",
    "                             [gradio.Image(shape=(200, 200)), gradio.Textbox(label=\"is hit\")]\n",
    "                            )\n",
    "\n",
    "interface.launch(inline=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2b5959d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/index.rst
================================================
OpenAI
=================

.. toctree::
   :maxdepth: 1
   :caption: Contents:


   chat
   image_generation
   sql_translate
   tweet_classifier
   image_generation
   speech_to_text


================================================
FILE: docs/bootcamp/openai/language_translate.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ac4a3bc",
   "metadata": {},
   "source": [
    "# Language Translation \n",
    "\n",
    "This example will show you how to translates English to other languages, the original example is on [OpenAI Example](https://platform.openai.com/examples/default-translate), the difference is that we will teach you how to cache the response for exact and similar matches with **gptcache**, it will be very simple, you just need to add an extra step to initialize the cache.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.\n",
    "\n",
    "Then we can learn the usage and acceleration effect of gptcache by the following code, which consists of three parts, the original openai way, the exact search and the similar search."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79934be0",
   "metadata": {},
   "source": [
    "## OpenAI API original usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "74499030",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Answer: 1. Quels salles avez-vous disponibles?\n",
      "2. ¿Qué habitaciones tienen disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 2.06s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import openai\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "start_time = time.time()\n",
    "response = openai.Completion.create(\n",
    "  model=\"text-davinci-003\",\n",
    "  prompt=\"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhat rooms do you have available?\\n\\n1.\",\n",
    "  temperature=0.3,\n",
    "  max_tokens=100,\n",
    "  top_p=1.0,\n",
    "  frequency_penalty=0.0,\n",
    "  presence_penalty=0.0\n",
    ")\n",
    "\n",
    "print(f\"\\nAnswer: 1.{response_text(response)}\")\n",
    "print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8f271f0",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, exact match cache\n",
    "\n",
    "Initalize the cache to run GPTCache and import `openai` form `gptcache.adapter`, which will automatically set the map data manager to match the exact cahe, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76bdb5e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "\n",
      "Answer: 1. Quels sont les chambres que vous avez disponibles ?\n",
      "2. ¿Qué habitaciones tienes disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 1.81s\n",
      "\n",
      "Answer: 1. Quelles pièces avez-vous disponibles?\n",
      "2. ¿Qué habitaciones tienen disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 4.47s\n",
      "\n",
      "Answer: 1. Quels types de chambres avez-vous disponibles ?\n",
      "2. ¿Qué tipos de habitaciones tienen disponibles?\n",
      "3. どんな部屋が利用可能ですか?\n",
      "Time consuming: 1.40s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "# To use GPTCache, that's all you need\n",
    "# -------------------------------------------------\n",
    "from gptcache import cache\n",
    "from gptcache.processor.pre import get_prompt\n",
    "\n",
    "cache.init(pre_embedding_func=get_prompt)\n",
    "cache.set_openai_key()\n",
    "# -------------------------------------------------\n",
    "\n",
    "questions = [\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhat rooms do you have available?\\n\\n1.\",\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhich rooms do you have available?\\n\\n1.\",\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhat kind of rooms do you have available?\\n\\n1.\",\n",
    "]\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.Completion.create(\n",
    "                  model=\"text-davinci-003\",\n",
    "                  prompt=question,\n",
    "                  temperature=0.3,\n",
    "                  max_tokens=100,\n",
    "                  top_p=1.0,\n",
    "                  frequency_penalty=0.0,\n",
    "                  presence_penalty=0.0\n",
    "                )\n",
    "    print(f\"\\nAnswer: 1.{response_text(response)}\")\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba5b959a",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, similar search cache\n",
    "\n",
    "Set the cache with `pre_embedding_func` to preprocess the input data, `embedding_func` to generate embedding for the text, and `data_manager` to manager the cache data, `similarity_evaluation` to evaluate the similarities, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2278a87",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "\n",
      "Answer: 1. Quels salles avez-vous disponibles?\n",
      "2. ¿Qué habitaciones tienes disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 4.40s\n",
      "\n",
      "Answer: 1. Quels salles avez-vous disponibles?\n",
      "2. ¿Qué habitaciones tienes disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 0.19s\n",
      "\n",
      "Answer: 1. Quels salles avez-vous disponibles?\n",
      "2. ¿Qué habitaciones tienes disponibles?\n",
      "3. どの部屋が利用可能ですか?\n",
      "Time consuming: 0.21s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.processor.pre import get_prompt\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "onnx = Onnx()\n",
    "data_manager = get_data_manager(CacheBase(\"sqlite\"), VectorBase(\"faiss\", dimension=onnx.dimension))\n",
    "cache.init(pre_embedding_func=get_prompt,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()\n",
    "\n",
    "questions = [\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhat rooms do you have available?\\n\\n1.\",\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhich rooms do you have available?\\n\\n1.\",\n",
    "    \"Translate this into 1. French, 2. Spanish and 3. Japanese:\\n\\nWhat kind of rooms do you have available?\\n\\n1.\",\n",
    "]\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.Completion.create(\n",
    "                  model=\"text-davinci-003\",\n",
    "                  prompt=question,\n",
    "                  temperature=0.3,\n",
    "                  max_tokens=100,\n",
    "                  top_p=1.0,\n",
    "                  frequency_penalty=0.0,\n",
    "                  presence_penalty=0.0\n",
    "                )\n",
    "    print(f\"\\nAnswer: 1.{response_text(response)}\")\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2094a47",
   "metadata": {},
   "source": [
    "We find that the performance improvement when searching the similar because the three statements of the query are similar, and hitting cache in gptcache, so it will return the cached results directly instead of requesting. And you can then also try running the query again for exact search, which will also speed up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4030b170",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/speech_to_text.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8b0e0905",
   "metadata": {},
   "source": [
    "# Speech to Text\n",
    "\n",
    "This example will show you how to use GPTCache and OpenAI to implement speech to text, i.e. to turn audio into text. Where the OpenAI model will be used to turn the audio data, and GPTCache will cache the generated text so that the next time the same or similar audio is requested, it can be returned directly from the cache, which can improve efficiency and reduce costs.\n",
    "\n",
    "This bootcamp is divided into three parts: how to initialize gptcache, running the openai model to turn audio data into text, and finally showing how to start the service with gradio. You can also find the colab in here. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1vtiD-emu9gJdzPq0c5dwGOV2ngmug9EN?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b0f837",
   "metadata": {},
   "source": [
    "## Initialize the gptcache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache. There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the database cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46326449",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache\n",
    "\n",
    "`cache.init` is used to initialize gptcache, the default is to use map to search for cached data, `pre_embedding_func` is used to pre-process the data inserted into the cache, and it will use the `get_file_bytes` method, more configuration refer to [initialize Cache](https://gptcache.readthedocs.io/en/latest/references/gptcache.html#module-gptcache.Cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bbd4d14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# from gptcache.adapter import openai\n",
    "# from gptcache.processor.pre import get_file_bytes\n",
    "\n",
    "# cache.init(pre_embedding_func=get_file_bytes)\n",
    "# cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ebda74",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: pre-processing before extracting feature vectors, it will use the `get_file_name` method\n",
    "- `embedding_func`: the method to extract the text feature vector\n",
    "- `data_manager`: DataManager for cache management\n",
    "- `similarity_evaluation`: the evaluation method after the cache hit\n",
    "\n",
    "The `data_manager` is used to audio feature vector, response text in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db7e64ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.processor.pre import get_file_name\n",
    "\n",
    "from gptcache.embedding import Data2VecAudio\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase\n",
    "\n",
    "\n",
    "data2vec = Data2VecAudio()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='localhost', port='19530', dimension=data2vec.dimension)\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "\n",
    "cache.init(\n",
    "    pre_embedding_func=get_file_name,\n",
    "    embedding_func=data2vec.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4201d301",
   "metadata": {},
   "source": [
    "## Run openai speech to text \n",
    "\n",
    "Then run `openai.Audio.translations`, which can translate and transcribe the audio into english, and it is based on large-v2 Whisper model. The file uploads are currently limited to 25 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.\n",
    "\n",
    "Note that `openai` here is imported from `gptcache.adapter.openai`, which can be used to cache with gptcache at request time. Please download the [blues.00000.mp3](https://github.com/towhee-io/examples/releases/download/data/blues.00000.mp3) before running the following code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7555c46b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "One bourbon, one scotch and one bill Hey Mr. Bartender, come here I want another drink and I want it now My baby she gone, she been gone tonight I ain't seen my baby since night of her life One bourbon, one scotch and one bill\n"
     ]
    }
   ],
   "source": [
    "audio_file= open(\"./blues.00000.mp3\", \"rb\")\n",
    "transcript = openai.Audio.transcribe(\"whisper-1\", audio_file)\n",
    "print(transcript[\"text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d816d5",
   "metadata": {},
   "source": [
    "## Start with gradio\n",
    "\n",
    "Finally, we can start a gradio application to translate and transcribe the audio.\n",
    "\n",
    "First define the `speech_to_text` method, which is used to generate text based on the input audio and also return whether the cache hit or not. Then start the service with gradio, as shown below:\n",
    "\n",
    "![](../assets/speech_to_text_gradio.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b160e01f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def speech_to_text(audio_file):\n",
    "    audio_file= open(audio_file, \"rb\")\n",
    "    transcript = openai.Audio.transcribe(\"whisper-1\", audio_file)\n",
    "    return transcript[\"text\"], transcript.get(\"gptcache\", False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6cbf624",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio\n",
    "\n",
    "interface = gradio.Interface(speech_to_text, \n",
    "                             gradio.Audio(source=\"upload\", type=\"filepath\"),\n",
    "                             [gradio.Textbox(label=\"transcript\"), gradio.Textbox(label=\"is hit\")]\n",
    "                            )\n",
    "\n",
    "interface.launch(inline=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2b5959d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/sql_translate.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d5f1205",
   "metadata": {},
   "source": [
    "# SQL Translate\n",
    "\n",
    "This example will show you how to translate natural language to SQL queries, the original example is on [OpenAI Example](https://platform.openai.com/examples/default-sql-translate), the difference is that we will teach you how to cache the response for exact and similar matches with **gptcache**, it will be very simple, you just need to add an extra step to initialize the cache.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.\n",
    "\n",
    "Then we can learn the usage and acceleration effect of gptcache by the following code, which consists of three parts, the original openai way, the exact search and the similar search."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dd7eb8a",
   "metadata": {},
   "source": [
    "## OpenAI API original usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6fe19528",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "SELECT  d.name \n",
      "FROM Department d \n",
      "INNER JOIN Employee e ON d.id = e.department_id \n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id \n",
      "WHERE sp.date > NOW() - INTERVAL '3 months' \n",
      "GROUP BY d.name \n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 2.78s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import openai\n",
    "\n",
    "question = \"A query to list the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\"\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "start_time = time.time()\n",
    "response = openai.Completion.create(\n",
    "  model=\"text-davinci-003\",\n",
    "  prompt=\"### Postgres SQL tables, with their properties:\\n#\\n# Employee(id, name, department_id)\\n# Department(id, name, address)\\n# Salary_Payments(id, employee_id, amount, date)\\n#\\n### \" + question,\n",
    "  temperature=0,\n",
    "  max_tokens=150,\n",
    "  top_p=1.0,\n",
    "  frequency_penalty=0.0,\n",
    "  presence_penalty=0.0,\n",
    "  stop=[\"#\", \";\"]\n",
    ")\n",
    "print(\"\\nSELECT\", response_text(response))\n",
    "print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37112300",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, exact match cache\n",
    "\n",
    "Initalize the cache to run GPTCache and import `openai` form `gptcache.adapter`, which will automatically set the map data manager to match the exact cahe, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7b5a627c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "\n",
      "SELECT  d.name \n",
      "FROM Department d \n",
      "INNER JOIN Employee e ON d.id = e.department_id \n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id \n",
      "WHERE sp.date > NOW() - INTERVAL '3 months' \n",
      "GROUP BY d.name \n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 1.88s\n",
      "\n",
      "SELECT  d.name\n",
      "FROM Department d\n",
      "INNER JOIN Employee e ON d.id = e.department_id\n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id\n",
      "WHERE sp.date > NOW() - INTERVAL '3 months'\n",
      "GROUP BY d.name\n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 1.46s\n",
      "\n",
      "SELECT  d.name\n",
      "FROM Department d\n",
      "INNER JOIN Employee e ON d.id = e.department_id\n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id\n",
      "WHERE sp.date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)\n",
      "GROUP BY d.name\n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 2.08s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "# To use GPTCache, that's all you need\n",
    "# -------------------------------------------------\n",
    "from gptcache import cache\n",
    "from gptcache.processor.pre import get_prompt\n",
    "\n",
    "cache.init(pre_embedding_func=get_prompt)\n",
    "cache.set_openai_key()\n",
    "# -------------------------------------------------\n",
    "\n",
    "questions = [\n",
    "    \"A query to list the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "    \"Query the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "    \"List the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "]\n",
    "\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.Completion.create(\n",
    "      model=\"text-davinci-003\",\n",
    "      prompt=\"### Postgres SQL tables, with their properties:\\n#\\n# Employee(id, name, department_id)\\n# Department(id, name, address)\\n# Salary_Payments(id, employee_id, amount, date)\\n#\\n### \" + question,\n",
    "      temperature=0,\n",
    "      max_tokens=150,\n",
    "      top_p=1.0,\n",
    "      frequency_penalty=0.0,\n",
    "      presence_penalty=0.0,\n",
    "      stop=[\"#\", \";\"]\n",
    "    )\n",
    "    print(\"\\nSELECT\", response_text(response))\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b57d718a",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, similar search cache\n",
    "\n",
    "Set the cache with `pre_embedding_func` to preprocess the input data, `embedding_func` to generate embedding for the text, and `data_manager` to manager the cache data, `similarity_evaluation` to evaluate the similarities, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4018bb39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "A query to list the names of the departments which employed more than 10 employees in the last 3 months\n",
      "SELECT  d.name \n",
      "FROM Department d \n",
      "INNER JOIN Employee e ON d.id = e.department_id \n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id \n",
      "WHERE sp.date > NOW() - INTERVAL '3 months' \n",
      "GROUP BY d.name \n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 1.89s\n",
      "Query the names of the departments which employed more than 10 employees in the last 3 months\n",
      "SELECT  d.name \n",
      "FROM Department d \n",
      "INNER JOIN Employee e ON d.id = e.department_id \n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id \n",
      "WHERE sp.date > NOW() - INTERVAL '3 months' \n",
      "GROUP BY d.name \n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 0.25s\n",
      "List the names of the departments which employed more than 10 employees in the last 3 months\n",
      "SELECT  d.name \n",
      "FROM Department d \n",
      "INNER JOIN Employee e ON d.id = e.department_id \n",
      "INNER JOIN Salary_Payments sp ON e.id = sp.employee_id \n",
      "WHERE sp.date > NOW() - INTERVAL '3 months' \n",
      "GROUP BY d.name \n",
      "HAVING COUNT(*) > 10\n",
      "Time consuming: 0.22s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp[\"choices\"][0][\"text\"]\n",
    "\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.processor.pre import get_prompt\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "onnx = Onnx()\n",
    "data_manager = get_data_manager(CacheBase(\"sqlite\"), VectorBase(\"faiss\", dimension=onnx.dimension))\n",
    "cache.init(pre_embedding_func=get_prompt,\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()\n",
    "\n",
    "questions = [\n",
    "    \"A query to list the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "    \"Query the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "    \"List the names of the departments which employed more than 10 employees in the last 3 months\\nSELECT\",\n",
    "]\n",
    "\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.Completion.create(\n",
    "      model=\"text-davinci-003\",\n",
    "      prompt=\"### Postgres SQL tables, with their properties:\\n#\\n# Employee(id, name, department_id)\\n# Department(id, name, address)\\n# Salary_Payments(id, employee_id, amount, date)\\n#\\n### \" + question,\n",
    "      temperature=0,\n",
    "      max_tokens=150,\n",
    "      top_p=1.0,\n",
    "      frequency_penalty=0.0,\n",
    "      presence_penalty=0.0,\n",
    "      stop=[\"#\", \";\"]\n",
    "    )\n",
    "    print(question, response_text(response))\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8666399e",
   "metadata": {},
   "source": [
    "We find that the performance improvement when searching the similar because the three statements of the query are similar, and hitting cache in gptcache, so it will return the cached results directly instead of requesting. And you can then also try running the query again for exact search, which will also speed up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6a69fef",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/openai/tweet_classifier.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b3ba1cc",
   "metadata": {},
   "source": [
    "# Tweet Classifier\n",
    "\n",
    "This example will show you how to determine sentiment of tweets, the original example is on [OpenAI Example](https://platform.openai.com/examples/default-tweet-classifier), the difference is that we will teach you how to cache the response for exact and similar matches with **gptcache**, it will be very simple, you just need to add an extra step to initialize the cache.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.\n",
    "\n",
    "Then we can learn the usage and acceleration effect of gptcache by the following code, which consists of three parts, the original openai way, the exact search and the similar search."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa0ba70e",
   "metadata": {},
   "source": [
    "## OpenAI API original usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "80e9dae2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tweet: I loved the new Batman movie!\n",
      "Time consuming: 0.81s\n",
      "Sentiment: Positive\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import openai\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "tweet = \"I loved the new Batman movie!\"\n",
    "\n",
    "# OpenAI API original usage\n",
    "start_time = time.time()\n",
    "response = openai.ChatCompletion.create(\n",
    "  model='gpt-3.5-turbo',\n",
    "  messages=[\n",
    "    {\n",
    "        'role': 'user',\n",
    "        'content': f\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\\n\\nTweet: \\\"{tweet}\\\"\\nSentiment:\",\n",
    "    }\n",
    "  ],\n",
    ")\n",
    "print(f'Tweet: {tweet}')\n",
    "print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "print(f'Sentiment: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d871550",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, exact match cache\n",
    "\n",
    "Initalize the cache to run GPTCache and import `openai` form `gptcache.adapter`, which will automatically set the map data manager to match the exact cahe, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "And if you send the exact same tweets, the answer to the second tweet will be obtained from the cache without requesting ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "024484f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Tweet: The weather today is neither good nor bad\n",
      "Time consuming: 0.62s\n",
      "Sentiment: neutral\n",
      "\n",
      "Tweet: The weather today is neither good nor bad\n",
      "Time consuming: 0.00s\n",
      "Sentiment: neutral\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "# To use GPTCache, that's all you need\n",
    "# -------------------------------------------------\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "\n",
    "cache.init()\n",
    "cache.set_openai_key()\n",
    "# -------------------------------------------------\n",
    "\n",
    "tweet = \"The weather today is neither good nor bad\"\n",
    "for _ in range(2):\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "      model='gpt-3.5-turbo',\n",
    "      messages=[\n",
    "        {\n",
    "            'role': 'user',\n",
    "            'content': f\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\\n\\nTweet: \\\"{tweet}\\\"\\nSentiment:\",\n",
    "        }\n",
    "      ],\n",
    "    )\n",
    "    print(f'Tweet: {tweet}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Sentiment: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f2ff699",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, similar search cache\n",
    "\n",
    "We are going to use [DocArray's in-memory](https://docs.docarray.org/user_guide/storing/index_in_memory/) index to perform similarity search.\n",
    "\n",
    "Set the cache with `embedding_func` to generate embedding for the text, and `data_manager` to manager the cache data, `similarity_evaluation` to evaluate the similarities, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "After obtaining an answer from ChatGPT in response to several similar tweets, the answers to subsequent questions can be retrieved from the cache without the need to request ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fd1ff06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/jinaai/Desktop/GPTCache/venv1/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Tweet: The new restaurant in town exceeded my expectations with its delectable cuisine and impeccable service\n",
      "Time consuming: 0.70s\n",
      "Sentiment:  Positive\n",
      "\n",
      "Tweet: New restaurant in town exceeded my expectations with its delectable cuisine and impeccable service\n",
      "Time consuming: 0.59s\n",
      "Sentiment:  Positive\n",
      "\n",
      "Tweet: The new restaurant exceeded my expectations with its delectable cuisine and impeccable service\n",
      "Time consuming: 0.74s\n",
      "Sentiment:  Positive\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "onnx = Onnx()\n",
    "data_manager = get_data_manager(CacheBase(\"sqlite\"), VectorBase(\"docarray\"))\n",
    "cache.init(\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()\n",
    "\n",
    "tweets = [\n",
    "    \"The new restaurant in town exceeded my expectations with its delectable cuisine and impeccable service\",\n",
    "    \"New restaurant in town exceeded my expectations with its delectable cuisine and impeccable service\",\n",
    "    \"The new restaurant exceeded my expectations with its delectable cuisine and impeccable service\",\n",
    "]\n",
    "\n",
    "for tweet in tweets:\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model='gpt-3.5-turbo',\n",
    "        messages=[\n",
    "            {\n",
    "                'role': 'user',\n",
    "                'content': f\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\\n\\nTweet: \\\"{tweet}\\\"\\nSentiment:\",\n",
    "            }\n",
    "        ],\n",
    "    )\n",
    "    print(f'Tweet: {tweet}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Sentiment: {response_text(response)}\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/replicate/index.rst
================================================
Replicate
=================

.. toctree::
   :maxdepth: 1
   :caption: Contents:


   visual_question_answering

================================================
FILE: docs/bootcamp/replicate/visual_question_answering.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39354318",
   "metadata": {},
   "source": [
    "# Visual Question Answering\n",
    "\n",
    "This example will show you how to use GPTCache and Replicate to implement question answering about images, it uses BLIP model to answer free-form questions about images in natural language. Where the Replicate will be used to return the answer, and GPTCache will cache the generated answer so that the next time the same or similar question about the image is requested, it can be returned directly from the cache, which can improve efficiency and reduce costs.\n",
    "\n",
    "This bootcamp is divided into three parts: how to initialize gptcache, running the Replicate model to get the answer, and finally showing how to start the service with gradio. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1W6dQfkX9p8cMfdIuWBVPi-iru7OlGPSH?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7abc8f5",
   "metadata": {},
   "source": [
    "## Initialize the gptcache\n",
    "\n",
    "Please [install gptcache](https://gptcache.readthedocs.io/en/latest/index.html#) first, then we can initialize the cache. There are two ways to initialize the cache, the first is to use the map cache (exact match cache) and the second is to use the database cache (similar search cache), it is more recommended to use the second one, but you have to install the related requirements.\n",
    "\n",
    "Before running the example, make sure the `REPLICATE_API_TOKEN` environment variable is set by executing `echo $REPLICATE_API_TOKEN`. If it is not already set, it can be set by using `export REPLICATE_API_TOKEN=YOUR_API_TOKEN` on Unix/Linux/MacOS systems or `set REPLICATE_API_TOKEN=YOUR_API_TOKEN` on Windows systems."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f78f8d1b",
   "metadata": {},
   "source": [
    "### 1. Init for exact match cache\n",
    "\n",
    "`cache.init` is used to initialize gptcache, the default is to use map to search for cached data, `pre_embedding_func` is used to pre-process the data inserted into the cache, and it will use the `get_input_str` method, more configuration refer to [initialize Cache](https://gptcache.readthedocs.io/en/latest/references/gptcache.html#module-gptcache.Cache)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cd436f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from gptcache import cache\n",
    "# from gptcache.processor.pre import get_input_str\n",
    "# # init gptcache\n",
    "# cache.init(pre_embedding_func=get_input_str)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f88b92d6",
   "metadata": {},
   "source": [
    "### 2. Init for similar match cache\n",
    "\n",
    "When initializing gptcahe, the following four parameters are configured:\n",
    "\n",
    "- `pre_embedding_func`: \n",
    "\n",
    "   Pre-processing before extracting feature vectors, it will use the `get_input_image_file_name` method.\n",
    "\n",
    "- `embedding_func`:\n",
    "\n",
    "   The method to extract the image feature vector, you can refer to [gptcache.embedding](https://gptcache.readthedocs.io/en/latest/references/embedding.html) for options of image embedding methods.\n",
    "\n",
    "- `data_manager`:\n",
    "\n",
    "   DataManager for cache management. It is used for image feature vector, question and response answer in the example, it takes [Milvus](https://milvus.io/docs) (please make sure it is started), you can also configure other vector storage, refer to [VectorBase API](https://gptcache.readthedocs.io/en/latest/references/manager.html#module-gptcache.manager.vector_data).\n",
    "\n",
    "- `similarity_evaluation`:\n",
    "\n",
    "   The evaluation method after the cache hit. It evaluates the similarity between the current question and questions of cache hits. In this case, you can select `ExactMatchEvaluation`, `OnnxModelEvaluation`, `NumpyNormEvaluation` from [gptcache.similarity_evaluation](https://gptcache.readthedocs.io/en/latest/references/similarity_evaluation.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5bb28fb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.processor.pre import get_input_image_file_name\n",
    "\n",
    "from gptcache.embedding import Timm\n",
    "from gptcache.similarity_evaluation.onnx import OnnxModelEvaluation\n",
    "from gptcache.manager import get_data_manager, CacheBase, VectorBase\n",
    "\n",
    "\n",
    "timm = Timm()\n",
    "cache_base = CacheBase('sqlite')\n",
    "vector_base = VectorBase('milvus', host='localhost', port='19530', dimension=timm.dimension)\n",
    "data_manager = get_data_manager(cache_base, vector_base)\n",
    "\n",
    "cache.init(\n",
    "    pre_embedding_func=get_input_image_file_name,\n",
    "    embedding_func=timm.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=OnnxModelEvaluation(),\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "266888ef",
   "metadata": {},
   "source": [
    "## Run replicate blip\n",
    "\n",
    "Then run `replicate.run`, which will use blip model to answer free-form questions about images in natural language.\n",
    "\n",
    "Note that `replicate` here is imported from `gptcache.adapter.replicate`, which can be used to cache with gptcache at request time. Please download the [merlion.png](https://github.com/salesforce/LAVIS/raw/main/docs/_static/merlion.png) before running the following code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a4e83890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "singapore\n"
     ]
    }
   ],
   "source": [
    "from gptcache.adapter import replicate\n",
    "\n",
    "output = replicate.run(\n",
    "            \"andreasjansson/blip-2:4b32258c42e9efd4288bb9910bc532a69727f9acd26aa08e175713a0a857a608\",\n",
    "            input={\"image\": open(\"./merlion.png\", \"rb\"),\n",
    "                   \"question\": \"Which city is this photo taken?\"}\n",
    "        )\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15f7734a",
   "metadata": {},
   "source": [
    "## Start with gradio\n",
    "\n",
    "Finally, we can start a gradio application to answer the questions about images. First define the `vqa` method, then start the service with gradio, as shown below:\n",
    "\n",
    "![](../assets/vqa.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "319fcb7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def vqa(img, question):\n",
    "    output = replicate.run(\n",
    "            \"andreasjansson/blip-2:4b32258c42e9efd4288bb9910bc532a69727f9acd26aa08e175713a0a857a608\",\n",
    "            input={\"image\": open(img, \"rb\"),\n",
    "                   \"question\": question}\n",
    "        )\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65b9134a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio\n",
    "\n",
    "interface = gradio.Interface(vqa, \n",
    "                             [gradio.Image(source=\"upload\", type=\"filepath\"), gradio.Textbox(label=\"Question\")],\n",
    "                             gradio.Textbox(label=\"Answer\")\n",
    "                            )\n",
    "\n",
    "interface.launch(inline=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-audio/.streamlit/config.toml
================================================
[theme]
base="dark"


================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-audio/README.md
================================================
# GPTCache Demo for OpenAI Audio Transcription
This project demonstrates how [GPTcache](https://github.com/zilliztech/GPTCache) can be used to save costs when using OpenAI’s audio transcription API. It provides a simple Streamlit app that allows users to input an audio file and see the corresponding transcribed text. The app uses a cache to store previously generated transcriptions and reuses them for the same audio file, thus avoiding making duplicate API calls.

## Requirements
* Python 3.6 or later 
* Dependencies listed in requirements.txt 
* OpenAI API key
## Usage
1. Clone the repository to your local machine
Install the required packages: pip install -r requirements.txt
2. Run the app: streamlit run audio.py
3. Open the app in your browser at http://localhost:8501
4. Enter your OpenAI API key and upload an audio file to transcribe, then click “generate” to wait for the transcribed text to appear.
If a cache hit occurred, you should see a message like “cache” at the bottom of the transcribed text.

<p align="center">
  <img src="./example.png" alt="example"/>
</p>





================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-audio/audio.py
================================================
import streamlit as st
import os
import uuid

from gptcache import cache
from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase
from gptcache.adapter import openai
from gptcache.processor.pre import get_file_name
from gptcache.embedding import Data2VecAudio
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation


@st.cache_resource
def initialize_configuration():
    data2vec = Data2VecAudio()
    data_manager = get_data_manager(CacheBase('sqlite', sql_url='sqlite:///./local/gptcache20.db'),
                                    VectorBase('faiss', dimension=data2vec.dimension, index_path='./local/faiss20.index'),
                                    ObjectBase('local', path='./local'))
    cache.init(
        pre_embedding_func=get_file_name,
        embedding_func=data2vec.to_embeddings,
        data_manager=data_manager,
        similarity_evaluation=SearchDistanceEvaluation(),
        )
    return data_manager

data_manager = initialize_configuration()

def api_call(audio_bytes, open_ai_key):
    os.environ['OPENAI_API_KEY'] =  open_ai_key
    os.environ['CURL_CA_BUNDLE'] = ''
    os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
    cache.set_openai_key()
    transcript = openai.Audio.transcribe('whisper-1', audio_bytes, api_key=open_ai_key)
    is_cached = transcript.get('gptcache', False)
    return transcript['text'], is_cached

def main():

    st.title('GPTCache for Audio Demo')
    open_ai_key = st.text_input('OpenAI key')
    audio_file = st.file_uploader('Choose an audio file (.mp3, .wav, or .ogg)', type=['mp3', 'wav', 'ogg'])

    if st.button('generate', key='button'):
        file_extension = os.path.splitext(audio_file.name)[1]  # Get the extension of the uploaded file
        random_filename = str(uuid.uuid4()) + file_extension
        with open(random_filename, 'wb') as f:
            f.write(audio_file.getbuffer())
        audio_file_handler = open(random_filename, 'rb')
        text, is_cached = api_call(audio_file_handler, open_ai_key)
        st.write(text)
        os.remove(random_filename)

        if is_cached:
            st.markdown('<div style="display: flex; align-items: center; justify-content: center; \
                    background-color: green; padding: 10px; color: white; font-weight: bold; border-radius: \
                    5px; margin: 10px auto; max-width: 100px;">cache</div>', unsafe_allow_html=True)

if __name__ == '__main__':
    main()


================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-audio/requirements.txt
================================================
gptcache
pillow
streamlit
torch
faiss-cpu
torchaudio
transformers
sqlalchemy


================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-image/README.md
================================================
# GPTCache Demo for OpenAI Image Generation
This project demonstrates how [GPTcache](https://github.com/zilliztech/GPTCache) can be used to save costs when using OpenAI’s DALL-E API. It provides a simple Streamlit app that allows users to input a prompt and see the corresponding DALL-E output image. The app uses a cache to store previously generated images and reuses them for the same prompt, thus avoiding making duplicate API calls. There is an online [demo](https://gptcache-openai-image.streamlit.app/) hosted for preview.

## Requirements
* Python 3.6 or later 
* Dependencies listed in requirements.txt 
* OpenAI API key
## Usage
1. Clone the repository to your local machine
Install the required packages: pip install -r requirements.txt
2. Run the app: streamlit run imagen.py
3. Open the app in your browser at http://localhost:8501
4. Enter your OpenAI key and prompt then click “generate” to
wait for the DALL-E output image to appear.
If a cache hit occurred, you should see a message like “cache” at the bottom of the image.

<p align="center">
  <img src="./example.png" alt="example"/>
</p>





================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-image/imagen.py
================================================
import streamlit as st
from PIL import Image
import os
import io
import base64
from io import BytesIO
import requests

from gptcache import cache
from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase
from gptcache.adapter import openai
from gptcache.processor.pre import get_prompt
from gptcache.embedding import Onnx
from gptcache.similarity_evaluation import ExactMatchEvaluation

st.title('GPTCache for Image Demo')

@st.cache_resource
def initialize_configuration():
    onnx = Onnx()
    data_manager = get_data_manager(CacheBase('sqlite', sql_url='sqlite:///./local/gptcache10.db'),
                                    VectorBase('faiss', dimension=onnx.dimension, index_path='./local/faiss10.index'),
                                    ObjectBase('local', path='./local'))
    cache.init(
        pre_embedding_func=get_prompt,
        embedding_func=onnx.to_embeddings,
        data_manager=data_manager,
        similarity_evaluation=ExactMatchEvaluation(),
        )
    return data_manager

data_manager = initialize_configuration()

def api_call(text_input, open_ai_key):
    os.environ['CURL_CA_BUNDLE'] = ''
    response = openai.Image.create(
      prompt=text_input,
      n=1,
      size='256x256',
      api_key=open_ai_key
    )
    image_url = response['data'][0]['url']

    is_cached = response.get('gptcache', False)
    if is_cached is False:
        response = requests.get(image_url)
        img = Image.open(BytesIO(response.content))
    else:
        img = Image.open(image_url)
    return img, is_cached

def main():
    open_ai_key = st.text_input('OpenAI key:')
    text_input = st.text_input('prompt:')

    if st.button('generate', key='button'):
        try:
            image, is_cached = api_call(text_input, open_ai_key)
            width, height = image.size
            desired_width = 500
            desired_height = int(height * desired_width / width)
            resized_image = image.resize((desired_width, desired_height))
            img_bytes = io.BytesIO()
            resized_image.save(img_bytes, format='PNG')
            img_str = base64.b64encode(img_bytes.getvalue()).decode()

            st.markdown(
                f'<div style="display: flex; justify-content: center;"><img src="data:image/png;base64,{img_str}" \
                        alt="Uploaded Image" width="{desired_width}"></div>',
                unsafe_allow_html=True
            )

            if is_cached:
                st.markdown('<div style="display: flex; align-items: center; justify-content: center; background-color: \
                        green; padding: 10px; color: white; font-weight: bold; border-radius: 5px; margin: 10px auto; \
                        max-width: 100px;">cache</div>', unsafe_allow_html=True)
        except Exception as e:
            st.error('invalid OpenAI API key or inappropriate prompt rejected by OpenAI.')

if __name__ == '__main__':
    main()


================================================
FILE: docs/bootcamp/streamlit/gptcache-streamlit-image/requirements.txt
================================================
gptcache
pillow
streamlit
onnxruntime
faiss-cpu
transformers
sqlalchemy


================================================
FILE: docs/bootcamp/temperature/chat.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82e45532",
   "metadata": {},
   "source": [
    "# OpenAI Chat with Temperature\n",
    "\n",
    "Temperature in deep learning is a parameter usually used to adjust the probability distribution of the predicted outputs. It is also known as softmax temperature or softmax scaling. In simple terms, it controls the level of confidence that a neural network has in its predictions. It helps to **increase the diversity of the model's outputs**.\n",
    "\n",
    "For temperature in OpenAI chat request, \"higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic\" as explained in [OpenAI do
Download .txt
gitextract_233_9gzq/

├── .github/
│   ├── ISSUE_TEMPLATE/
│   │   ├── bug_report.yaml
│   │   ├── documentation_improvement.yaml
│   │   ├── enhancement.yaml
│   │   └── feature_request.yaml
│   ├── mergify.yml
│   └── workflows/
│       ├── Nightly_CI_main.yaml
│       ├── build_dev_python_package.yaml
│       ├── publish_dev_package.yaml
│       ├── publish_release_image.yaml
│       ├── publish_release_package.yaml
│       ├── pylint.yaml
│       └── unit_test_main.yaml
├── .gitignore
├── LICENSE
├── MANIFEST.in
├── Makefile
├── OWNERS
├── README.md
├── cache_config_template.yml
├── codecov.yml
├── docs/
│   ├── .readthedocs.yaml
│   ├── Makefile
│   ├── _exts/
│   │   ├── docgen2.py
│   │   └── index_con.py
│   ├── _templates/
│   │   ├── author.html
│   │   ├── copyright.html
│   │   ├── function.rst
│   │   └── index.rst
│   ├── bootcamp/
│   │   ├── langchain/
│   │   │   ├── baby_agi.ipynb
│   │   │   ├── index.rst
│   │   │   ├── qa_generation.ipynb
│   │   │   ├── question_answering.ipynb
│   │   │   └── sqlite.ipynb
│   │   ├── llama_index/
│   │   │   ├── index.rst
│   │   │   └── webpage_qa.ipynb
│   │   ├── openai/
│   │   │   ├── chat.ipynb
│   │   │   ├── image_generation.ipynb
│   │   │   ├── index.rst
│   │   │   ├── language_translate.ipynb
│   │   │   ├── speech_to_text.ipynb
│   │   │   ├── sql_translate.ipynb
│   │   │   └── tweet_classifier.ipynb
│   │   ├── replicate/
│   │   │   ├── index.rst
│   │   │   └── visual_question_answering.ipynb
│   │   ├── streamlit/
│   │   │   ├── gptcache-streamlit-audio/
│   │   │   │   ├── .streamlit/
│   │   │   │   │   └── config.toml
│   │   │   │   ├── README.md
│   │   │   │   ├── audio.py
│   │   │   │   └── requirements.txt
│   │   │   └── gptcache-streamlit-image/
│   │   │       ├── README.md
│   │   │       ├── imagen.py
│   │   │       └── requirements.txt
│   │   ├── temperature/
│   │   │   ├── chat.ipynb
│   │   │   ├── create_image.ipynb
│   │   │   └── index.rst
│   │   └── vertex/
│   │       ├── index.rst
│   │       └── vertexai_caching.ipynb
│   ├── conf.py
│   ├── configure_it.md
│   ├── contributing.md
│   ├── feature.md
│   ├── horizontal-scaling-usage.md
│   ├── index.rst
│   ├── make.bat
│   ├── references/
│   │   └── index.rst
│   ├── release_note.md
│   ├── requirements.txt
│   ├── toc.bak
│   └── usage.md
├── examples/
│   ├── README.md
│   ├── adapter/
│   │   ├── api.py
│   │   ├── langchain_llms.py
│   │   └── openai_chatgpt.py
│   ├── benchmark/
│   │   ├── benchmark_sqlite_faiss_onnx.py
│   │   └── mock_data.json
│   ├── context_process/
│   │   ├── selective_context.py
│   │   └── summarization_context.py
│   ├── data_manager/
│   │   ├── map_manager.py
│   │   ├── scalar_store.py
│   │   └── vector_store.py
│   ├── embedding/
│   │   ├── default.py
│   │   ├── onnx.py
│   │   ├── paddlenlp.py
│   │   └── random.py
│   ├── eviction/
│   │   └── distributed_eviction.py
│   ├── integrate/
│   │   ├── diffusers/
│   │   │   └── stable_diffusion.py
│   │   ├── dolly/
│   │   │   └── basic_usage.py
│   │   ├── langchain/
│   │   │   ├── langchain_llms_mock.py
│   │   │   ├── langchain_prompt_openai.py
│   │   │   ├── langchain_qa_chain.py
│   │   │   └── langchain_similaritycache_openai.py
│   │   ├── llama_cpp/
│   │   │   └── basic_usage.py
│   │   ├── openai/
│   │   │   ├── basic_usage.py
│   │   │   ├── create_image.py
│   │   │   ├── qa.py
│   │   │   ├── readme.py
│   │   │   └── summarize.py
│   │   ├── replicate/
│   │   │   └── vqa.py
│   │   └── stability/
│   │       └── text_to_image.py
│   ├── processor/
│   │   ├── llm_verifier_example.py
│   │   └── temperature_example.py
│   ├── session/
│   │   └── session.py
│   ├── similarity_evaluation/
│   │   ├── exact_match.py
│   │   ├── onnx.py
│   │   ├── search_distance.py
│   │   └── sequence_match.py
│   └── vqa_demo.py
├── gptcache/
│   ├── __init__.py
│   ├── adapter/
│   │   ├── __init__.py
│   │   ├── adapter.py
│   │   ├── api.py
│   │   ├── base.py
│   │   ├── diffusers.py
│   │   ├── dolly.py
│   │   ├── langchain_models.py
│   │   ├── llama_cpp.py
│   │   ├── minigpt4.py
│   │   ├── openai.py
│   │   ├── replicate.py
│   │   └── stability_sdk.py
│   ├── client.py
│   ├── config.py
│   ├── core.py
│   ├── embedding/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── cohere.py
│   │   ├── data2vec.py
│   │   ├── fasttext.py
│   │   ├── huggingface.py
│   │   ├── langchain.py
│   │   ├── onnx.py
│   │   ├── openai.py
│   │   ├── paddlenlp.py
│   │   ├── rwkv.py
│   │   ├── sbert.py
│   │   ├── string.py
│   │   ├── timm.py
│   │   ├── uform.py
│   │   └── vit.py
│   ├── manager/
│   │   ├── __init__.py
│   │   ├── data_manager.py
│   │   ├── eviction/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── distributed_cache.py
│   │   │   ├── manager.py
│   │   │   ├── memory_cache.py
│   │   │   └── redis_eviction.py
│   │   ├── eviction_manager.py
│   │   ├── factory.py
│   │   ├── object_data/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── local_storage.py
│   │   │   ├── manager.py
│   │   │   └── s3_storage.py
│   │   ├── scalar_data/
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── dynamo_storage.py
│   │   │   ├── manager.py
│   │   │   ├── mongo.py
│   │   │   ├── redis_storage.py
│   │   │   └── sql_storage.py
│   │   └── vector_data/
│   │       ├── __init__.py
│   │       ├── base.py
│   │       ├── chroma.py
│   │       ├── docarray_index.py
│   │       ├── faiss.py
│   │       ├── hnswlib_store.py
│   │       ├── manager.py
│   │       ├── milvus.py
│   │       ├── pgvector.py
│   │       ├── qdrant.py
│   │       ├── redis_vectorstore.py
│   │       ├── usearch.py
│   │       └── weaviate.py
│   ├── processor/
│   │   ├── __init__.py
│   │   ├── check_hit.py
│   │   ├── context/
│   │   │   ├── __init__.py
│   │   │   ├── concat_context.py
│   │   │   ├── context.py
│   │   │   ├── selective_context.py
│   │   │   └── summarization_context.py
│   │   ├── post.py
│   │   └── pre.py
│   ├── report.py
│   ├── session.py
│   ├── similarity_evaluation/
│   │   ├── __init__.py
│   │   ├── cohere_rerank.py
│   │   ├── distance.py
│   │   ├── exact_match.py
│   │   ├── kreciprocal.py
│   │   ├── np.py
│   │   ├── onnx.py
│   │   ├── sbert_crossencoder.py
│   │   ├── sequence_match.py
│   │   ├── similarity_evaluation.py
│   │   └── time.py
│   └── utils/
│       ├── __init__.py
│       ├── cache_func.py
│       ├── dependency_control.py
│       ├── error.py
│       ├── lazy_import.py
│       ├── log.py
│       ├── response.py
│       ├── softmax.py
│       ├── time.py
│       └── token.py
├── gptcache_server/
│   ├── __init__.py
│   ├── dockerfiles/
│   │   └── Dockerfile
│   └── server.py
├── pylint.conf
├── requirements.txt
├── scripts/
│   ├── manage_conda_env.sh
│   └── remove_example_cache.sh
├── setup.py
└── tests/
    ├── integration_tests/
    │   ├── base/
    │   │   └── client_base.py
    │   ├── common/
    │   │   ├── common_func.py
    │   │   └── common_type.py
    │   ├── config/
    │   │   └── log_config.py
    │   ├── examples/
    │   │   ├── map/
    │   │   │   └── test_example_map.py
    │   │   ├── sqlite_faiss_mock/
    │   │   │   └── test_example_sqlite_faiss.py
    │   │   └── sqlite_faiss_onnx/
    │   │       └── test_example_sqlite_faiss_onnx.py
    │   ├── processor/
    │   │   └── pre/
    │   │       └── test_pre_without_prompt.py
    │   ├── test_redis_onnx.py
    │   ├── test_sqlite_faiss_onnx.py
    │   ├── test_sqlite_milvus_sbert.py
    │   └── utils/
    │       └── util_log.py
    ├── pytest.ini
    ├── requirements.txt
    └── unit_tests/
        ├── adapter/
        │   ├── test_adapter.py
        │   ├── test_api.py
        │   ├── test_diffusers.py
        │   ├── test_dolly.py
        │   ├── test_langchain_models.py
        │   ├── test_llama_cpp.py
        │   ├── test_openai.py
        │   ├── test_replicate.py
        │   └── test_stability.py
        ├── embedding/
        │   ├── test_cohere.py
        │   ├── test_data2vec.py
        │   ├── test_embedding_openai.py
        │   ├── test_embedding_string.py
        │   ├── test_fasttext.py
        │   ├── test_huggingface.py
        │   ├── test_langchain.py
        │   ├── test_onnx.py
        │   ├── test_paddlenlp.py
        │   ├── test_rwkv.py
        │   ├── test_sbert.py
        │   ├── test_timm.py
        │   ├── test_uform.py
        │   └── test_vit.py
        ├── eviction/
        │   ├── test_distributed_cache.py
        │   └── test_memory_cache.py
        ├── manager/
        │   ├── test_base.py
        │   ├── test_chromadb.py
        │   ├── test_dynamo_storage.py
        │   ├── test_eviction.py
        │   ├── test_factory.py
        │   ├── test_local_index.py
        │   ├── test_map.py
        │   ├── test_milvusdb.py
        │   ├── test_mongo.py
        │   ├── test_object_storage.py
        │   ├── test_pgvector.py
        │   ├── test_qdrant.py
        │   ├── test_redis.py
        │   ├── test_redis_cache_storage.py
        │   ├── test_sql_scalar.py
        │   ├── test_usearch.py
        │   └── test_weaviate.py
        ├── processor/
        │   ├── test_concat_context.py
        │   ├── test_context.py
        │   ├── test_post.py
        │   ├── test_pre.py
        │   ├── test_selective_context.py
        │   └── test_summarize_context.py
        ├── similarity_evaluation/
        │   ├── test_cohere_rerank.py
        │   ├── test_evaluation_kreciprocal.py
        │   ├── test_evaluation_onnx.py
        │   ├── test_evaluation_sbert.py
        │   ├── test_evaluation_sequence.py
        │   ├── test_evaluation_string.py
        │   ├── test_evalution_time.py
        │   ├── test_np.py
        │   └── test_simple.py
        ├── test_client.py
        ├── test_core.py
        ├── test_session.py
        └── utils/
            ├── test_error.py
            ├── test_log.py
            └── test_response.py
Download .txt
SYMBOL INDEX (1038 symbols across 197 files)

FILE: docs/_exts/docgen2.py
  class DocGen (line 13) | class DocGen:
    method __init__ (line 14) | def __init__(
    method title_bar (line 27) | def title_bar(input_str):
    method section_bar (line 31) | def section_bar(input_str):
    method get_filename (line 35) | def get_filename(input_str):
    method cap (line 47) | def cap(input_str):
    method model_name (line 53) | def model_name(self, input_str: str):
    method get_module_and_libs (line 56) | def get_module_and_libs(self, module_dir, is_root):
    method generate (line 79) | def generate(self):

FILE: docs/_exts/index_con.py
  class IndexCon (line 4) | class IndexCon:
    method __init__ (line 6) | def __init__(self, source, output):
    method preprocess (line 11) | def preprocess(self):

FILE: docs/bootcamp/streamlit/gptcache-streamlit-audio/audio.py
  function initialize_configuration (line 14) | def initialize_configuration():
  function api_call (line 29) | def api_call(audio_bytes, open_ai_key):
  function main (line 38) | def main():

FILE: docs/bootcamp/streamlit/gptcache-streamlit-image/imagen.py
  function initialize_configuration (line 19) | def initialize_configuration():
  function api_call (line 34) | def api_call(text_input, open_ai_key):
  function main (line 52) | def main():

FILE: examples/adapter/api.py
  function run_basic (line 7) | def run_basic():
  function run_similar_match (line 14) | def run_similar_match():

FILE: examples/adapter/langchain_llms.py
  function run_llm (line 18) | def run_llm():
  function get_msg (line 37) | def get_msg(data, **_):
  function run_chat_model (line 41) | def run_chat_model():

FILE: examples/benchmark/benchmark_sqlite_faiss_onnx.py
  function run (line 13) | def run():

FILE: examples/context_process/selective_context.py
  function response_text (line 15) | def response_text(openai_resp):
  function cache_init (line 23) | def cache_init():
  function base_request (line 40) | def base_request():

FILE: examples/context_process/summarization_context.py
  function response_text (line 12) | def response_text(openai_resp):
  function cache_init (line 16) | def cache_init():
  function base_request (line 33) | def base_request():

FILE: examples/data_manager/map_manager.py
  function run (line 8) | def run():

FILE: examples/data_manager/scalar_store.py
  function mock_embeddings (line 13) | def mock_embeddings(data, **kwargs):
  function run (line 17) | def run():

FILE: examples/data_manager/vector_store.py
  function mock_embeddings (line 11) | def mock_embeddings(data, **kwargs):
  function run (line 15) | def run():

FILE: examples/embedding/default.py
  function run (line 6) | def run():

FILE: examples/embedding/onnx.py
  function run (line 9) | def run():

FILE: examples/embedding/paddlenlp.py
  function run (line 9) | def run():

FILE: examples/embedding/random.py
  function mock_embeddings (line 11) | def mock_embeddings(data, **kwargs):
  function run (line 15) | def run():

FILE: examples/eviction/distributed_eviction.py
  function get_data_manager_example (line 9) | def get_data_manager_example():
  function get_manager_example_redis_only (line 33) | def get_manager_example_redis_only():
  function manager_factory_example (line 57) | def manager_factory_example():

FILE: examples/integrate/dolly/basic_usage.py
  function dolly_base_usage (line 11) | def dolly_base_usage():
  function dolly_from_hugggingface (line 34) | def dolly_from_hugggingface():

FILE: examples/integrate/langchain/langchain_llms_mock.py
  function run (line 14) | def run():

FILE: examples/integrate/langchain/langchain_qa_chain.py
  function get_content_func (line 12) | def get_content_func(data, **_):

FILE: examples/integrate/llama_cpp/basic_usage.py
  function llama_cpp_base_usage (line 10) | def llama_cpp_base_usage():
  function llama_cpp_stream_usage (line 28) | def llama_cpp_stream_usage():

FILE: examples/integrate/openai/basic_usage.py
  function response_text (line 11) | def response_text(openai_resp):
  function cache_init (line 15) | def cache_init():
  function base_request (line 22) | def base_request():
  function stream_request (line 39) | def stream_request():
  function similar_request (line 67) | def similar_request():

FILE: examples/integrate/openai/readme.py
  function response_text (line 7) | def response_text(openai_resp):

FILE: examples/session/session.py
  function run_session (line 10) | def run_session():
  function run_custom_session (line 25) | def run_custom_session():

FILE: examples/similarity_evaluation/exact_match.py
  function run (line 6) | def run():

FILE: examples/similarity_evaluation/onnx.py
  function run (line 8) | def run():

FILE: examples/similarity_evaluation/search_distance.py
  function run (line 8) | def run():

FILE: examples/similarity_evaluation/sequence_match.py
  function run (line 10) | def run():

FILE: examples/vqa_demo.py
  function parse_args (line 22) | def parse_args():

FILE: gptcache/adapter/adapter.py
  function adapt (line 12) | def adapt(llm_handler, cache_data_convert, update_cache_callback, *args,...
  function aadapt (line 289) | async def aadapt(
  function _summarize_input (line 546) | def _summarize_input(text, text_length):
  function cache_health_check (line 562) | def cache_health_check(vectordb, cache_dict):

FILE: gptcache/adapter/api.py
  function _cache_data_converter (line 47) | def _cache_data_converter(cache_data):
  function _update_cache_callback_none (line 52) | def _update_cache_callback_none(
  function _llm_handle_none (line 59) | def _llm_handle_none(*llm_args, **llm_kwargs) -> None:  # pylint: disabl...
  function _update_cache_callback (line 64) | def _update_cache_callback(
  function put (line 71) | def put(prompt: str, data: Any, **kwargs) -> None:
  function get (line 105) | def get(prompt: str, **kwargs) -> Any:
  function init_similar_cache (line 134) | def init_similar_cache(
  function init_similar_cache_from_config (line 194) | def init_similar_cache_from_config(config_dir: str, cache_obj: Optional[...
  function _get_model (line 260) | def _get_model(model_src, model_config=None):
  function _get_eval (line 290) | def _get_eval(strategy, kws=None):
  function _get_pre_func (line 314) | def _get_pre_func(pre_process):
  function _get_pre_context_function (line 318) | def _get_pre_context_function(pre_context_process, kws=None):
  function _get_post_func (line 329) | def _get_post_func(post_process):

FILE: gptcache/adapter/base.py
  class BaseCacheLLM (line 5) | class BaseCacheLLM(metaclass=ABCMeta):
    method fill_base_args (line 63) | def fill_base_args(cls, **kwargs):

FILE: gptcache/adapter/diffusers.py
  class StableDiffusionPipeline (line 19) | class StableDiffusionPipeline(diffusers.StableDiffusionPipeline):
    method _llm_handler (line 44) | def _llm_handler(self, *llm_args, **llm_kwargs):
    method __call__ (line 50) | def __call__(self, *args, **kwargs):
  function _construct_resp_from_cache (line 67) | def _construct_resp_from_cache(img_64):

FILE: gptcache/adapter/dolly.py
  class Dolly (line 13) | class Dolly:
    method __init__ (line 44) | def __init__(self, dolly_pipeline: Any):
    method from_model (line 48) | def from_model(cls, model: str, **kwargs):
    method __call__ (line 52) | def __call__(self, prompt: str, **kwargs):
  function _cache_data_convert (line 62) | def _cache_data_convert(cache_data):
  function _update_cache_callback (line 66) | def _update_cache_callback(llm_data, update_cache_func, *args, **kwargs)...

FILE: gptcache/adapter/langchain_models.py
  class LangChainLLMs (line 28) | class LangChainLLMs(LLM):
    method _llm_type (line 55) | def _llm_type(self) -> str:
    method _identifying_params (line 59) | def _identifying_params(self) -> Mapping[str, Any]:
    method __str__ (line 62) | def __str__(self) -> str:
    method _call (line 65) | def _call(
    method _acall (line 88) | async def _acall(self, prompt: str, stop: Optional[List[str]] = None,
    method generate (line 92) | def generate(
    method agenerate (line 102) | async def agenerate(
    method __call__ (line 112) | def __call__(
  class LangChainChat (line 128) | class LangChainChat(BaseChatModel):
    method _llm_type (line 150) | def _llm_type(self) -> str:
    method _generate (line 157) | def _generate(
    method _agenerate (line 181) | async def _agenerate(
    method generate (line 205) | def generate(
    method agenerate (line 215) | async def agenerate(
    method _identifying_params (line 226) | def _identifying_params(self):
    method _combine_llm_outputs (line 229) | def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> d...
    method get_num_tokens (line 232) | def get_num_tokens(self, text: str) -> int:
    method get_num_tokens_from_messages (line 235) | def get_num_tokens_from_messages(self, messages: List[BaseMessage]) ->...
    method __call__ (line 238) | def __call__(self, messages: Any, stop: Optional[List[str]] = None, **...
  function _cache_data_convert (line 246) | def _cache_data_convert(cache_data):
  function _update_cache_callback (line 250) | def _update_cache_callback(
  function _cache_msg_data_convert (line 257) | def _cache_msg_data_convert(cache_data):
  function _update_cache_msg_callback (line 271) | def _update_cache_msg_callback(

FILE: gptcache/adapter/llama_cpp.py
  class Llama (line 13) | class Llama(llama_cpp.Llama):
    method __call__ (line 33) | def __call__(
  function _construct_resp_from_cache (line 67) | def _construct_resp_from_cache(return_message):
  function _construct_stream_resp_from_cache (line 83) | def _construct_stream_resp_from_cache(return_message):

FILE: gptcache/adapter/minigpt4.py
  class MiniGPT4 (line 19) | class MiniGPT4:  # pragma: no cover
    method __init__ (line 38) | def __init__(self, chat, return_hit):
    method from_pretrained (line 43) | def from_pretrained(cls, cfg_path, gpu_id=0, options=None,  return_hit...
    method _llm_handler (line 56) | def _llm_handler(self, image, question):
    method __call__ (line 67) | def __call__(self, image, question, *args, **kwargs):

FILE: gptcache/adapter/openai.py
  class ChatCompletion (line 31) | class ChatCompletion(openai.ChatCompletion, BaseCacheLLM):
    method _llm_handler (line 57) | def _llm_handler(cls, *llm_args, **llm_kwargs):
    method _allm_handler (line 68) | async def _allm_handler(cls, *llm_args, **llm_kwargs):
    method _update_cache_callback (line 79) | def _update_cache_callback(
    method create (line 109) | def create(cls, *args, **kwargs):
    method acreate (line 134) | async def acreate(cls, *args, **kwargs):
  function async_iter (line 161) | async def async_iter(input_list):
  class Completion (line 166) | class Completion(openai.Completion, BaseCacheLLM):
    method _llm_handler (line 186) | def _llm_handler(cls, *llm_args, **llm_kwargs):
    method _allm_handler (line 197) | async def _allm_handler(cls, *llm_args, **llm_kwargs):
    method _cache_data_convert (line 208) | def _cache_data_convert(cache_data):
    method _update_cache_callback (line 212) | def _update_cache_callback(
    method create (line 219) | def create(cls, *args, **kwargs):
    method acreate (line 230) | async def acreate(cls, *args, **kwargs):
  class Audio (line 241) | class Audio(openai.Audio):
    method transcribe (line 264) | def transcribe(cls, model: str, file: Any, *args, **kwargs):
    method translate (line 293) | def translate(cls, model: str, file: Any, *args, **kwargs):
  class Image (line 322) | class Image(openai.Image):
    method _llm_handler (line 345) | def _llm_handler(cls, *llm_args, **llm_kwargs):
    method create (line 352) | def create(cls, *args, **kwargs):
  class Moderation (line 386) | class Moderation(openai.Moderation, BaseCacheLLM):
    method _llm_handler (line 403) | def _llm_handler(cls, *llm_args, **llm_kwargs):
    method _cache_data_convert (line 414) | def _cache_data_convert(cls, cache_data):
    method _update_cache_callback (line 418) | def _update_cache_callback(
    method create (line 425) | def create(cls, *args, **kwargs):
  function _construct_resp_from_cache (line 451) | def _construct_resp_from_cache(return_message, saved_token):
  function _construct_stream_resp_from_cache (line 468) | def _construct_stream_resp_from_cache(return_message, saved_token):
  function _construct_text_from_cache (line 499) | def _construct_text_from_cache(return_text):
  function _construct_image_create_resp_from_cache (line 515) | def _construct_image_create_resp_from_cache(image_data, response_format,...
  function _construct_audio_text_from_cache (line 549) | def _construct_audio_text_from_cache(return_text):
  function _num_tokens_from_messages (line 556) | def _num_tokens_from_messages(messages):

FILE: gptcache/adapter/replicate.py
  class Client (line 11) | class Client(replicate.client.Client):
    method run (line 44) | def run(self, model_version: str, **kwargs):

FILE: gptcache/adapter/stability_sdk.py
  class StabilityInference (line 22) | class StabilityInference(client.StabilityInference):
    method _llm_handler (line 61) | def _llm_handler(self, *llm_args, **llm_kwargs):
    method generate (line 67) | def generate(self, *args, **kwargs):
  function _construct_resp_from_cache (line 100) | def _construct_resp_from_cache(img_64, height, width):
  class MockArtifact (line 114) | class MockArtifact:
  class MockAnswer (line 120) | class MockAnswer:

FILE: gptcache/client.py
  class Client (line 14) | class Client:
    method __init__ (line 30) | def __init__(self, uri: str = "http://localhost:8000"):
    method _put (line 33) | async def _put(self, question: str, answer: str):
    method _get (line 46) | async def _get(self, question: str):
    method put (line 58) | def put(self, question: str, answer: str):
    method get (line 68) | def get(self, question: str):

FILE: gptcache/config.py
  class Config (line 6) | class Config:
    method __init__ (line 37) | def __init__(

FILE: gptcache/core.py
  class Cache (line 19) | class Cache:
    method __init__ (line 34) | def __init__(self):
    method init (line 46) | def init(
    method import_data (line 90) | def import_data(self, questions: List[Any], answers: List[Any], sessio...
    method flush (line 106) | def flush(self):
    method set_openai_key (line 115) | def set_openai_key():
    method set_azure_openai_key (line 122) | def set_azure_openai_key():

FILE: gptcache/embedding/__init__.py
  function Cohere (line 35) | def Cohere(model="large", api_key=None):
  function OpenAI (line 39) | def OpenAI(model="text-embedding-ada-002", api_key=None):
  function Huggingface (line 43) | def Huggingface(model="distilbert-base-uncased"):
  function SBERT (line 47) | def SBERT(model="all-MiniLM-L6-v2"):
  function Onnx (line 51) | def Onnx(model="GPTCache/paraphrase-albert-onnx"):
  function FastText (line 55) | def FastText(model="en", dim=None):
  function Data2VecAudio (line 59) | def Data2VecAudio(model="facebook/data2vec-audio-base-960h"):
  function Timm (line 63) | def Timm(model="resnet50", device="default"):
  function ViT (line 67) | def ViT(model="google/vit-base-patch16-384"):
  function LangChain (line 71) | def LangChain(embeddings, dimension=0):
  function Rwkv (line 75) | def Rwkv(model="sgugger/rwkv-430M-pile"):
  function PaddleNLP (line 79) | def PaddleNLP(model="ernie-3.0-medium-zh"):
  function UForm (line 83) | def UForm(model="unum-cloud/uform-vl-multilingual", embedding_type="text"):

FILE: gptcache/embedding/base.py
  class BaseEmbedding (line 4) | class BaseEmbedding(metaclass=ABCMeta):
    method to_embeddings (line 10) | def to_embeddings(self, data, **kwargs):
    method dimension (line 15) | def dimension(self) -> int:

FILE: gptcache/embedding/cohere.py
  class Cohere (line 11) | class Cohere(BaseEmbedding):
    method __init__ (line 29) | def __init__(self, model: str = "large", api_key: str = None):
    method to_embeddings (line 38) | def to_embeddings(self, data, **_):
    method dimension (line 53) | def dimension(self):
    method dim_dict (line 64) | def dim_dict():

FILE: gptcache/embedding/data2vec.py
  class Data2VecAudio (line 15) | class Data2VecAudio(BaseEmbedding):
    method __init__ (line 30) | def __init__(self, model_name = "facebook/data2vec-audio-base-960h"):
    method to_embeddings (line 36) | def to_embeddings(self, data, **_):
    method load_audio (line 52) | def load_audio(self, audio_path, target_sr):
    method dimension (line 61) | def dimension(self):

FILE: gptcache/embedding/fasttext.py
  class FastText (line 12) | class FastText(BaseEmbedding):
    method __init__ (line 30) | def __init__(self, model: str = "en", dim: int = None):
    method to_embeddings (line 38) | def to_embeddings(self, data, **_):
    method dimension (line 51) | def dimension(self):

FILE: gptcache/embedding/huggingface.py
  class Huggingface (line 13) | class Huggingface(BaseEmbedding):
    method __init__ (line 33) | def __init__(self, model: str = "distilbert-base-uncased"):
    method to_embeddings (line 48) | def to_embeddings(self, data, **_):
    method post_proc (line 65) | def post_proc(self, token_embeddings, inputs):
    method dimension (line 76) | def dimension(self):

FILE: gptcache/embedding/langchain.py
  class LangChain (line 11) | class LangChain(BaseEmbedding):
    method __init__ (line 32) | def __init__(self, embeddings: Embeddings, dimension: int = 0):
    method to_embeddings (line 38) | def to_embeddings(self, data, **kwargs):
    method dimension (line 43) | def dimension(self) -> int:

FILE: gptcache/embedding/onnx.py
  class Onnx (line 19) | class Onnx(BaseEmbedding):
    method __init__ (line 32) | def __init__(self, model="GPTCache/paraphrase-albert-onnx"):
    method to_embeddings (line 43) | def to_embeddings(self, data, **_):
    method post_proc (line 64) | def post_proc(self, token_embeddings, attention_mask):
    method dimension (line 76) | def dimension(self):

FILE: gptcache/embedding/openai.py
  class OpenAI (line 12) | class OpenAI(BaseEmbedding):
    method __init__ (line 30) | def __init__(self, model: str = "text-embedding-ada-002", api_key: str...
    method to_embeddings (line 49) | def to_embeddings(self, data, **_):
    method dimension (line 61) | def dimension(self):
    method dim_dict (line 72) | def dim_dict():

FILE: gptcache/embedding/paddlenlp.py
  class PaddleNLP (line 13) | class PaddleNLP(BaseEmbedding):
    method __init__ (line 29) | def __init__(self, model: str = "ernie-3.0-medium-zh"):
    method to_embeddings (line 38) | def to_embeddings(self, data, **_):
    method post_proc (line 55) | def post_proc(self, token_embeddings, inputs):
    method dimension (line 66) | def dimension(self):

FILE: gptcache/embedding/rwkv.py
  class Rwkv (line 11) | class Rwkv(BaseEmbedding):
    method __init__ (line 28) | def __init__(self, model: str = "sgugger/rwkv-430M-pile"):
    method to_embeddings (line 41) | def to_embeddings(self, data, **_):
    method dimension (line 55) | def dimension(self):

FILE: gptcache/embedding/sbert.py
  class SBERT (line 10) | class SBERT(BaseEmbedding):
    method __init__ (line 26) | def __init__(self, model: str = "all-MiniLM-L6-v2"):
    method to_embeddings (line 31) | def to_embeddings(self, data, **_):
    method dimension (line 48) | def dimension(self):

FILE: gptcache/embedding/string.py
  function to_embeddings (line 1) | def to_embeddings(data, **_):

FILE: gptcache/embedding/timm.py
  class Timm (line 16) | class Timm(BaseEmbedding):
    method __init__ (line 34) | def __init__(self, model: str = "resnet18", device: str = "default"):
    method to_embeddings (line 48) | def to_embeddings(self, data, skip_preprocess: bool = False, **_):
    method post_proc (line 67) | def post_proc(self, features):
    method preprocess (line 78) | def preprocess(self, image_path):
    method dimension (line 96) | def dimension(self):

FILE: gptcache/embedding/uform.py
  class UForm (line 14) | class UForm(BaseEmbedding):
    method __init__ (line 36) | def __init__(self, model: Union[str, TritonClient] = "unum-cloud/uform...
    method to_embeddings (line 49) | def to_embeddings(self, data: Any, **_):
    method dimension (line 67) | def dimension(self):

FILE: gptcache/embedding/vit.py
  class ViT (line 13) | class ViT(BaseEmbedding):
    method __init__ (line 39) | def __init__(self, model: str = "google/vit-base-patch16-384"):
    method to_embeddings (line 47) | def to_embeddings(self, data, **__):
    method preprocess (line 65) | def preprocess(self, data):
    method dimension (line 71) | def dimension(self):

FILE: gptcache/manager/data_manager.py
  class DataManager (line 25) | class DataManager(metaclass=ABCMeta):
    method save (line 29) | def save(self, question, answer, embedding_data, **kwargs):
    method import_data (line 33) | def import_data(
    method get_scalar_data (line 43) | def get_scalar_data(self, res_data, **kwargs) -> CacheData:
    method hit_cache_callback (line 46) | def hit_cache_callback(self, res_data, **kwargs):
    method search (line 50) | def search(self, embedding_data, **kwargs):
    method flush (line 57) | def flush(self):
    method add_session (line 61) | def add_session(self, res_data, session_id, pre_embedding_data):
    method list_sessions (line 65) | def list_sessions(self, session_id, key):
    method delete_session (line 69) | def delete_session(self, session_id):
    method report_cache (line 72) | def report_cache(
    method close (line 84) | def close(self):
  class MapDataManager (line 88) | class MapDataManager(DataManager):
    method __init__ (line 107) | def __init__(self, data_path, max_size, get_data_container=None):
    method init (line 115) | def init(self):
    method save (line 126) | def save(self, question, answer, embedding_data, **kwargs):
    method import_data (line 133) | def import_data(
    method get_scalar_data (line 154) | def get_scalar_data(self, res_data, **kwargs) -> CacheData:
    method search (line 166) | def search(self, embedding_data, **kwargs):
    method flush (line 172) | def flush(self):
    method add_session (line 181) | def add_session(self, res_data, session_id, pre_embedding_data):
    method list_sessions (line 184) | def list_sessions(self, session_id=None, key=None):
    method delete_session (line 193) | def delete_session(self, session_id):
    method close (line 200) | def close(self):
  function normalize (line 204) | def normalize(vec):
  class SSDataManager (line 210) | class SSDataManager(DataManager):
    method __init__ (line 223) | def __init__(
    method _clear (line 249) | def _clear(self, marked_keys):
    method save (line 254) | def save(self, question, answer, embedding_data, **kwargs):
    method _process_answer_data (line 277) | def _process_answer_data(self, answers: Union[Answer, List[Answer]]):
    method _process_question_data (line 288) | def _process_question_data(self, question: Union[str, Question]):
    method import_data (line 300) | def import_data(
    method get_scalar_data (line 340) | def get_scalar_data(self, res_data, **kwargs) -> Optional[CacheData]:
    method hit_cache_callback (line 366) | def hit_cache_callback(self, res_data, **kwargs):
    method search (line 369) | def search(self, embedding_data, **kwargs):
    method flush (line 374) | def flush(self):
    method add_session (line 378) | def add_session(self, res_data, session_id, pre_embedding_data):
    method list_sessions (line 381) | def list_sessions(self, session_id=None, key=None):
    method delete_session (line 389) | def delete_session(self, session_id):
    method report_cache (line 393) | def report_cache(
    method close (line 411) | def close(self):

FILE: gptcache/manager/eviction/__init__.py
  function EvictionBase (line 10) | def EvictionBase(name: str, **kwargs):

FILE: gptcache/manager/eviction/base.py
  class EvictionBase (line 5) | class EvictionBase(metaclass=ABCMeta):
    method put (line 11) | def put(self, objs: List[Any]):
    method get (line 15) | def get(self, obj: Any):
    method policy (line 20) | def policy(self) -> str:

FILE: gptcache/manager/eviction/distributed_cache.py
  class DistributedEviction (line 8) | class DistributedEviction(EvictionBase, ABC):
    method put (line 14) | def put(self, objs: List[str]):
    method get (line 18) | def get(self, obj: str):
    method policy (line 23) | def policy(self) -> str:
  class NoOpEviction (line 27) | class NoOpEviction(EvictionBase):
    method policy (line 34) | def policy(self) -> str:
    method __init__ (line 37) | def __init__(self, **kwargs):
    method put (line 40) | def put(self, objs: List[str]):
    method get (line 43) | def get(self, obj: str):

FILE: gptcache/manager/eviction/manager.py
  class EvictionBase (line 7) | class EvictionBase:
    method __init__ (line 12) | def __init__(self):
    method get (line 19) | def get(

FILE: gptcache/manager/eviction/memory_cache.py
  function popitem_wrapper (line 8) | def popitem_wrapper(func, wrapper_func, clean_size):
  class MemoryCacheEviction (line 20) | class MemoryCacheEviction(EvictionBase):
    method __init__ (line 35) | def __init__(
    method put (line 57) | def put(self, objs: List[Any]):
    method get (line 61) | def get(self, obj: Any):
    method policy (line 65) | def policy(self) -> str:

FILE: gptcache/manager/eviction/redis_eviction.py
  class RedisCacheEviction (line 14) | class RedisCacheEviction(DistributedEviction, ABC):
    method __init__ (line 40) | def __init__(self,
    method _create_key (line 61) | def _create_key(self, key: str) -> str:
    method put (line 64) | def put(self, objs: List[str], expire=False):
    method get (line 69) | def get(self, obj: str):
    method policy (line 82) | def policy(self) -> str:

FILE: gptcache/manager/eviction_manager.py
  class EvictionManager (line 1) | class EvictionManager:
    method __init__ (line 16) | def __init__(self, scalar_storage, vector_base):
    method check_evict (line 21) | def check_evict(self):
    method delete (line 31) | def delete(self):
    method rebuild (line 39) | def rebuild(self):
    method soft_evict (line 45) | def soft_evict(self, marked_keys):

FILE: gptcache/manager/factory.py
  function manager_factory (line 11) | def manager_factory(manager="map",
  function get_data_manager (line 134) | def get_data_manager(

FILE: gptcache/manager/object_data/__init__.py
  function ObjectBase (line 10) | def ObjectBase(name: str, **kwargs):

FILE: gptcache/manager/object_data/base.py
  class ObjectBase (line 5) | class ObjectBase(ABC):
    method put (line 11) | def put(self, obj: Any) -> str:
    method get (line 15) | def get(self, obj: str) -> Any:
    method get_access_link (line 19) | def get_access_link(self, obj: str) -> str:
    method delete (line 23) | def delete(self, to_delete: List[str]):

FILE: gptcache/manager/object_data/local_storage.py
  class LocalObjectStorage (line 9) | class LocalObjectStorage(ObjectBase):
    method __init__ (line 13) | def __init__(self, local_root: str):
    method put (line 17) | def put(self, obj: Any) -> str:
    method get (line 23) | def get(self, obj: str) -> Any:
    method get_access_link (line 30) | def get_access_link(self, obj: str, _: int = 3600):
    method delete (line 33) | def delete(self, to_delete: List[str]):

FILE: gptcache/manager/object_data/manager.py
  class ObjectBase (line 4) | class ObjectBase:
    method __init__ (line 35) | def __init__(self):
    method get (line 41) | def get(name, **kwargs):

FILE: gptcache/manager/object_data/s3_storage.py
  class S3Storage (line 14) | class S3Storage(ObjectBase):
    method __init__ (line 18) | def __init__(self, bucket: str, path_prefix: str, access_key: str, sec...
    method put (line 28) | def put(self, obj: Any) -> str:
    method get (line 33) | def get(self, obj: str) -> Any:
    method get_access_link (line 40) | def get_access_link(self, obj: str, expires: int = 3600) -> str:
    method delete (line 54) | def delete(self, to_delete: List[str]):

FILE: gptcache/manager/scalar_data/__init__.py
  function CacheBase (line 10) | def CacheBase(name: str, **kwargs):

FILE: gptcache/manager/scalar_data/base.py
  class DataType (line 10) | class DataType(IntEnum):
  class Answer (line 17) | class Answer:
  class QuestionDep (line 29) | class QuestionDep:
    method from_dict (line 39) | def from_dict(cls, d: Dict):
  class Question (line 44) | class Question:
    method from_dict (line 53) | def from_dict(cls, d: Dict):
  class CacheData (line 61) | class CacheData:
    method __init__ (line 73) | def __init__(
  class CacheStorage (line 99) | class CacheStorage(metaclass=ABCMeta):
    method create (line 105) | def create(self):
    method batch_insert (line 109) | def batch_insert(self, all_data: List[CacheData]):
    method get_data_by_id (line 113) | def get_data_by_id(self, key):
    method mark_deleted (line 117) | def mark_deleted(self, keys):
    method clear_deleted_data (line 121) | def clear_deleted_data(self):
    method get_ids (line 125) | def get_ids(self, deleted=True):
    method count (line 129) | def count(self, state: int = 0, is_all: bool = False):
    method flush (line 132) | def flush(self):
    method add_session (line 136) | def add_session(self, question_id, session_id, session_question):
    method list_sessions (line 140) | def list_sessions(self, session_id, key):
    method delete_session (line 144) | def delete_session(self, keys):
    method report_cache (line 148) | def report_cache(
    method close (line 160) | def close(self):

FILE: gptcache/manager/scalar_data/dynamo_storage.py
  class DynamoStorage (line 18) | class DynamoStorage(CacheStorage):
    method __init__ (line 41) | def __init__(
    method create (line 61) | def create(self):
    method batch_insert (line 110) | def batch_insert(self, all_data: List[CacheData]) -> List[str]:
    method get_data_by_id (line 151) | def get_data_by_id(self, key: str) -> Optional[CacheData]:
    method mark_deleted (line 183) | def mark_deleted(self, keys: str):
    method clear_deleted_data (line 197) | def clear_deleted_data(self):
    method get_ids (line 239) | def get_ids(self, deleted: bool = True) -> List[str]:
    method count (line 266) | def count(self, state: int = 0, is_all: bool = False) -> int:
    method add_session (line 296) | def add_session(self, question_id: str, session_id: str, session_quest...
    method list_sessions (line 308) | def list_sessions(self, session_id = None, key = None) -> List[str]:
    method delete_session (line 353) | def delete_session(self, keys: List[str]):
    method report_cache (line 389) | def report_cache(
    method close (line 418) | def close(self):
    method _does_table_already_exist_and_is_active (line 421) | def _does_table_already_exist_and_is_active(self, table_name: str) -> ...
    method _wait_until_table_exists (line 428) | def _wait_until_table_exists(self, table_name: str):
    method _response_item_to_cache_data (line 437) | def _response_item_to_cache_data(self, question_resp: Dict) -> Optiona...
    method _fetch_all_pages (line 450) | def _fetch_all_pages(self, scan_fn) -> List[Dict]:
    method _question_text (line 474) | def _question_text(self, question) -> str:
    method _generate_id (line 477) | def _generate_id(self):
    method _strip_all_props_with_none_values (line 489) | def _strip_all_props_with_none_values(self, obj: Dict) -> Dict:
    method _serialize_deleted_value (line 496) | def _serialize_deleted_value(self, value: bool, suffix_value = randint...
    method _serialize_answer (line 507) | def _serialize_answer(self, answer: Answer) -> Dict:
    method _serialize_question_deps (line 510) | def _serialize_question_deps(self, dep: QuestionDep) -> Dict:
    method _deserialize_deleted_value (line 513) | def _deserialize_deleted_value(self, value: str) -> bool:
    method _deserialize_question_dep (line 516) | def _deserialize_question_dep(self, dep: Dict) -> QuestionDep:
    method _deserialize_answer (line 519) | def _deserialize_answer(self, answer: Dict) -> Answer:

FILE: gptcache/manager/scalar_data/manager.py
  class CacheBase (line 17) | class CacheBase:
    method __init__ (line 58) | def __init__(self):
    method get (line 64) | def get(name, **kwargs):

FILE: gptcache/manager/scalar_data/mongo.py
  function get_models (line 22) | def get_models():
  class MongoStorage (line 112) | class MongoStorage(CacheStorage):
    method __init__ (line 159) | def __init__(
    method create (line 184) | def create(self):
    method _insert (line 187) | def _insert(self, data: CacheData):
    method batch_insert (line 233) | def batch_insert(self, all_data: List[CacheData]):
    method get_data_by_id (line 239) | def get_data_by_id(self, key) -> Optional[CacheData]:
    method mark_deleted (line 263) | def mark_deleted(self, keys):
    method clear_deleted_data (line 266) | def clear_deleted_data(self):
    method get_ids (line 274) | def get_ids(self, deleted: bool = True):
    method count (line 279) | def count(self, state: int = 0, is_all: bool = False):
    method add_session (line 284) | def add_session(self, question_id, session_id, session_question):
    method list_sessions (line 291) | def list_sessions(self, session_id=None, key=None):
    method delete_session (line 300) | def delete_session(self, keys):
    method count_answers (line 303) | def count_answers(self):
    method report_cache (line 306) | def report_cache(
    method close (line 325) | def close(self):

FILE: gptcache/manager/scalar_data/redis_storage.py
  function get_models (line 23) | def get_models(global_key: str, redis_connection: Redis):
  class RedisCacheStorage (line 159) | class RedisCacheStorage(CacheStorage):
    method __init__ (line 201) | def __init__(
    method init_eviction_params (line 226) | def init_eviction_params(self, policy, maxmemory, maxmemory_samples, t...
    method create (line 235) | def create(self):
    method _insert (line 238) | def _insert(self, data: CacheData, pipeline: Pipeline = None):
    method batch_insert (line 293) | def batch_insert(self, all_data: List[CacheData]):
    method get_data_by_id (line 301) | def get_data_by_id(self, key: str):
    method mark_deleted (line 329) | def mark_deleted(self, keys):
    method clear_deleted_data (line 334) | def clear_deleted_data(self):
    method get_ids (line 347) | def get_ids(self, deleted=True):
    method count (line 354) | def count(self, state: int = 0, is_all: bool = False):
    method add_session (line 359) | def add_session(self, question_id, session_id, session_question):
    method list_sessions (line 366) | def list_sessions(self, session_id=None, key=None):
    method delete_session (line 379) | def delete_session(self, keys: List[str]):
    method report_cache (line 388) | def report_cache(self, user_question, cache_question, cache_question_i...
    method close (line 400) | def close(self):

FILE: gptcache/manager/scalar_data/sql_storage.py
  function _get_table_len (line 38) | def _get_table_len(config: Dict, column_alias: str) -> int:
  function get_models (line 44) | def get_models(table_prefix, db_type, table_len_config):
  class SQLStorage (line 181) | class SQLStorage(CacheStorage):
    method __init__ (line 201) | def __init__(
    method create (line 218) | def create(self):
    method _insert (line 225) | def _insert(self, data: CacheData, session: sqlalchemy.orm.Session) ->...
    method batch_insert (line 269) | def batch_insert(self, all_data: List[CacheData]):
    method get_data_by_id (line 277) | def get_data_by_id(self, key: int) -> Optional[CacheData]:
    method get_ids (line 324) | def get_ids(self, deleted=True):
    method mark_deleted (line 330) | def mark_deleted(self, keys):
    method clear_deleted_data (line 337) | def clear_deleted_data(self):
    method count (line 353) | def count(self, state: int = 0, is_all: bool = False):
    method add_session (line 363) | def add_session(self, question_id, session_id, session_question):
    method delete_session (line 373) | def delete_session(self, keys):
    method list_sessions (line 378) | def list_sessions(self, session_id=None, key=None):
    method report_cache (line 387) | def report_cache(self, user_question, cache_question, cache_question_i...
    method close (line 400) | def close(self):
    method count_answers (line 403) | def count_answers(self):

FILE: gptcache/manager/vector_data/__init__.py
  function VectorBase (line 10) | def VectorBase(name: str, **kwargs):

FILE: gptcache/manager/vector_data/base.py
  class VectorData (line 9) | class VectorData:
  class VectorBase (line 14) | class VectorBase(ABC):
    method mul_add (line 18) | def mul_add(self, datas: List[VectorData]):
    method search (line 22) | def search(self, data: np.ndarray, top_k: int):
    method rebuild (line 26) | def rebuild(self, ids=None) -> bool:
    method delete (line 30) | def delete(self, ids) -> bool:
    method flush (line 33) | def flush(self):
    method close (line 36) | def close(self):
    method get_embeddings (line 39) | def get_embeddings(self, data_id: Union[int, str]) -> Optional[np.ndar...
    method update_embeddings (line 42) | def update_embeddings(self, data_id: Union[int, str], emb: np.ndarray):

FILE: gptcache/manager/vector_data/chroma.py
  class Chromadb (line 14) | class Chromadb(VectorBase):
    method __init__ (line 28) | def __init__(
    method mul_add (line 49) | def mul_add(self, datas: List[VectorData]):
    method search (line 53) | def search(self, data, top_k: int = -1):
    method delete (line 65) | def delete(self, ids):
    method rebuild (line 68) | def rebuild(self, ids=None):  # pylint: disable=unused-argument
    method get_embeddings (line 71) | def get_embeddings(self, data_id: str):
    method update_embeddings (line 81) | def update_embeddings(self, data_id: str, emb: np.ndarray):

FILE: gptcache/manager/vector_data/docarray_index.py
  class DocarrayVectorData (line 15) | class DocarrayVectorData(BaseDoc):
  class DocArrayIndex (line 22) | class DocArrayIndex(VectorBase):
    method __init__ (line 32) | def __init__(self, index_file_path: str, top_k: int):
    method mul_add (line 39) | def mul_add(self, datas: List[VectorData]) -> None:
    method search (line 50) | def search(
    method rebuild (line 68) | def rebuild(self, ids: Optional[List[int]] = None) -> bool:
    method delete (line 74) | def delete(self, ids: Optional[List[str]]) -> None:
    method flush (line 83) | def flush(self) -> None:
    method close (line 86) | def close(self) -> None:

FILE: gptcache/manager/vector_data/faiss.py
  class Faiss (line 14) | class Faiss(VectorBase):
    method __init__ (line 25) | def __init__(self, index_file_path, dimension, top_k):
    method mul_add (line 33) | def mul_add(self, datas: List[VectorData]):
    method search (line 39) | def search(self, data: np.ndarray, top_k: int = -1):
    method rebuild (line 49) | def rebuild(self, ids=None):
    method delete (line 52) | def delete(self, ids):
    method flush (line 56) | def flush(self):
    method close (line 59) | def close(self):
    method count (line 62) | def count(self):

FILE: gptcache/manager/vector_data/hnswlib_store.py
  class Hnswlib (line 14) | class Hnswlib(VectorBase):
    method __init__ (line 27) | def __init__(self, index_file_path: str, dimension: int, top_k: int, m...
    method add (line 39) | def add(self, key: int, data: np.ndarray):
    method mul_add (line 43) | def mul_add(self, datas: List[VectorData]):
    method search (line 49) | def search(self, data: np.ndarray, top_k: int = -1):
    method rebuild (line 56) | def rebuild(self, ids):
    method delete (line 67) | def delete(self, ids):
    method flush (line 71) | def flush(self):
    method close (line 74) | def close(self):

FILE: gptcache/manager/vector_data/manager.py
  class VectorBase (line 35) | class VectorBase:
    method __init__ (line 98) | def __init__(self):
    method check_dimension (line 104) | def check_dimension(dimension):
    method get (line 111) | def get(name, **kwargs):

FILE: gptcache/manager/vector_data/milvus.py
  class Milvus (line 23) | class Milvus(VectorBase):
    method __init__ (line 64) | def __init__(
    method _create_local (line 96) | def _create_local(self, port, local_data):
    method _connect (line 105) | def _connect(self, host, port, user, password, secure):
    method _create_collection (line 125) | def _create_collection(self, collection_name):
    method mul_add (line 166) | def mul_add(self, datas: List[VectorData]):
    method search (line 172) | def search(self, data: np.ndarray, top_k: int = -1):
    method delete (line 183) | def delete(self, ids):
    method rebuild (line 187) | def rebuild(self, ids=None):  # pylint: disable=unused-argument
    method flush (line 190) | def flush(self):
    method close (line 193) | def close(self):
    method get_embeddings (line 198) | def get_embeddings(self, data_id: int):
    method update_embeddings (line 208) | def update_embeddings(self, data_id: int, emb: np.ndarray):

FILE: gptcache/manager/vector_data/pgvector.py
  class _VectorType (line 22) | class _VectorType(UserDefinedType):
    method __init__ (line 28) | def __init__(self, precision=8):
    method get_col_spec (line 31) | def get_col_spec(self, **_):
    method bind_processor (line 35) | def bind_processor(self, dialect):
    method result_processor (line 39) | def result_processor(self, dialect, coltype):
  function _get_model_and_index (line 43) | def _get_model_and_index(table_prefix, vector_dimension, index_type, lis...
  class PGVector (line 66) | class PGVector(VectorBase):
    method __init__ (line 87) | def __init__(
    method _connect (line 112) | def _connect(self, url):
    method _create_collection (line 116) | def _create_collection(self):
    method _query (line 123) | def _query(self, session):
    method _format_data_for_search (line 126) | def _format_data_for_search(self, data):
    method mul_add (line 129) | def mul_add(self, datas: List[VectorData]):
    method search (line 138) | def search(self, data: np.ndarray, top_k: int = -1):
    method delete (line 156) | def delete(self, ids):
    method rebuild (line 161) | def rebuild(self, ids=None):  # pylint: disable=unused-argument
    method flush (line 166) | def flush(self):
    method close (line 170) | def close(self):

FILE: gptcache/manager/vector_data/qdrant.py
  class QdrantVectorStore (line 22) | class QdrantVectorStore(VectorBase):
    method __init__ (line 25) | def __init__(
    method _create_local (line 60) | def _create_local(self, location):
    method _create_remote (line 63) | def _create_remote(
    method _create_collection (line 78) | def _create_collection(
    method mul_add (line 109) | def mul_add(self, datas: List[VectorData]):
    method search (line 117) | def search(self, data: np.ndarray, top_k: int = -1):
    method delete (line 128) | def delete(self, ids: List[str]):
    method rebuild (line 131) | def rebuild(self, ids=None):  # pylint: disable=unused-argument
    method flush (line 139) | def flush(self):
    method close (line 143) | def close(self):

FILE: gptcache/manager/vector_data/redis_vectorstore.py
  class RedisVectorStore (line 18) | class RedisVectorStore(VectorBase):
    method __init__ (line 43) | def __init__(
    method _check_index_exists (line 64) | def _check_index_exists(self, index_name: str) -> bool:
    method _create_collection (line 74) | def _create_collection(self, collection_name):
    method mul_add (line 101) | def mul_add(self, datas: List[VectorData]):
    method search (line 113) | def search(self, data: np.ndarray, top_k: int = -1):
    method rebuild (line 131) | def rebuild(self, ids=None) -> bool:
    method delete (line 134) | def delete(self, ids) -> None:

FILE: gptcache/manager/vector_data/usearch.py
  class USearch (line 15) | class USearch(VectorBase):
    method __init__ (line 36) | def __init__(
    method mul_add (line 61) | def mul_add(self, datas: List[VectorData]):
    method search (line 67) | def search(self, data: np.ndarray, top_k: int = -1):
    method rebuild (line 74) | def rebuild(self, ids=None):
    method delete (line 77) | def delete(self, ids):
    method flush (line 80) | def flush(self):
    method close (line 83) | def close(self):
    method count (line 86) | def count(self):

FILE: gptcache/manager/vector_data/weaviate.py
  class Weaviate (line 17) | class Weaviate(VectorBase):
    method __init__ (line 24) | def __init__(
    method _create_class (line 65) | def _create_class(self):
    method _get_default_class_schema (line 75) | def _get_default_class_schema(self) -> dict:
    method mul_add (line 89) | def mul_add(self, datas: List[VectorData]):
    method search (line 100) | def search(self, data: np.ndarray, top_k: int = -1):
    method _get_uuids (line 119) | def _get_uuids(self, data_ids):
    method delete (line 140) | def delete(self, ids):
    method rebuild (line 146) | def rebuild(self, ids=None):
    method flush (line 149) | def flush(self):
    method close (line 152) | def close(self):
    method get_embeddings (line 155) | def get_embeddings(self, data_id: int):
    method update_embeddings (line 178) | def update_embeddings(self, data_id: int, emb: np.ndarray):

FILE: gptcache/processor/check_hit.py
  function check_hit_session (line 2) | def check_hit_session(cur_session_id: str, cache_session_ids: list, cach...

FILE: gptcache/processor/context/__init__.py
  function SummarizationContextProcess (line 23) | def SummarizationContextProcess(model_name=None, tokenizer=None, target_...
  function SelectiveContextProcess (line 29) | def SelectiveContextProcess(
  function ConcatContextProcess (line 43) | def ConcatContextProcess():

FILE: gptcache/processor/context/concat_context.py
  class ConcatContextProcess (line 6) | class ConcatContextProcess(ContextProcess):
    method __init__ (line 31) | def __init__(
    method format_all_content (line 37) | def format_all_content(self, data: Dict[str, Any], **params: Dict[str,...
    method process_all_content (line 42) | def process_all_content(self) -> (Any, Any):

FILE: gptcache/processor/context/context.py
  class ContextProcess (line 5) | class ContextProcess(metaclass=ABCMeta):
    method format_all_content (line 19) | def format_all_content(self, data: Dict[str, Any], **params: Dict[str,...
    method process_all_content (line 28) | def process_all_content(self) -> (Any, Any):
    method pre_process (line 34) | def pre_process(self, data: Dict[str, Any], **params: Dict[str, Any]) ...

FILE: gptcache/processor/context/selective_context.py
  class SelectiveContextProcess (line 11) | class SelectiveContextProcess(ContextProcess):
    method __init__ (line 40) | def __init__(
    method format_all_content (line 51) | def format_all_content(self, data: Dict[str, Any], **params: Dict[str,...
    method process_all_content (line 55) | def process_all_content(self) -> (Any, Any):

FILE: gptcache/processor/context/summarization_context.py
  function summarize_to_length (line 13) | def summarize_to_length(summarizer, text, target_len, max_len=1024):
  class SummarizationContextProcess (line 39) | class SummarizationContextProcess(ContextProcess):
    method __init__ (line 59) | def __init__(
    method summarize_to_sentence (line 70) | def summarize_to_sentence(self, sentences, target_size=1000):
    method format_all_content (line 91) | def format_all_content(self, data: Dict[str, Any], **params: Dict[str,...
    method process_all_content (line 97) | def process_all_content(self) -> (Any, Any):

FILE: gptcache/processor/post.py
  function random_one (line 9) | def random_one(messages: List[Any]) -> Any:
  function first (line 26) | def first(messages: List[Any]) -> Any:
  function nop (line 44) | def nop(messages: List[Any]) -> Any:
  function temperature_softmax (line 62) | def temperature_softmax(messages: List[Any], scores: List[float], temper...
  function llm_semantic_verification (line 93) | def llm_semantic_verification(
  class LlmVerifier (line 173) | class LlmVerifier:
    method __init__ (line 185) | def __init__(self, client=None, system_prompt=None, model="gpt-3.5-tur...
    method __call__ (line 190) | def __call__(self, messages, scores=None, original_question=None, **kw...

FILE: gptcache/processor/pre.py
  function last_content (line 6) | def last_content(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
  function last_content_without_prompt (line 23) | def last_content_without_prompt(data: Dict[str, Any], **params: Dict[str...
  function _get_pattern_value (line 51) | def _get_pattern_value(pattern_str: str, value_str: str):
  function last_content_without_template (line 77) | def last_content_without_template(data: Dict[str, Any], **params: Dict[s...
  function all_content (line 138) | def all_content(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
  function nop (line 164) | def nop(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
  function get_prompt (line 181) | def get_prompt(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
  function get_file_name (line 198) | def get_file_name(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
  function get_file_bytes (line 216) | def get_file_bytes(data: Dict[str, Any], **_: Dict[str, Any]) -> bytes:
  function get_input_str (line 232) | def get_input_str(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
  function get_input_image_file_name (line 249) | def get_input_image_file_name(data: Dict[str, Any], **_: Dict[str, Any])...
  function get_image_question (line 267) | def get_image_question(data: Dict[str, Any], **_: Dict[str, Any]) -> str...
  function get_image (line 285) | def get_image(data: Dict[str, Any], **_: Dict[str, Any]) -> str:  # prag...
  function get_inputs (line 302) | def get_inputs(data: Dict[str, Any], **_: Dict[str, Any]):
  function get_messages_last_content (line 319) | def get_messages_last_content(data: Dict[str, Any], **_: Any) -> str:
  function get_openai_moderation_input (line 336) | def get_openai_moderation_input(data: Dict[str, Any], **_: Dict[str, Any...
  function concat_all_queries (line 354) | def concat_all_queries(data: Dict[str, Any], **params: Dict[str, Any]) -...

FILE: gptcache/report.py
  class Report (line 1) | class Report:
    method __init__ (line 4) | def __init__(self):
    method pre (line 15) | def pre(self, delta_time):
    method embedding (line 23) | def embedding(self, delta_time):
    method search (line 31) | def search(self, delta_time):
    method data (line 39) | def data(self, delta_time):
    method evaluation (line 48) | def evaluation(self, delta_time):
    method post (line 56) | def post(self, delta_time):
    method llm (line 64) | def llm(self, delta_time):
    method save (line 72) | def save(self, delta_time):
    method average_pre_time (line 80) | def average_pre_time(self):
    method average_embedding_time (line 84) | def average_embedding_time(self):
    method average_search_time (line 88) | def average_search_time(self):
    method average_data_time (line 92) | def average_data_time(self):
    method average_evaluation_time (line 96) | def average_evaluation_time(self):
    method average_post_time (line 100) | def average_post_time(self):
    method average_llm_time (line 104) | def average_llm_time(self):
    method average_save_time (line 108) | def average_save_time(self):
    method hint_cache (line 112) | def hint_cache(self):
  class OpCounter (line 117) | class OpCounter:
    method average (line 125) | def average(self):

FILE: gptcache/session.py
  class Session (line 10) | class Session:
    method __init__ (line 48) | def __init__(
    method name (line 61) | def name(self):
    method __enter__ (line 64) | def __enter__(self):
    method __exit__ (line 70) | def __exit__(self, *_):
    method drop (line 73) | def drop(self):

FILE: gptcache/similarity_evaluation/__init__.py
  function OnnxModelEvaluation (line 47) | def OnnxModelEvaluation(model="GPTCache/albert-duplicate-onnx"):
  function NumpyNormEvaluation (line 51) | def NumpyNormEvaluation(enable_normal: bool = False, **kwargs):
  function SearchDistanceEvaluation (line 55) | def SearchDistanceEvaluation(max_distance=4.0, positive=False):
  function ExactMatchEvaluation (line 59) | def ExactMatchEvaluation():
  function KReciprocalEvaluation (line 63) | def KReciprocalEvaluation(vectordb, top_k=3, max_distance=4.0, positive=...
  function CohereRerankEvaluation (line 67) | def CohereRerankEvaluation(model: str = "rerank-english-v2.0", api_key: ...
  function SequenceMatchEvaluation (line 71) | def SequenceMatchEvaluation(weights, embedding_extractor, embedding_conf...
  function TimeEvaluation (line 75) | def TimeEvaluation(evaluation: str, evaluation_config: Dict[str, Any], t...
  function SbertCrossencoderEvaluation (line 78) | def SbertCrossencoderEvaluation(model: str = "cross-encoder/quora-distil...

FILE: gptcache/similarity_evaluation/cohere_rerank.py
  class CohereRerank (line 11) | class CohereRerank(SimilarityEvaluation):
    method __init__ (line 37) | def __init__(self, model: str = "rerank-english-v2.0", api_key: str = ...
    method evaluation (line 41) | def evaluation(self, src_dict: Dict[str, Any], cache_dict: Dict[str, A...
    method range (line 52) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/distance.py
  class SearchDistanceEvaluation (line 6) | class SearchDistanceEvaluation(SimilarityEvaluation):
    method __init__ (line 34) | def __init__(self, max_distance=4.0, positive=False):
    method evaluation (line 38) | def evaluation(
    method range (line 58) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/exact_match.py
  class ExactMatchEvaluation (line 6) | class ExactMatchEvaluation(SimilarityEvaluation):
    method __init__ (line 28) | def __init__(self):
    method evaluation (line 31) | def evaluation(
    method range (line 45) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/kreciprocal.py
  function euclidean_distance_calculate (line 8) | def euclidean_distance_calculate(vec_l: np.array, vec_r: np.array):
  class KReciprocalEvaluation (line 11) | class KReciprocalEvaluation(SearchDistanceEvaluation):
    method __init__ (line 57) | def __init__(self, vectordb: VectorBase, top_k: int = 3, max_distance:...
    method normalize (line 63) | def normalize(vec: np.ndarray):
    method evaluation (line 76) | def evaluation(

FILE: gptcache/similarity_evaluation/np.py
  class NumpyNormEvaluation (line 8) | class NumpyNormEvaluation(SimilarityEvaluation):
    method __init__ (line 39) | def __init__(self, enable_normal: bool = True, question_embedding_func...
    method normalize (line 44) | def normalize(vec: np.ndarray):
    method evaluation (line 56) | def evaluation(
    method range (line 86) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/onnx.py
  function pad_sequence (line 21) | def pad_sequence(input_ids_list: List[np.ndarray], padding_value: int = 0):
  class OnnxModelEvaluation (line 29) | class OnnxModelEvaluation(SimilarityEvaluation):
    method __init__ (line 53) | def __init__(self, model: str = "GPTCache/albert-duplicate-onnx"):
    method evaluation (line 61) | def evaluation(
    method range (line 82) | def range(self) -> Tuple[float, float]:
    method inference (line 89) | def inference(self, reference: str, candidates: List[str]) -> np.ndarray:

FILE: gptcache/similarity_evaluation/sbert_crossencoder.py
  class SbertCrossencoderEvaluation (line 7) | class SbertCrossencoderEvaluation(SimilarityEvaluation):
    method __init__ (line 31) | def __init__(self, model: str="cross-encoder/quora-distilroberta-base"):
    method evaluation (line 34) | def evaluation(
    method range (line 55) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/sequence_match.py
  function euclidean_distance_calculate (line 9) | def euclidean_distance_calculate(vec_l: np.array, vec_r: np.array):
  function reweight (line 13) | def reweight(weights, length):
  class SequenceMatchEvaluation (line 26) | class SequenceMatchEvaluation(SimilarityEvaluation):
    method __init__ (line 56) | def __init__(
    method normalize (line 63) | def normalize(vec: np.ndarray):
    method evaluation (line 75) | def evaluation(
    method range (line 117) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/similarity_evaluation.py
  class SimilarityEvaluation (line 5) | class SimilarityEvaluation(metaclass=ABCMeta):
    method evaluation (line 22) | def evaluation(
    method range (line 35) | def range(self) -> Tuple[float, float]:

FILE: gptcache/similarity_evaluation/time.py
  class TimeEvaluation (line 8) | class TimeEvaluation(SimilarityEvaluation):
    method __init__ (line 38) | def __init__(self, evaluation: str, evaluation_config=None, time_range...
    method evaluation (line 44) | def evaluation(self, src_dict: Dict[str, Any], cache_dict: Dict[str, A...
    method range (line 53) | def range(self) -> Tuple[float, float]:

FILE: gptcache/utils/__init__.py
  function _check_library (line 54) | def _check_library(libname: str, prompt: bool = True, package: Optional[...
  function import_pymilvus (line 63) | def import_pymilvus():
  function import_milvus_lite (line 67) | def import_milvus_lite():
  function import_qdrant (line 71) | def import_qdrant():
  function import_sbert (line 75) | def import_sbert():
  function import_cohere (line 79) | def import_cohere():
  function import_fasttext (line 83) | def import_fasttext():
  function import_huggingface (line 87) | def import_huggingface():
  function import_uform (line 91) | def import_uform():
  function import_usearch (line 95) | def import_usearch():
  function import_torch (line 99) | def import_torch():
  function import_huggingface_hub (line 103) | def import_huggingface_hub():
  function import_onnxruntime (line 107) | def import_onnxruntime():
  function import_faiss (line 111) | def import_faiss():
  function import_hnswlib (line 115) | def import_hnswlib():
  function import_chromadb (line 119) | def import_chromadb():
  function import_sqlalchemy (line 123) | def import_sqlalchemy():
  function import_postgresql (line 127) | def import_postgresql():
  function import_pymysql (line 131) | def import_pymysql():
  function import_pyodbc (line 137) | def import_pyodbc():
  function import_cxoracle (line 142) | def import_cxoracle():
  function import_duckdb (line 146) | def import_duckdb():
  function import_sql_client (line 151) | def import_sql_client(db_name):
  function import_mongodb (line 164) | def import_mongodb():
  function import_pydantic (line 169) | def import_pydantic():
  function import_langchain (line 173) | def import_langchain():
  function import_pillow (line 177) | def import_pillow():
  function import_boto3 (line 181) | def import_boto3():
  function import_diffusers (line 185) | def import_diffusers():
  function import_torchaudio (line 189) | def import_torchaudio():
  function import_torchvision (line 193) | def import_torchvision():
  function import_timm (line 197) | def import_timm():
  function import_vit (line 201) | def import_vit():
  function import_replicate (line 205) | def import_replicate():
  function import_stability (line 209) | def import_stability():
  function import_scipy (line 213) | def import_scipy():
  function import_llama_cpp_python (line 217) | def import_llama_cpp_python():
  function import_ruamel (line 221) | def import_ruamel():
  function import_selective_context (line 225) | def import_selective_context():
  function import_httpx (line 229) | def import_httpx():
  function import_openai (line 233) | def import_openai():
  function import_docarray (line 237) | def import_docarray():
  function import_paddle (line 241) | def import_paddle():
  function import_paddlenlp (line 246) | def import_paddlenlp():
  function import_tiktoken (line 250) | def import_tiktoken():
  function import_fastapi (line 254) | def import_fastapi():
  function import_redis (line 259) | def import_redis():
  function import_starlette (line 264) | def import_starlette():
  function import_weaviate (line 268) | def import_weaviate():

FILE: gptcache/utils/cache_func.py
  function cache_all (line 1) | def cache_all(*_, **__):

FILE: gptcache/utils/dependency_control.py
  function prompt_install (line 7) | def prompt_install(package: str, warn: bool = False):  # pragma: no cover

FILE: gptcache/utils/error.py
  class CacheError (line 1) | class CacheError(Exception):
  class NotInitError (line 5) | class NotInitError(CacheError):
    method __init__ (line 7) | def __init__(self):
  class NotFoundError (line 11) | class NotFoundError(CacheError):
    method __init__ (line 13) | def __init__(self, store_type, current_type_name):
  class ParamError (line 17) | class ParamError(CacheError):
  class PipInstallError (line 21) | class PipInstallError(CacheError):
    method __init__ (line 23) | def __init__(self, package):
  function wrap_error (line 27) | def wrap_error(e: Exception) -> Exception:

FILE: gptcache/utils/lazy_import.py
  class LazyImport (line 5) | class LazyImport(ModuleType):
    method __init__ (line 10) | def __init__(self, local_name, parent_module_globals, name):
    method _load (line 15) | def _load(self):
    method __getattr__ (line 21) | def __getattr__(self, item):
    method __dir__ (line 25) | def __dir__(self):

FILE: gptcache/utils/response.py
  function get_message_from_openai_answer (line 5) | def get_message_from_openai_answer(openai_resp):
  function get_stream_message_from_openai_answer (line 9) | def get_stream_message_from_openai_answer(openai_data):
  function get_text_from_openai_answer (line 13) | def get_text_from_openai_answer(openai_resp):
  function get_image_from_openai_b64 (line 17) | def get_image_from_openai_b64(openai_resp):
  function get_image_from_openai_url (line 21) | def get_image_from_openai_url(openai_resp):
  function get_image_from_path (line 28) | def get_image_from_path(openai_resp):
  function get_audio_text_from_openai_answer (line 35) | def get_audio_text_from_openai_answer(openai_resp):

FILE: gptcache/utils/softmax.py
  function softmax (line 4) | def softmax(x: list):

FILE: gptcache/utils/time.py
  function time_cal (line 6) | def time_cal(func, func_name=None, report_func=None):

FILE: gptcache/utils/token.py
  function _get_encoding (line 6) | def _get_encoding():
  function token_counter (line 15) | def token_counter(text):

FILE: gptcache_server/server.py
  class CacheData (line 33) | class CacheData(BaseModel):
  function hello (line 39) | async def hello():
  function put_cache (line 44) | async def put_cache(cache_data: CacheData) -> str:
  function get_cache (line 50) | async def get_cache(cache_data: CacheData) -> CacheData:
  function flush_cache (line 56) | async def flush_cache() -> str:
  function get_cache_file (line 62) | async def get_cache_file(key: str = "") -> FileResponse:
  function chat (line 89) | async def chat(request: Request):
  function main (line 144) | def main():

FILE: setup.py
  function parse_requirements (line 16) | def parse_requirements(file_name: str) -> List[str]:
  function read (line 24) | def read(*parts):
  function find_version (line 29) | def find_version(*file_paths):

FILE: tests/integration_tests/base/client_base.py
  class Base (line 8) | class Base:
    method setup_method (line 9) | def setup_method(self, method):
    method teardown_method (line 15) | def teardown_method(self, method):

FILE: tests/integration_tests/common/common_func.py
  function remove_file (line 7) | def remove_file(file_names=[ct.sqlite_file, ct.faiss_file]):
  function log_time_func (line 19) | def log_time_func(func_name, delta_time):
  function disable_cache (line 29) | def disable_cache(*args, **kwargs):

FILE: tests/integration_tests/config/log_config.py
  class LogConfig (line 5) | class LogConfig:
    method __init__ (line 6) | def __init__(self):
    method get_env_variable (line 14) | def get_env_variable(var="CI_LOG_PATH"):
    method create_path (line 29) | def create_path(log_path):
    method get_default_config (line 35) | def get_default_config(self):

FILE: tests/integration_tests/examples/map/test_example_map.py
  function test_map (line 9) | def test_map():

FILE: tests/integration_tests/examples/sqlite_faiss_mock/test_example_sqlite_faiss.py
  function mock_embeddings (line 14) | def mock_embeddings(data, **kwargs):  # pylint: disable=W0613
  function test_sqlite_faiss (line 18) | def test_sqlite_faiss():

FILE: tests/integration_tests/examples/sqlite_faiss_onnx/test_example_sqlite_faiss_onnx.py
  function test_sqlite_faiss_onnx (line 11) | def test_sqlite_faiss_onnx():

FILE: tests/integration_tests/processor/pre/test_pre_without_prompt.py
  function test_pre_without_prompt (line 10) | def test_pre_without_prompt():

FILE: tests/integration_tests/test_redis_onnx.py
  function test_redis_sqlite (line 13) | def test_redis_sqlite():

FILE: tests/integration_tests/test_sqlite_faiss_onnx.py
  class TestSqliteInvalid (line 13) | class TestSqliteInvalid(Base):
    method test_invalid_similarity_threshold (line 23) | def test_invalid_similarity_threshold(self, threshold):
    method test_no_openai_key (line 50) | def test_no_openai_key(self):
  class TestSqliteFaiss (line 85) | class TestSqliteFaiss(Base):
    method test_hit_default (line 94) | def test_hit_default(self):
    method test_hit (line 126) | def test_hit(self):
    method test_miss (line 159) | def test_miss(self):
    method test_disable_cache (line 198) | def test_disable_cache(self):

FILE: tests/integration_tests/test_sqlite_milvus_sbert.py
  function get_text_response (line 16) | def get_text_response(response):
  class TestSqliteMilvus (line 26) | class TestSqliteMilvus(Base):
    method test_cache_health_check (line 34) | def test_cache_health_check(self):

FILE: tests/integration_tests/utils/util_log.py
  class TestLog (line 7) | class TestLog:
    method __init__ (line 8) | def __init__(self, logger, log_debug, log_file, log_err, log_worker):

FILE: tests/unit_tests/adapter/test_adapter.py
  function test_adapt (line 19) | def test_adapt():
  function test_not_init_cache (line 117) | def test_not_init_cache():
  function test_cache_temperature (line 128) | def test_cache_temperature():
  function test_input_summarization (line 164) | def test_input_summarization():

FILE: tests/unit_tests/adapter/test_api.py
  function test_gptcache_api (line 25) | def test_gptcache_api():
  function test_none_scale_data (line 54) | def test_none_scale_data():
  function test_init_with_config (line 85) | def test_init_with_config():
  function test_init_with_new_config (line 117) | def test_init_with_new_config():

FILE: tests/unit_tests/adapter/test_diffusers.py
  function get_dummy_components (line 22) | def get_dummy_components():
  function test_stable_diffusion (line 78) | def test_stable_diffusion():

FILE: tests/unit_tests/adapter/test_dolly.py
  class MockDolly (line 15) | class MockDolly:
    method __init__ (line 16) | def __init__(self, *args, **kwargs):
    method __call__ (line 19) | def __call__(self, inputs, **kwargs):
  class TestDolly (line 23) | class TestDolly(unittest.TestCase):
    method test_normal (line 24) | def test_normal(self):

FILE: tests/unit_tests/adapter/test_langchain_models.py
  function test_langchain_llms (line 21) | def test_langchain_llms():
  function test_langchain_chats (line 62) | def test_langchain_chats():
  function test_last_content_without_template (line 143) | def test_last_content_without_template():

FILE: tests/unit_tests/adapter/test_llama_cpp.py
  class MockLlama (line 16) | class MockLlama:
    method __init__ (line 17) | def __init__(self, *args, **kwargs):
    method create_completion (line 20) | def create_completion(*args, **kwargs):
  class TestLlama (line 49) | class TestLlama(unittest.TestCase):
    method test_llama_cpp (line 50) | def test_llama_cpp(self):
    method test_llama_cpp_stream (line 77) | def test_llama_cpp_stream(self):

FILE: tests/unit_tests/adapter/test_openai.py
  function test_normal_openai (line 44) | def test_normal_openai(enable_token_counter):
  function test_normal_openai_async (line 87) | async def test_normal_openai_async(enable_token_counter):
  function test_stream_openai (line 132) | def test_stream_openai():
  function test_stream_openai_async (line 206) | async def test_stream_openai_async():
  function test_completion (line 290) | def test_completion():
  function test_completion_async (line 314) | async def test_completion_async():
  function test_completion_error_wrapping (line 342) | async def test_completion_error_wrapping():
  function test_image_create (line 359) | def test_image_create():
  function test_audio_transcribe (line 415) | def test_audio_transcribe():
  function test_audio_translate (line 437) | def test_audio_translate():
  function test_moderation (line 463) | def test_moderation():
  function test_base_llm_cache (line 581) | def test_base_llm_cache():
  function test_base_llm_cache_async (line 679) | async def test_base_llm_cache_async():

FILE: tests/unit_tests/adapter/test_replicate.py
  function test_replicate (line 25) | def test_replicate():

FILE: tests/unit_tests/adapter/test_stability.py
  function test_stability_inference_map (line 25) | def test_stability_inference_map():
  function test_stability_inference_faiss (line 61) | def test_stability_inference_faiss():

FILE: tests/unit_tests/embedding/test_cohere.py
  function test_embedding (line 11) | def test_embedding():

FILE: tests/unit_tests/embedding/test_data2vec.py
  function test_data2vec_audio (line 9) | def test_data2vec_audio():

FILE: tests/unit_tests/embedding/test_embedding_openai.py
  function test_embedding (line 8) | def test_embedding():

FILE: tests/unit_tests/embedding/test_embedding_string.py
  function test_embedding (line 4) | def test_embedding():

FILE: tests/unit_tests/embedding/test_huggingface.py
  function test_huggingface (line 5) | def test_huggingface():

FILE: tests/unit_tests/embedding/test_langchain.py
  function test_langchain_embedding (line 8) | def test_langchain_embedding():

FILE: tests/unit_tests/embedding/test_onnx.py
  function test_onnx (line 5) | def test_onnx():

FILE: tests/unit_tests/embedding/test_paddlenlp.py
  function test_paddlenlp (line 5) | def test_paddlenlp():

FILE: tests/unit_tests/embedding/test_rwkv.py
  function test_rwkv (line 5) | def test_rwkv():

FILE: tests/unit_tests/embedding/test_sbert.py
  function test_sbert (line 5) | def test_sbert():

FILE: tests/unit_tests/embedding/test_timm.py
  function test_timm (line 9) | def test_timm():

FILE: tests/unit_tests/embedding/test_uform.py
  function test_uform (line 13) | def test_uform():

FILE: tests/unit_tests/embedding/test_vit.py
  function test_timm (line 9) | def test_timm():

FILE: tests/unit_tests/eviction/test_distributed_cache.py
  class TestDistributedCache (line 13) | class TestDistributedCache(unittest.TestCase):
    method setUp (line 16) | def setUp(cls) -> None:
    method _clear_test_db (line 20) | def _clear_test_db():
    method test_redis_only_cache_eviction (line 26) | def test_redis_only_cache_eviction(self):
    method test_eviction_base_str (line 37) | def test_eviction_base_str(self):
    method test_redis_sqlite_cache_eviction (line 53) | def test_redis_sqlite_cache_eviction(self):
    method test_noeviction_policy (line 72) | def test_noeviction_policy(self):
    method test_ttl (line 100) | def test_ttl(self):
    method test_redis_only_config (line 126) | def test_redis_only_config(self):
    method test_redis_only_with_no_op_eviction_config (line 147) | def test_redis_only_with_no_op_eviction_config(self):
    method test_cache_configuration (line 168) | def test_cache_configuration(self):
    method test_ttl_access (line 183) | def test_ttl_access(self):

FILE: tests/unit_tests/eviction/test_memory_cache.py
  class TestEviction (line 8) | class TestEviction(unittest.TestCase):
    method test_memory_cache_eviction (line 9) | def test_memory_cache_eviction(self):
    method test_lru (line 31) | def test_lru(self):
    method test_lru_no_clean_size (line 56) | def test_lru_no_clean_size(self):
    method test_lfu (line 82) | def test_lfu(self):
    method test_fifo (line 113) | def test_fifo(self):
    method test_rr (line 138) | def test_rr(self):

FILE: tests/unit_tests/manager/test_base.py
  class TestBaseStore (line 9) | class TestBaseStore(unittest.TestCase):
    method test_cache_base (line 10) | def test_cache_base(self):
    method test_vector_base (line 17) | def test_vector_base(self):

FILE: tests/unit_tests/manager/test_chromadb.py
  class TestChromadb (line 9) | class TestChromadb(unittest.TestCase):
    method test_normal (line 10) | def test_normal(self):

FILE: tests/unit_tests/manager/test_dynamo_storage.py
  class TestDynamoCacheStorage (line 19) | class TestDynamoCacheStorage(unittest.TestCase):
    method setUp (line 23) | def setUp(self):
    method test_create_is_idempotent (line 44) | def test_create_is_idempotent(self):
    method test_creation_and_batch_insert (line 60) | def test_creation_and_batch_insert(self):
    method test_mark_deleted (line 126) | def test_mark_deleted(self):
    method test_clear_deleted_data (line 140) | def test_clear_deleted_data(self):
    method test_get_ids (line 163) | def test_get_ids(self):
    method test_count (line 189) | def test_count(self):
    method test_add_session (line 211) | def test_add_session(self):
    method test_list_sessions (line 240) | def test_list_sessions(self):
    method test_delete_session (line 273) | def test_delete_session(self):
    method test_get_data_by_id (line 292) | def test_get_data_by_id(self):
    method test_report_cache (line 341) | def test_report_cache(self):
    method _random_cachedata_without_dependencies (line 375) | def _random_cachedata_without_dependencies(self, session_id = None):
    method _random_cachedata_with_dependencies (line 384) | def _random_cachedata_with_dependencies(self, session_id = None):
    method _dynamo_resource (line 399) | def _dynamo_resource(self):

FILE: tests/unit_tests/manager/test_eviction.py
  function mock_embeddings (line 12) | def mock_embeddings():
  class TestEviction (line 16) | class TestEviction(unittest.TestCase):
    method test_eviction_lru (line 19) | def test_eviction_lru(self):
    method test_eviction_fifo (line 36) | def test_eviction_fifo(self):

FILE: tests/unit_tests/manager/test_factory.py
  class TestFactory (line 13) | class TestFactory(unittest.TestCase):
    method test_normal (line 14) | def test_normal(self):
    method test_manager (line 37) | def test_manager(self):
    method test_manager_factory (line 75) | def test_manager_factory(self):
    method test_multi_level_directory (line 107) | def test_multi_level_directory(self):

FILE: tests/unit_tests/manager/test_local_index.py
  class TestLocalIndex (line 20) | class TestLocalIndex(unittest.TestCase):
    method test_faiss (line 21) | def test_faiss(self):
    method test_hnswlib (line 34) | def test_hnswlib(self):
    method test_docarray (line 51) | def test_docarray(self):
    method _internal_test_normal (line 63) | def _internal_test_normal(self, vector_class):
    method _internal_test_with_rebuild (line 77) | def _internal_test_with_rebuild(self, vector_class):
    method _internal_test_reload (line 89) | def _internal_test_reload(self, vector_class):
    method _internal_test_delete (line 108) | def _internal_test_delete(self, vector_class):
    method _internal_test_create_from_vector_base (line 122) | def _internal_test_create_from_vector_base(self, **kwargs):

FILE: tests/unit_tests/manager/test_map.py
  function test_map (line 8) | def test_map():

FILE: tests/unit_tests/manager/test_milvusdb.py
  class TestMilvusDB (line 9) | class TestMilvusDB(unittest.TestCase):
    method test_normal (line 10) | def test_normal(self):

FILE: tests/unit_tests/manager/test_mongo.py
  function test_mongo (line 13) | def test_mongo():
  function _clear_test_db (line 30) | def _clear_test_db(dbname):
  function _inner_test_normal (line 36) | def _inner_test_normal(dbname: str):
  function _inner_test_with_deps (line 72) | def _inner_test_with_deps(dbname: str):
  function _test_create_on (line 110) | def _test_create_on(dbname):
  function _test_session (line 137) | def _test_session(dbname):

FILE: tests/unit_tests/manager/test_object_storage.py
  class TestLocal (line 14) | class TestLocal(unittest.TestCase):
    method test_normal (line 15) | def test_normal(self):
  class TestS3 (line 27) | class TestS3(unittest.TestCase):
    method test_normal (line 28) | def test_normal(self):
  class TestBase (line 44) | class TestBase(unittest.TestCase):
    method test_local (line 45) | def test_local(self):
    method test_s3 (line 56) | def test_s3(self):

FILE: tests/unit_tests/manager/test_pgvector.py
  class TestPgvector (line 10) | class TestPgvector(unittest.TestCase):
    method test_normal (line 11) | def test_normal(self):

FILE: tests/unit_tests/manager/test_qdrant.py
  class TestQdrant (line 10) | class TestQdrant(unittest.TestCase):
    method test_normal (line 11) | def test_normal(self):

FILE: tests/unit_tests/manager/test_redis.py
  function test_redis_vector_store (line 8) | def test_redis_vector_store():

FILE: tests/unit_tests/manager/test_redis_cache_storage.py
  class TestRedisStorage (line 14) | class TestRedisStorage(unittest.TestCase):
    method setUp (line 20) | def setUp(cls) -> None:
    method _clear_test_db (line 24) | def _clear_test_db():
    method test_normal (line 30) | def test_normal(self):
    method test_with_deps (line 67) | def test_with_deps(self):
    method test_create_on (line 107) | def test_create_on(self):
    method test_session (line 134) | def test_session(self):
    method test_cache_configuration (line 159) | def test_cache_configuration(self):

FILE: tests/unit_tests/manager/test_sql_scalar.py
  class TestSQLStore (line 13) | class TestSQLStore(unittest.TestCase):
    method test_sqlite (line 14) | def test_sqlite(self):
    method test_duckdb (line 18) | def test_duckdb(self):
    method _inner_test_normal (line 23) | def _inner_test_normal(self, db_name: str):
    method _inner_test_with_deps (line 68) | def _inner_test_with_deps(self, db_name: str):
    method test_create_on (line 110) | def test_create_on(self):

FILE: tests/unit_tests/manager/test_usearch.py
  class TestUSearchDB (line 9) | class TestUSearchDB(unittest.TestCase):
    method test_normal (line 10) | def test_normal(self):

FILE: tests/unit_tests/processor/test_concat_context.py
  function test_concat_context_process (line 4) | def test_concat_context_process():

FILE: tests/unit_tests/processor/test_context.py
  class CITestContextProcess (line 13) | class CITestContextProcess(ContextProcess):
    method __init__ (line 14) | def __init__(self):
    method format_all_content (line 17) | def format_all_content(self, data: Dict[str, Any], **params: Dict[str,...
    method process_all_content (line 20) | def process_all_content(self) -> (Any, Any):
  function test_context_process (line 26) | def test_context_process():

FILE: tests/unit_tests/processor/test_post.py
  function test_random_one (line 5) | def test_random_one():
  function test_first (line 10) | def test_first():
  function test_nop (line 15) | def test_nop():
  function test_temperature_softmax (line 21) | def test_temperature_softmax():
  function test_llm_verifier (line 32) | def test_llm_verifier():

FILE: tests/unit_tests/processor/test_pre.py
  function test_last_content (line 12) | def test_last_content():
  function test_last_content_without_prompt (line 18) | def test_last_content_without_prompt():
  function test_all_content (line 35) | def test_all_content():
  function test_nop (line 41) | def test_nop():
  function test_get_prompt (line 46) | def test_get_prompt():
  function test_get_openai_moderation_input (line 51) | def test_get_openai_moderation_input():
  function test_get_messages_last_content (line 56) | def test_get_messages_last_content():
  function test_concat_all_queries (line 60) | def test_concat_all_queries():

FILE: tests/unit_tests/processor/test_selective_context.py
  function test_selective_context_process (line 5) | def test_selective_context_process():

FILE: tests/unit_tests/processor/test_summarize_context.py
  function test_summarization_context_process (line 6) | def test_summarization_context_process():

FILE: tests/unit_tests/similarity_evaluation/test_cohere_rerank.py
  function test_cohere_rerank (line 12) | def test_cohere_rerank():

FILE: tests/unit_tests/similarity_evaluation/test_evaluation_kreciprocal.py
  function normalize (line 8) | def normalize(vec):
  function _test_evaluation (line 15) | def _test_evaluation(evaluation):
  function test_kreciprocal (line 32) | def test_kreciprocal():
  function test_get_eval (line 36) | def test_get_eval():

FILE: tests/unit_tests/similarity_evaluation/test_evaluation_onnx.py
  function _test_evaluation (line 7) | def _test_evaluation(evaluation):
  function test_onnx (line 27) | def test_onnx():
  function test_get_eval (line 32) | def test_get_eval():

FILE: tests/unit_tests/similarity_evaluation/test_evaluation_sbert.py
  function _test_evaluation (line 7) | def _test_evaluation(evaluation):
  function test_sbert (line 26) | def test_sbert():
  function test_get_eval (line 31) | def test_get_eval():

FILE: tests/unit_tests/similarity_evaluation/test_evaluation_sequence.py
  function normalize (line 8) | def normalize(vec):
  function _test_evaluation (line 13) | def _test_evaluation(evaluation):
  function test_sequence_match (line 31) | def test_sequence_match():
  function test_get_eval (line 37) | def test_get_eval():
  function test_reweigth (line 49) | def test_reweigth():

FILE: tests/unit_tests/similarity_evaluation/test_evaluation_string.py
  function _test_evaluation (line 6) | def _test_evaluation(evaluation):
  function test_exact_match_evaluation (line 18) | def test_exact_match_evaluation():
  function test_get_eval (line 23) | def test_get_eval():

FILE: tests/unit_tests/similarity_evaluation/test_evalution_time.py
  function test_evaluation_time (line 7) | def test_evaluation_time():

FILE: tests/unit_tests/similarity_evaluation/test_np.py
  function _test_evaluation (line 12) | def _test_evaluation(evaluation):
  function test_norm (line 44) | def test_norm():
  function test_get_eval (line 49) | def test_get_eval():

FILE: tests/unit_tests/similarity_evaluation/test_simple.py
  function _test_evaluation_default (line 7) | def _test_evaluation_default(evaluation):
  function _test_evaluation_config (line 19) | def _test_evaluation_config(evaluation):
  function test_search_distance_evaluation (line 30) | def test_search_distance_evaluation():
  function test_get_eval (line 38) | def test_get_eval():

FILE: tests/unit_tests/test_client.py
  function test_client (line 9) | def test_client():

FILE: tests/unit_tests/test_core.py
  function test_time_cal (line 9) | def test_time_cal():
  function test_cache_all (line 41) | def test_cache_all():
  function test_report (line 45) | def test_report():

FILE: tests/unit_tests/test_session.py
  function check_hit (line 15) | def check_hit(cur_session_id, cache_session_ids, cache_questions, cache_...
  class TestSession (line 21) | class TestSession(unittest.TestCase):
    method test_with (line 27) | def test_with(self):
    method test_map (line 48) | def test_map(self):
    method test_ssd (line 83) | def test_ssd(self):

FILE: tests/unit_tests/utils/test_error.py
  function test_error_type (line 9) | def test_error_type():
  function test_wrap (line 20) | def test_wrap():

FILE: tests/unit_tests/utils/test_log.py
  function test_error_type (line 4) | def test_error_type():

FILE: tests/unit_tests/utils/test_response.py
  function test_get_message_from_openai_answer (line 7) | def test_get_message_from_openai_answer():
  function test_get_stream_message_from_openai_answer (line 31) | def test_get_stream_message_from_openai_answer():
Condensed preview — 290 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (3,282K chars).
[
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.yaml",
    "chars": 1407,
    "preview": "name: 🐞 Bug Report\ndescription: Create a bug report to help us improve GPTCache\ntitle: \"[Bug]: \"\nbody:\n- type: markdown\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/documentation_improvement.yaml",
    "chars": 1010,
    "preview": "name: 📖 Documentation Improvement\ndescription: Suggest improvements to our documentation\ntitle: \"[DOCS]: \"\nlabels: [Docu"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/enhancement.yaml",
    "chars": 929,
    "preview": "name: Enhancement Request\ndescription: As a developer, I want to make an enhancement for GPTCache\ntitle: \"[Enhancement]:"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.yaml",
    "chars": 1164,
    "preview": "name: 🚀 Feature Request\ndescription: As a user, I want to request a feature for GPTCache\ntitle: \"[Feature]:\"\nlabels: [ki"
  },
  {
    "path": ".github/mergify.yml",
    "chars": 2646,
    "preview": "pull_request_rules:\n  - name: Add needs-dco label when DCO check failed\n    conditions:\n      - or:\n        - base=main\n"
  },
  {
    "path": ".github/workflows/Nightly_CI_main.yaml",
    "chars": 1921,
    "preview": "name: Nightly CI\n\non:\n  schedule:\n    # * is a special character in YAML so you have to quote this string\n    #        ┌"
  },
  {
    "path": ".github/workflows/build_dev_python_package.yaml",
    "chars": 1208,
    "preview": "name: Build dev package\non:\n  push:\n    branches:\n      - main\n      - dev\n    paths:\n      - 'examples/**'\n      - 'gpt"
  },
  {
    "path": ".github/workflows/publish_dev_package.yaml",
    "chars": 1672,
    "preview": "name: Publish package to TestPyPI\n# on:\n#   push:\n#     branches:\n#       - 'main'\n#     paths:\n#       - 'gptcache/**'\n"
  },
  {
    "path": ".github/workflows/publish_release_image.yaml",
    "chars": 1048,
    "preview": "name: Publish image to dockerhub\non:\n  release:\n    types: [published]\n  workflow_dispatch:\n\njobs:\n  build-and-publish-i"
  },
  {
    "path": ".github/workflows/publish_release_package.yaml",
    "chars": 1287,
    "preview": "name: Publish package to PyPI\n# on:\n#   push:\n#     branches:\n#       - 'main'\n#     paths:\n#       - 'gptcache/**'\n#   "
  },
  {
    "path": ".github/workflows/pylint.yaml",
    "chars": 955,
    "preview": "name: Pylint\non:\n  push:\n    branches:\n      - main\n      - dev\n      # file paths to consider in the event\n    paths:\n "
  },
  {
    "path": ".github/workflows/unit_test_main.yaml",
    "chars": 3028,
    "preview": "name: Unit test\n\non:\n  push:\n    branches:\n      - main\n      - dev\n    paths:\n      - 'examples/**'\n      - 'gptcache/*"
  },
  {
    "path": ".gitignore",
    "chars": 1972,
    "preview": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packagi"
  },
  {
    "path": "LICENSE",
    "chars": 1068,
    "preview": "The MIT License (MIT)\n\nCopyright (c) Zilliz\n\nPermission is hereby granted, free of charge, to any person obtaining a cop"
  },
  {
    "path": "MANIFEST.in",
    "chars": 25,
    "preview": "include requirements.txt\n"
  },
  {
    "path": "Makefile",
    "chars": 593,
    "preview": "install:\n\t@pip install -r requirements.txt\n\t@python setup.py install\n\npip_upgrade:\n\t@python -m pip install --upgrade pip"
  },
  {
    "path": "OWNERS",
    "chars": 143,
    "preview": "filters:\n  \".*\":\n    reviewers:\n      - SimFG\n      - xiaofan-luan\n      - cxie\n    approvers:\n      - SimFG\n      - xia"
  },
  {
    "path": "README.md",
    "chars": 23791,
    "preview": "# GPTCache : A Library for Creating Semantic Cache for LLM Queries\nSlash Your LLM API Costs by 10x 💰, Boost Speed by 100"
  },
  {
    "path": "cache_config_template.yml",
    "chars": 576,
    "preview": "# For `model_src`, `evaluation`, `post_function`, `pre_function`,\n# `storage_config` options, Check README for more.\n\nem"
  },
  {
    "path": "codecov.yml",
    "chars": 736,
    "preview": "# Configuration File for CodeCov\ncodecov:\n  require_ci_to_pass: no\n  notify:\n    require_ci_to_pass: no\n    wait_for_ci:"
  },
  {
    "path": "docs/.readthedocs.yaml",
    "chars": 60,
    "preview": "version: 2\n\npython:\n   version: 3.8\n   system_packages: true"
  },
  {
    "path": "docs/Makefile",
    "chars": 672,
    "preview": "# Minimal makefile for Sphinx documentation\n#\n\n# You can set these variables from the command line, and also\n# from the "
  },
  {
    "path": "docs/_exts/docgen2.py",
    "chars": 4696,
    "preview": "import os\nimport sys\n\nfrom jinja2 import Environment, FileSystemLoader, select_autoescape\n\n_default_skip_file_list = [\"_"
  },
  {
    "path": "docs/_exts/index_con.py",
    "chars": 1882,
    "preview": "from m2r2 import convert\nimport os\n\nclass IndexCon:\n    \n  def __init__(self, source, output):\n    self.source = source\n"
  },
  {
    "path": "docs/_templates/author.html",
    "chars": 53,
    "preview": "By <a href=\"https://zilliz.com/cloud\">Zilliz Inc.</a>"
  },
  {
    "path": "docs/_templates/copyright.html",
    "chars": 75,
    "preview": "<p>© Copyright 2023, <a href=\"https://zilliz.com/cloud\">Zilliz Inc.</a></p>"
  },
  {
    "path": "docs/_templates/function.rst",
    "chars": 237,
    "preview": "{{ module_name | cap }}\n{{ module_name | title_bar }}\n\n.. contents:: Index\n\n{% for func in funcs  -%}\n{{func[0]}}\n{{ fun"
  },
  {
    "path": "docs/_templates/index.rst",
    "chars": 147,
    "preview": "🥸 API References\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n{% for module in modules %}\n   "
  },
  {
    "path": "docs/bootcamp/langchain/baby_agi.ipynb",
    "chars": 13072,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"517a9fd4\",\n   \"metadata\": {},\n   \"source\": [\n    \"# BabyAGI User"
  },
  {
    "path": "docs/bootcamp/langchain/index.rst",
    "chars": 143,
    "preview": "LangChain\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   qa_generation\n   question_answerin"
  },
  {
    "path": "docs/bootcamp/langchain/qa_generation.ipynb",
    "chars": 7551,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"ee2a3a21\",\n   \"metadata\": {},\n   \"source\": [\n    \"# QA Generatio"
  },
  {
    "path": "docs/bootcamp/langchain/question_answering.ipynb",
    "chars": 13570,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"05859721\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Question Ans"
  },
  {
    "path": "docs/bootcamp/langchain/sqlite.ipynb",
    "chars": 24750,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"0ed6aab1\",\n   \"metadata\": {\n    \"pycharm\": {\n     \"name\": \"#%% m"
  },
  {
    "path": "docs/bootcamp/llama_index/index.rst",
    "chars": 98,
    "preview": "Llama Index\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   webpage_qa"
  },
  {
    "path": "docs/bootcamp/llama_index/webpage_qa.ipynb",
    "chars": 4439,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"d39937ca\",\n   \"metadata\": {},\n   \"source\": [\n    \"# WebPage QA\\n"
  },
  {
    "path": "docs/bootcamp/openai/chat.ipynb",
    "chars": 11394,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"6b3ba1cc\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Chat\\n\",\n   "
  },
  {
    "path": "docs/bootcamp/openai/image_generation.ipynb",
    "chars": 7680,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"8b0e0905\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Image Genera"
  },
  {
    "path": "docs/bootcamp/openai/index.rst",
    "chars": 183,
    "preview": "OpenAI\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   chat\n   image_generation\n   sql_trans"
  },
  {
    "path": "docs/bootcamp/openai/language_translate.ipynb",
    "chars": 9487,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"0ac4a3bc\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Language Tra"
  },
  {
    "path": "docs/bootcamp/openai/speech_to_text.ipynb",
    "chars": 7960,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"8b0e0905\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Speech to Te"
  },
  {
    "path": "docs/bootcamp/openai/sql_translate.ipynb",
    "chars": 11332,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"2d5f1205\",\n   \"metadata\": {},\n   \"source\": [\n    \"# SQL Translat"
  },
  {
    "path": "docs/bootcamp/openai/tweet_classifier.ipynb",
    "chars": 9191,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"6b3ba1cc\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Tweet Classi"
  },
  {
    "path": "docs/bootcamp/replicate/index.rst",
    "chars": 111,
    "preview": "Replicate\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   visual_question_answering"
  },
  {
    "path": "docs/bootcamp/replicate/visual_question_answering.ipynb",
    "chars": 8128,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"39354318\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Visual Quest"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-audio/.streamlit/config.toml",
    "chars": 20,
    "preview": "[theme]\nbase=\"dark\"\n"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-audio/README.md",
    "chars": 1090,
    "preview": "# GPTCache Demo for OpenAI Audio Transcription\nThis project demonstrates how [GPTcache](https://github.com/zilliztech/GP"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-audio/audio.py",
    "chars": 2460,
    "preview": "import streamlit as st\nimport os\nimport uuid\n\nfrom gptcache import cache\nfrom gptcache.manager import get_data_manager, "
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-audio/requirements.txt",
    "chars": 77,
    "preview": "gptcache\npillow\nstreamlit\ntorch\nfaiss-cpu\ntorchaudio\ntransformers\nsqlalchemy\n"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-image/README.md",
    "chars": 1112,
    "preview": "# GPTCache Demo for OpenAI Image Generation\nThis project demonstrates how [GPTcache](https://github.com/zilliztech/GPTCa"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-image/imagen.py",
    "chars": 2944,
    "preview": "import streamlit as st\nfrom PIL import Image\nimport os\nimport io\nimport base64\nfrom io import BytesIO\nimport requests\n\nf"
  },
  {
    "path": "docs/bootcamp/streamlit/gptcache-streamlit-image/requirements.txt",
    "chars": 72,
    "preview": "gptcache\npillow\nstreamlit\nonnxruntime\nfaiss-cpu\ntransformers\nsqlalchemy\n"
  },
  {
    "path": "docs/bootcamp/temperature/chat.ipynb",
    "chars": 10295,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"82e45532\",\n   \"metadata\": {},\n   \"source\": [\n    \"# OpenAI Chat "
  },
  {
    "path": "docs/bootcamp/temperature/create_image.ipynb",
    "chars": 1847788,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"id\": \"2215b430\",\n   \"metadata\": {},\n   \"source\": [\n    \"# OpenAI Image"
  },
  {
    "path": "docs/bootcamp/temperature/index.rst",
    "chars": 108,
    "preview": "Temperature\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   chat\n   create_image"
  },
  {
    "path": "docs/bootcamp/vertex/index.rst",
    "chars": 99,
    "preview": "Vertex\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   vertexai_caching"
  },
  {
    "path": "docs/bootcamp/vertex/vertexai_caching.ipynb",
    "chars": 28144,
    "preview": "{\n  \"cells\": [\n    {\n      \"cell_type\": \"markdown\",\n      \"id\": \"6b3ba1cc\",\n      \"metadata\": {\n        \"id\": \"6b3ba1cc\""
  },
  {
    "path": "docs/conf.py",
    "chars": 3701,
    "preview": "# Configuration file for the Sphinx documentation builder.\n#\n# This file only contains a selection of the most common op"
  },
  {
    "path": "docs/configure_it.md",
    "chars": 30006,
    "preview": "# How to better configure your cache\n\n**Last update time**: 2023.6.26\n\n**Latest version**: v0.1.32\n\nBefore reading the f"
  },
  {
    "path": "docs/contributing.md",
    "chars": 9050,
    "preview": "# 😍 Contributing to GPTCache\n\nBefore contributing to GPTCache, it is recommended to read the [usage doc](https://github."
  },
  {
    "path": "docs/feature.md",
    "chars": 1306,
    "preview": "# Feature\n\n- Support the openai chat completion normal and stream request\n- Get top_k similar search results, it can be "
  },
  {
    "path": "docs/horizontal-scaling-usage.md",
    "chars": 3416,
    "preview": "# Horizontal Scaling using Distributed Cache\n\nTo see the need for Horizontal scaling of the cache, we need to see how GP"
  },
  {
    "path": "docs/index.rst",
    "chars": 25038,
    "preview": "\nGPTCache : A Library for Creating Semantic Cache for LLM Queries\n======================================================"
  },
  {
    "path": "docs/make.bat",
    "chars": 800,
    "preview": "@ECHO OFF\r\n\r\npushd %~dp0\r\n\r\nREM Command file for Sphinx documentation\r\n\r\nif \"%SPHINXBUILD%\" == \"\" (\r\n\tset SPHINXBUILD=sp"
  },
  {
    "path": "docs/references/index.rst",
    "chars": 183,
    "preview": "🥸 API References\n=================\n\n.. toctree::\n   :maxdepth: 1\n   :caption: Contents:\n\n\n   gptcache\n   processor\n   em"
  },
  {
    "path": "docs/release_note.md",
    "chars": 21323,
    "preview": "# Release Note\n\nTo read the following content, you need to understand the basic use of GPTCache, references:\n\n- [Readme "
  },
  {
    "path": "docs/requirements.txt",
    "chars": 170,
    "preview": "sphinx\nurllib3<2.0\npyqt5<5.13\npyqtwebengine<5.13\nnbsphinx\nautodoc_pydantic\nmyst_nb\nsphinx-autobuild\nsphinx_book_theme\nsp"
  },
  {
    "path": "docs/toc.bak",
    "chars": 629,
    "preview": ".. toctree::\n   :maxdepth: 1\n   :caption: Getting Started\n   :name: getting-started\n   :hidden:\n\n   usage.md\n   feature."
  },
  {
    "path": "docs/usage.md",
    "chars": 13316,
    "preview": "# GPTCache Quick Start\n\nGPTCache is easy to use and can reduce the latency of LLM queries by 100x in just two steps:\n\n1."
  },
  {
    "path": "examples/README.md",
    "chars": 24183,
    "preview": "# Example\n\n- [Example](#example)\n  - [How to run Visual Question Answering with MiniGPT-4](#how-to-run-visual-question-a"
  },
  {
    "path": "examples/adapter/api.py",
    "chars": 825,
    "preview": "from gptcache import cache, Config, Cache\nfrom gptcache.adapter.api import put, get, init_similar_cache\nfrom gptcache.pr"
  },
  {
    "path": "examples/adapter/langchain_llms.py",
    "chars": 1365,
    "preview": "import os\n\nfrom langchain import Cohere\nfrom langchain.llms import OpenAI\nfrom langchain.chat_models import ChatOpenAI\nf"
  },
  {
    "path": "examples/adapter/openai_chatgpt.py",
    "chars": 323,
    "preview": "from gptcache import cache\nfrom gptcache.adapter import openai\n\ncache.init()\ncache.set_openai_key()\n\nquestion = 'what is"
  },
  {
    "path": "examples/benchmark/benchmark_sqlite_faiss_onnx.py",
    "chars": 3813,
    "preview": "import json\nimport os\nimport time\n\nfrom gptcache.adapter import openai\nfrom gptcache import cache, Config\nfrom gptcache."
  },
  {
    "path": "examples/benchmark/mock_data.json",
    "chars": 317720,
    "preview": "[\n    {\n        \"origin\": \"Hugging Face Hub is a platform to host Git-based models, datasets, and Spaces.\",\n        \"sim"
  },
  {
    "path": "examples/context_process/selective_context.py",
    "chars": 2672,
    "preview": "import os\nimport time\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.embedding import Onn"
  },
  {
    "path": "examples/context_process/summarization_context.py",
    "chars": 2515,
    "preview": "import os\nimport time\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.embedding import Onn"
  },
  {
    "path": "examples/data_manager/map_manager.py",
    "chars": 601,
    "preview": "import os\n\nfrom gptcache.manager import get_data_manager\nfrom gptcache.adapter import openai\nfrom gptcache import cache\n"
  },
  {
    "path": "examples/data_manager/scalar_store.py",
    "chars": 2030,
    "preview": "import os\n\nimport numpy as np\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.manager impo"
  },
  {
    "path": "examples/data_manager/vector_store.py",
    "chars": 1118,
    "preview": "import numpy as np\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.manager import CacheBas"
  },
  {
    "path": "examples/embedding/default.py",
    "chars": 450,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.embedding.string import to_embeddings as st"
  },
  {
    "path": "examples/embedding/onnx.py",
    "chars": 922,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager.factory import get_data_manager\nfro"
  },
  {
    "path": "examples/embedding/paddlenlp.py",
    "chars": 947,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager.factory import get_data_manager\nfro"
  },
  {
    "path": "examples/embedding/random.py",
    "chars": 915,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager import get_data_manager, CacheBase,"
  },
  {
    "path": "examples/eviction/distributed_eviction.py",
    "chars": 3502,
    "preview": "from gptcache import Cache\nfrom gptcache.embedding import Onnx\n\nfrom gptcache.manager.eviction import EvictionBase\n\nfrom"
  },
  {
    "path": "examples/integrate/diffusers/stable_diffusion.py",
    "chars": 1646,
    "preview": "import time\n\nimport torch\nfrom PIL import ImageChops\nfrom diffusers import DPMSolverMultistepScheduler\n\nfrom gptcache.ad"
  },
  {
    "path": "examples/integrate/dolly/basic_usage.py",
    "chars": 2346,
    "preview": "import time\nimport torch\nfrom transformers import pipeline\nfrom gptcache.processor.pre import get_inputs\nfrom gptcache.m"
  },
  {
    "path": "examples/integrate/langchain/langchain_llms_mock.py",
    "chars": 1196,
    "preview": "import os\n\nfrom langchain import Cohere\nfrom langchain.llms import OpenAI\n\nfrom gptcache.adapter.langchain_models import"
  },
  {
    "path": "examples/integrate/langchain/langchain_prompt_openai.py",
    "chars": 954,
    "preview": "import os\n\nimport openai\nimport time\nfrom langchain.llms import OpenAI\nfrom langchain import PromptTemplate, LLMChain\n\nf"
  },
  {
    "path": "examples/integrate/langchain/langchain_qa_chain.py",
    "chars": 936,
    "preview": "import time\n\nfrom langchain import OpenAI\nfrom langchain.chains.question_answering import load_qa_chain\nfrom langchain.s"
  },
  {
    "path": "examples/integrate/langchain/langchain_similaritycache_openai.py",
    "chars": 1084,
    "preview": "import os\nimport time\n\nimport openai\nfrom langchain import PromptTemplate\nfrom langchain.llms import OpenAI\n\nfrom gptcac"
  },
  {
    "path": "examples/integrate/llama_cpp/basic_usage.py",
    "chars": 1788,
    "preview": "import time\n\nfrom gptcache.adapter.llama_cpp import Llama\nfrom gptcache.manager import manager_factory\nfrom gptcache imp"
  },
  {
    "path": "examples/integrate/openai/basic_usage.py",
    "chars": 3523,
    "preview": "import os\nimport time\n\nfrom gptcache.manager import get_data_manager, VectorBase\nfrom gptcache import cache, Cache\nfrom "
  },
  {
    "path": "examples/integrate/openai/create_image.py",
    "chars": 2410,
    "preview": "import time\nimport base64\nfrom io import BytesIO\nfrom PIL import Image\n\nfrom gptcache.adapter import openai\nfrom gptcach"
  },
  {
    "path": "examples/integrate/openai/qa.py",
    "chars": 3666,
    "preview": "import os\nimport openai\nimport time\n\nopenai.api_key = os.getenv('OPENAI_API_KEY')\n\nbefore = time.time()\nresponse = opena"
  },
  {
    "path": "examples/integrate/openai/readme.py",
    "chars": 2692,
    "preview": "import os\nimport time\n\nimport openai\n\n\ndef response_text(openai_resp):\n    return openai_resp['choices'][0]['message']['"
  },
  {
    "path": "examples/integrate/openai/summarize.py",
    "chars": 2981,
    "preview": "import os\nimport openai\nimport time\n\nopenai.api_key = os.getenv('OPENAI_API_KEY')\n\nbefore = time.time()\nresponse = opena"
  },
  {
    "path": "examples/integrate/replicate/vqa.py",
    "chars": 1623,
    "preview": "import time\n\nfrom gptcache import cache\nfrom gptcache.adapter import replicate\nfrom gptcache.embedding import Timm, Onnx"
  },
  {
    "path": "examples/integrate/stability/text_to_image.py",
    "chars": 1938,
    "preview": "import io\nimport os\nimport time\n\nfrom PIL import Image\n\nfrom gptcache import cache\nfrom gptcache.adapter.stability_sdk i"
  },
  {
    "path": "examples/processor/llm_verifier_example.py",
    "chars": 1485,
    "preview": "import time\nimport os\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.embedding import Onn"
  },
  {
    "path": "examples/processor/temperature_example.py",
    "chars": 1224,
    "preview": "import time\n\nfrom gptcache import cache\nfrom gptcache.adapter import openai\nfrom gptcache.embedding import Onnx\nfrom gpt"
  },
  {
    "path": "examples/session/session.py",
    "chars": 1274,
    "preview": "from gptcache import cache\nfrom gptcache.session import Session\nfrom gptcache.adapter import openai\n\n# init gptcache\ncac"
  },
  {
    "path": "examples/similarity_evaluation/exact_match.py",
    "chars": 467,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.similarity_evaluation.exact_match import Ex"
  },
  {
    "path": "examples/similarity_evaluation/onnx.py",
    "chars": 874,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager import get_data_manager, CacheBase,"
  },
  {
    "path": "examples/similarity_evaluation/search_distance.py",
    "chars": 818,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager import get_data_manager, VectorBase"
  },
  {
    "path": "examples/similarity_evaluation/sequence_match.py",
    "chars": 1381,
    "preview": "from gptcache.adapter import openai\nfrom gptcache import cache\nfrom gptcache.manager import get_data_manager, VectorBase"
  },
  {
    "path": "examples/vqa_demo.py",
    "chars": 3530,
    "preview": "# ================================================================================\n# This demo comes from [minigpt4](htt"
  },
  {
    "path": "gptcache/__init__.py",
    "chars": 146,
    "preview": "\"\"\"gptcache version\"\"\"\n__version__ = \"0.1.44\"\n\nfrom gptcache.config import Config\nfrom gptcache.core import Cache\nfrom g"
  },
  {
    "path": "gptcache/adapter/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "gptcache/adapter/adapter.py",
    "chars": 23777,
    "preview": "import time\n\nimport numpy as np\n\nfrom gptcache import cache\nfrom gptcache.processor.post import temperature_softmax, Llm"
  },
  {
    "path": "gptcache/adapter/api.py",
    "chars": 10448,
    "preview": "# pylint: disable=wrong-import-position\nfrom typing import Any, Optional, Callable\n\nimport gptcache.processor.post\nimpor"
  },
  {
    "path": "gptcache/adapter/base.py",
    "chars": 2365,
    "preview": "from abc import ABCMeta\nfrom typing import Any, Dict, Callable, Optional\n\n\nclass BaseCacheLLM(metaclass=ABCMeta):\n    \"\""
  },
  {
    "path": "gptcache/adapter/diffusers.py",
    "chars": 2514,
    "preview": "import base64\nfrom io import BytesIO\n\nfrom gptcache.adapter.adapter import adapt\nfrom gptcache.manager.scalar_data.base "
  },
  {
    "path": "gptcache/adapter/dolly.py",
    "chars": 2131,
    "preview": "from typing import Any\n\nfrom gptcache.adapter.adapter import adapt\nfrom gptcache.manager.scalar_data.base import Answer,"
  },
  {
    "path": "gptcache/adapter/langchain_models.py",
    "chars": 8127,
    "preview": "from typing import Optional, List, Any, Mapping\n\nfrom gptcache.adapter.adapter import adapt, aadapt\nfrom gptcache.core i"
  },
  {
    "path": "gptcache/adapter/llama_cpp.py",
    "chars": 3012,
    "preview": "import time\nfrom typing import Iterator\n\nfrom gptcache.adapter.adapter import adapt\nfrom gptcache.manager.scalar_data.ba"
  },
  {
    "path": "gptcache/adapter/minigpt4.py",
    "chars": 3748,
    "preview": "from argparse import Namespace\n\nfrom minigpt4.common.config import Config\nfrom minigpt4.common.registry import registry\n"
  },
  {
    "path": "gptcache/adapter/openai.py",
    "chars": 18014,
    "preview": "import base64\nimport json\nimport os\nimport time\nfrom io import BytesIO\nfrom typing import Any, AsyncGenerator, Iterator,"
  },
  {
    "path": "gptcache/adapter/replicate.py",
    "chars": 3462,
    "preview": "from gptcache.utils.error import CacheError\nfrom gptcache.adapter.adapter import adapt\nfrom gptcache.utils import import"
  },
  {
    "path": "gptcache/adapter/stability_sdk.py",
    "chars": 4191,
    "preview": "import base64\nimport warnings\nfrom dataclasses import dataclass\nfrom io import BytesIO\nfrom typing import List\n\nfrom gpt"
  },
  {
    "path": "gptcache/client.py",
    "chars": 1987,
    "preview": "import asyncio\nimport json\n\nfrom gptcache.utils import import_httpx\n\nimport_httpx()\n\nimport httpx  # pylint: disable=C04"
  },
  {
    "path": "gptcache/config.py",
    "chars": 2897,
    "preview": "from typing import Optional, Callable, List\n\nfrom gptcache.utils.error import CacheError\n\n\nclass Config:\n    \"\"\"Pass con"
  },
  {
    "path": "gptcache/core.py",
    "chars": 5106,
    "preview": "import atexit\nimport os\nfrom typing import Optional, List, Any\n\nfrom gptcache.config import Config\nfrom gptcache.embeddi"
  },
  {
    "path": "gptcache/embedding/__init__.py",
    "chars": 2327,
    "preview": "__all__ = [\n    \"OpenAI\",\n    \"Huggingface\",\n    \"SBERT\",\n    \"Cohere\",\n    \"Onnx\",\n    \"FastText\",\n    \"Data2VecAudio\","
  },
  {
    "path": "gptcache/embedding/base.py",
    "chars": 291,
    "preview": "from abc import ABCMeta, abstractmethod\n\n\nclass BaseEmbedding(metaclass=ABCMeta):\n    \"\"\"\n    Base Embedding interface.\n"
  },
  {
    "path": "gptcache/embedding/cohere.py",
    "chars": 1768,
    "preview": "import numpy as np\n\nfrom gptcache.utils import import_cohere\nfrom gptcache.embedding.base import BaseEmbedding\n\nimport_c"
  },
  {
    "path": "gptcache/embedding/data2vec.py",
    "chars": 2271,
    "preview": "import numpy as np\n\nfrom gptcache.utils import import_huggingface, import_torch, import_torchaudio\nfrom gptcache.embeddi"
  },
  {
    "path": "gptcache/embedding/fasttext.py",
    "chars": 1663,
    "preview": "import numpy as np\nimport os\n\nfrom gptcache.utils import import_fasttext\nfrom gptcache.embedding.base import BaseEmbeddi"
  },
  {
    "path": "gptcache/embedding/huggingface.py",
    "chars": 2705,
    "preview": "import numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_huggingface, imp"
  },
  {
    "path": "gptcache/embedding/langchain.py",
    "chars": 1527,
    "preview": "import numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_langchain\n\nimpor"
  },
  {
    "path": "gptcache/embedding/onnx.py",
    "chars": 2708,
    "preview": "import numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import (\n    import_onnxruntim"
  },
  {
    "path": "gptcache/embedding/openai.py",
    "chars": 2313,
    "preview": "import os\n\nimport numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_opena"
  },
  {
    "path": "gptcache/embedding/paddlenlp.py",
    "chars": 2343,
    "preview": "import numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_paddlenlp, impor"
  },
  {
    "path": "gptcache/embedding/rwkv.py",
    "chars": 1864,
    "preview": "import numpy as np\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_huggingface\n\nimp"
  },
  {
    "path": "gptcache/embedding/sbert.py",
    "chars": 1537,
    "preview": "import numpy as np\nfrom gptcache.utils import import_sbert\nfrom gptcache.embedding.base import BaseEmbedding\n\nimport_sbe"
  },
  {
    "path": "gptcache/embedding/string.py",
    "chars": 94,
    "preview": "def to_embeddings(data, **_):\n    \"\"\"Nothing to do, return the origin data\"\"\"\n    return data\n"
  },
  {
    "path": "gptcache/embedding/timm.py",
    "chars": 3453,
    "preview": "import numpy as np\n\nfrom gptcache.utils import import_timm, import_torch, import_pillow\nfrom gptcache.embedding.base imp"
  },
  {
    "path": "gptcache/embedding/uform.py",
    "chars": 2477,
    "preview": "from typing import Union, Any\n\nfrom gptcache.embedding.base import BaseEmbedding\nfrom gptcache.utils import import_uform"
  },
  {
    "path": "gptcache/embedding/vit.py",
    "chars": 2390,
    "preview": "from gptcache.utils import import_huggingface, import_torch, import_torchvision\nfrom gptcache.embedding.base import Base"
  },
  {
    "path": "gptcache/manager/__init__.py",
    "chars": 226,
    "preview": "from gptcache.manager.scalar_data import CacheBase\nfrom gptcache.manager.vector_data import VectorBase\nfrom gptcache.man"
  },
  {
    "path": "gptcache/manager/data_manager.py",
    "chars": 13404,
    "preview": "import pickle\nfrom abc import abstractmethod, ABCMeta\nfrom typing import List, Any, Optional, Union\n\nimport cachetools\ni"
  },
  {
    "path": "gptcache/manager/eviction/__init__.py",
    "chars": 1023,
    "preview": "__all__ = [\"EvictionBase\"]\n\nfrom gptcache.utils.lazy_import import LazyImport\n\neviction_manager = LazyImport(\n    \"evict"
  },
  {
    "path": "gptcache/manager/eviction/base.py",
    "chars": 355,
    "preview": "from abc import ABCMeta, abstractmethod\nfrom typing import Any, List\n\n\nclass EvictionBase(metaclass=ABCMeta):\n    \"\"\"\n  "
  },
  {
    "path": "gptcache/manager/eviction/distributed_cache.py",
    "chars": 908,
    "preview": "# pylint: disable=wrong-import-position\nfrom abc import ABC, abstractmethod\nfrom typing import List\n\nfrom gptcache.manag"
  },
  {
    "path": "gptcache/manager/eviction/manager.py",
    "chars": 1525,
    "preview": "# pylint: disable=import-outside-toplevel\nfrom typing import Callable, List, Any\n\nfrom gptcache.utils.error import NotFo"
  },
  {
    "path": "gptcache/manager/eviction/memory_cache.py",
    "chars": 1945,
    "preview": "from typing import Any, Callable, List\n\nimport cachetools\n\nfrom gptcache.manager.eviction.base import EvictionBase\n\n\ndef"
  },
  {
    "path": "gptcache/manager/eviction/redis_eviction.py",
    "chars": 2827,
    "preview": "# pylint: disable=wrong-import-position\nfrom abc import ABC\nfrom typing import List\n\nfrom gptcache.manager.eviction.dist"
  },
  {
    "path": "gptcache/manager/eviction_manager.py",
    "chars": 1493,
    "preview": "class EvictionManager:\n    \"\"\"\n    EvictionManager to manager the eviction policy.\n\n    :param scalar_storage: CacheStor"
  },
  {
    "path": "gptcache/manager/factory.py",
    "chars": 9421,
    "preview": "import os\nfrom pathlib import Path\nfrom typing import Union, Callable\n\nfrom gptcache.manager import CacheBase, VectorBas"
  },
  {
    "path": "gptcache/manager/object_data/__init__.py",
    "chars": 455,
    "preview": "__all__ = [\"ObjectBase\"]\n\nfrom gptcache.utils.lazy_import import LazyImport\n\nobject_manager = LazyImport(\n    \"object_ma"
  },
  {
    "path": "gptcache/manager/object_data/base.py",
    "chars": 431,
    "preview": "from abc import ABC, abstractmethod\nfrom typing import Any, List\n\n\nclass ObjectBase(ABC):\n    \"\"\"\n    Object storage bas"
  },
  {
    "path": "gptcache/manager/object_data/local_storage.py",
    "chars": 1170,
    "preview": "from typing import Any, List\nimport os\nimport uuid\nfrom pathlib import Path\nfrom gptcache.manager.object_data.base impor"
  },
  {
    "path": "gptcache/manager/object_data/manager.py",
    "chars": 1852,
    "preview": "from gptcache.utils.error import NotFoundError\n\n\nclass ObjectBase:\n    \"\"\"\n    ObjectBase to manager the object storage."
  },
  {
    "path": "gptcache/manager/object_data/s3_storage.py",
    "chars": 1810,
    "preview": "from typing import Any, List\nimport uuid\nimport os\n\nfrom gptcache.manager.object_data.base import ObjectBase\n\nfrom gptca"
  },
  {
    "path": "gptcache/manager/scalar_data/__init__.py",
    "chars": 507,
    "preview": "__all__ = [\"CacheBase\"]\n\nfrom gptcache.utils.lazy_import import LazyImport\n\nscalar_manager = LazyImport(\n    \"scalar_man"
  },
  {
    "path": "gptcache/manager/scalar_data/base.py",
    "chars": 3217,
    "preview": "from abc import ABCMeta, abstractmethod\nfrom dataclasses import dataclass\nfrom datetime import datetime\nfrom enum import"
  },
  {
    "path": "gptcache/manager/scalar_data/dynamo_storage.py",
    "chars": 22842,
    "preview": "from functools import reduce\nfrom random import randint, SystemRandom\nfrom datetime import datetime\nfrom typing import L"
  },
  {
    "path": "gptcache/manager/scalar_data/manager.py",
    "chars": 5110,
    "preview": "from gptcache.utils import import_sql_client\nfrom gptcache.utils.error import NotFoundError\n\nSQL_URL = {\n    \"sqlite\": \""
  },
  {
    "path": "gptcache/manager/scalar_data/mongo.py",
    "chars": 9886,
    "preview": "from datetime import datetime\nfrom typing import List, Optional\n\nimport numpy as np\n\nfrom gptcache.manager.scalar_data.b"
  },
  {
    "path": "gptcache/manager/scalar_data/redis_storage.py",
    "chars": 12877,
    "preview": "import datetime\nfrom typing import List, Optional\n\nimport numpy as np\n\nfrom gptcache.manager.scalar_data.base import (\n "
  },
  {
    "path": "gptcache/manager/scalar_data/sql_storage.py",
    "chars": 14909,
    "preview": "from datetime import datetime\nfrom typing import List, Optional, Dict\n\nimport numpy as np\n\nfrom gptcache.manager.scalar_"
  },
  {
    "path": "gptcache/manager/vector_data/__init__.py",
    "chars": 344,
    "preview": "__all__ = [\"VectorBase\"]\n\nfrom gptcache.utils.lazy_import import LazyImport\n\nvector_manager = LazyImport(\n    \"vector_ma"
  },
  {
    "path": "gptcache/manager/vector_data/base.py",
    "chars": 857,
    "preview": "from abc import ABC, abstractmethod\nfrom dataclasses import dataclass\nfrom typing import List, Optional, Union\n\nimport n"
  },
  {
    "path": "gptcache/manager/vector_data/chroma.py",
    "chars": 2856,
    "preview": "from typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorData\nfrom g"
  },
  {
    "path": "gptcache/manager/vector_data/docarray_index.py",
    "chars": 2782,
    "preview": "from typing import List, Optional, Tuple\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, "
  },
  {
    "path": "gptcache/manager/vector_data/faiss.py",
    "chars": 1995,
    "preview": "import os\nfrom typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorD"
  },
  {
    "path": "gptcache/manager/vector_data/hnswlib_store.py",
    "chars": 2680,
    "preview": "import os\nfrom typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorD"
  },
  {
    "path": "gptcache/manager/vector_data/manager.py",
    "chars": 12377,
    "preview": "from gptcache.utils.error import NotFoundError, ParamError\n\nTOP_K = 1\n\nFAISS_INDEX_PATH = \"faiss.index\"\nDIMENSION = 0\n\nM"
  },
  {
    "path": "gptcache/manager/vector_data/milvus.py",
    "chars": 7922,
    "preview": "from typing import List\nfrom uuid import uuid4\nimport numpy as np\n\nfrom gptcache.utils import import_pymilvus\nfrom gptca"
  },
  {
    "path": "gptcache/manager/vector_data/pgvector.py",
    "chars": 5973,
    "preview": "from typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorData\nfrom g"
  },
  {
    "path": "gptcache/manager/vector_data/qdrant.py",
    "chars": 4798,
    "preview": "from typing import List, Optional\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorD"
  },
  {
    "path": "gptcache/manager/vector_data/redis_vectorstore.py",
    "chars": 4626,
    "preview": "from typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorData\nfrom g"
  },
  {
    "path": "gptcache/manager/vector_data/usearch.py",
    "chars": 2824,
    "preview": "import os\nfrom typing import List\n\nimport numpy as np\n\nfrom gptcache.manager.vector_data.base import VectorBase, VectorD"
  },
  {
    "path": "gptcache/manager/vector_data/weaviate.py",
    "chars": 5616,
    "preview": "from typing import List, Optional, Tuple, Union\nimport numpy as np\n\nfrom gptcache.utils import import_weaviate\nfrom gptc"
  },
  {
    "path": "gptcache/processor/__init__.py",
    "chars": 62,
    "preview": "from gptcache.processor.context.context import ContextProcess\n"
  },
  {
    "path": "gptcache/processor/check_hit.py",
    "chars": 1092,
    "preview": "# pylint: disable=unused-argument\ndef check_hit_session(cur_session_id: str, cache_session_ids: list, cache_questions: l"
  },
  {
    "path": "gptcache/processor/context/__init__.py",
    "chars": 1087,
    "preview": "from gptcache.utils.lazy_import import LazyImport\n\nsummarization = LazyImport(\n    \"summarization_context\",\n    globals("
  },
  {
    "path": "gptcache/processor/context/concat_context.py",
    "chars": 1392,
    "preview": "from typing import Any, Dict\n\nfrom gptcache.processor import ContextProcess\n\n\nclass ConcatContextProcess(ContextProcess)"
  },
  {
    "path": "gptcache/processor/context/context.py",
    "chars": 1515,
    "preview": "from abc import ABCMeta, abstractmethod\nfrom typing import Dict, Any\n\n\nclass ContextProcess(metaclass=ABCMeta):\n    \"\"\"C"
  },
  {
    "path": "gptcache/processor/context/selective_context.py",
    "chars": 2155,
    "preview": "from typing import Any, Dict\n\nfrom gptcache.processor import ContextProcess\nfrom gptcache.utils import import_selective_"
  },
  {
    "path": "gptcache/processor/context/summarization_context.py",
    "chars": 3997,
    "preview": "from typing import Dict, Any\n\nimport numpy as np\n\nfrom gptcache.processor import ContextProcess\nfrom gptcache.utils impo"
  },
  {
    "path": "gptcache/processor/post.py",
    "chars": 7027,
    "preview": "import random\nfrom typing import List, Any\n\nimport numpy\n\nfrom gptcache.utils import softmax\n\n\ndef random_one(messages: "
  },
  {
    "path": "gptcache/processor/pre.py",
    "chars": 12055,
    "preview": "import re\nimport string\nfrom typing import Dict, Any\n\n\ndef last_content(data: Dict[str, Any], **_: Dict[str, Any]) -> An"
  },
  {
    "path": "gptcache/report.py",
    "chars": 3389,
    "preview": "class Report:\n    \"\"\"Get GPTCache report including time and counts for different operations.\"\"\"\n\n    def __init__(self):"
  },
  {
    "path": "gptcache/session.py",
    "chars": 2742,
    "preview": "import uuid\nfrom typing import Callable, Optional\n\nfrom gptcache import cache\nfrom gptcache.manager.data_manager import "
  },
  {
    "path": "gptcache/similarity_evaluation/__init__.py",
    "chars": 2744,
    "preview": "from typing import Dict, Any\n\nfrom gptcache.similarity_evaluation.similarity_evaluation import SimilarityEvaluation\n\n__a"
  },
  {
    "path": "gptcache/similarity_evaluation/cohere_rerank.py",
    "chars": 1636,
    "preview": "from typing import Tuple, Dict, Any\n\nfrom gptcache.similarity_evaluation import SimilarityEvaluation\nfrom gptcache.utils"
  },
  {
    "path": "gptcache/similarity_evaluation/distance.py",
    "chars": 2368,
    "preview": "from typing import Tuple, Dict, Any\n\nfrom gptcache.similarity_evaluation import SimilarityEvaluation\n\n\nclass SearchDista"
  },
  {
    "path": "gptcache/similarity_evaluation/exact_match.py",
    "chars": 1521,
    "preview": "from typing import Tuple, Dict, Any\n\nfrom gptcache.similarity_evaluation.similarity_evaluation import SimilarityEvaluati"
  },
  {
    "path": "gptcache/similarity_evaluation/kreciprocal.py",
    "chars": 4361,
    "preview": "import numpy as np\nfrom typing import Dict, Any\n\nfrom gptcache.similarity_evaluation.distance import SearchDistanceEvalu"
  },
  {
    "path": "gptcache/similarity_evaluation/np.py",
    "chars": 3506,
    "preview": "from typing import Dict, Tuple, Any\n\nimport numpy as np\n\nfrom gptcache.similarity_evaluation import SimilarityEvaluation"
  },
  {
    "path": "gptcache/similarity_evaluation/onnx.py",
    "chars": 4745,
    "preview": "from typing import Dict, List, Tuple, Any\n\nimport numpy as np\n\nfrom gptcache.similarity_evaluation import SimilarityEval"
  },
  {
    "path": "gptcache/similarity_evaluation/sbert_crossencoder.py",
    "chars": 2153,
    "preview": "from typing import Dict, Tuple, Any\nfrom gptcache.utils import import_sbert\nfrom gptcache.similarity_evaluation import S"
  },
  {
    "path": "gptcache/similarity_evaluation/sequence_match.py",
    "chars": 3947,
    "preview": "from typing import Tuple, Dict, Any, List\n\nimport numpy as np\n\nfrom gptcache.adapter.api import _get_model\nfrom gptcache"
  },
  {
    "path": "gptcache/similarity_evaluation/similarity_evaluation.py",
    "chars": 1264,
    "preview": "from abc import ABCMeta, abstractmethod\nfrom typing import Tuple, Dict, Any\n\n\nclass SimilarityEvaluation(metaclass=ABCMe"
  },
  {
    "path": "gptcache/similarity_evaluation/time.py",
    "chars": 1978,
    "preview": "from datetime import datetime\nfrom typing import Tuple, Dict, Any\n\nfrom gptcache.adapter.api import _get_eval\nfrom gptca"
  },
  {
    "path": "gptcache/utils/__init__.py",
    "chars": 5176,
    "preview": "__all__ = [\n    \"import_pymilvus\",\n    \"import_milvus_lite\",\n    \"import_sbert\",\n    \"import_cohere\",\n    \"import_fastte"
  },
  {
    "path": "gptcache/utils/cache_func.py",
    "chars": 41,
    "preview": "def cache_all(*_, **__):\n    return True\n"
  },
  {
    "path": "gptcache/utils/dependency_control.py",
    "chars": 756,
    "preview": "import subprocess\n\nfrom gptcache.utils.error import PipInstallError\nfrom gptcache.utils.log import gptcache_log\n\n\ndef pr"
  },
  {
    "path": "gptcache/utils/error.py",
    "chars": 1492,
    "preview": "class CacheError(Exception):\n    \"\"\"GPTCache base error\"\"\"\n\n\nclass NotInitError(CacheError):\n    \"\"\"Raise when the cache"
  },
  {
    "path": "gptcache/utils/lazy_import.py",
    "chars": 708,
    "preview": "import importlib\nfrom types import ModuleType\n\n\nclass LazyImport(ModuleType):\n    \"\"\"\n    Lazily import a module.\n    \"\""
  }
]

// ... and 90 more files (download for full content)

About this extraction

This page contains the full source code of the zilliztech/GPTCache GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 290 files (3.0 MB), approximately 813.5k tokens, and a symbol index with 1038 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!