MCPcopy Index your code
hub / github.com/zilliztech/GPTCache

github.com/zilliztech/GPTCache @0.1.44

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.1.44 ↗ · + Follow
1,132 symbols 5,430 edges 217 files 257 documented · 23% updated 12mo ago0.1.44 · 2024-08-01★ 8,08575 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

GPTCache : A Library for Creating Semantic Cache for LLM Queries

Slash Your LLM API Costs by 10x 💰, Boost Speed by 100x ⚡

Release pip download Codecov License Twitter Discord

🎉 GPTCache has been fully integrated with 🦜️🔗LangChain ! Here are detailed usage instructions.

🐳 The GPTCache server docker image 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 and release note.

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

# 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. And more examples you can refer to the Bootcamp

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.

Click to SHOW example code

OpenAI API original usage

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.

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.

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 to learn about how temperature affects output.

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"])

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

from gptcache import cache
from gptcache.adapter import openai

cache.init()
cache.set_openai_key()

More Docs:

🎓 Bootcamp

😎 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. 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, 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 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

Core symbols most depended-on inside this repo

create
called by 100
gptcache/adapter/openai.py
init
called by 91
gptcache/core.py
get
called by 86
gptcache/manager/eviction/base.py
get
called by 77
gptcache/client.py
_check_library
called by 51
gptcache/utils/__init__.py
get_data_manager
called by 49
gptcache/manager/factory.py
manager_factory
called by 39
gptcache/manager/factory.py
set_openai_key
called by 25
gptcache/core.py

Shape

Method 607
Function 367
Class 152
Route 6

Languages

Python100%

Modules by API surface

gptcache/utils/__init__.py49 symbols
gptcache/manager/data_manager.py41 symbols
gptcache/adapter/openai.py38 symbols
gptcache/manager/scalar_data/redis_storage.py36 symbols
gptcache/manager/scalar_data/dynamo_storage.py28 symbols
gptcache/manager/scalar_data/mongo.py27 symbols
gptcache/adapter/langchain_models.py24 symbols
gptcache/manager/scalar_data/sql_storage.py23 symbols
gptcache/manager/scalar_data/base.py22 symbols
gptcache/report.py21 symbols
gptcache/manager/vector_data/pgvector.py19 symbols
gptcache/processor/pre.py17 symbols

Dependencies from manifests, versioned

anyio3.6.2 · 1×
coverage7.2.3 · 1×
grpcio1.53.0 · 1×
loguru0.5.3 · 1×
milvus2.2.8 · 1×
protobuf3.20.0 · 1×
pymilvus2.2.8 · 1×
pytest7.2.0 · 1×
pytest-assume2.4.3 · 1×
pytest-cov4.1.0 · 1×
pytest-html3.1.1 · 1×
pytest-level0.1.1 · 1×

Datastores touched

(mysql)Database · 1 repos
mysqlDatabase · 1 repos
postgresDatabase · 1 repos
(mongodb)Database · 1 repos

For agents

$ claude mcp add GPTCache \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact