Cos'è GPTCache?
GPTCache è una libreria open-source progettata per migliorare l'efficienza e la velocità delle applicazioni basate su GPT implementando una cache per memorizzare le risposte generate dai modelli linguistici. GPTCache consente agli utenti di personalizzare la cache in base alle proprie esigenze, con opzioni per funzioni di embedding, funzioni di valutazione della similarità, posizione di archiviazione ed eliminazione. Inoltre, GPTCache supporta attualmente l'interfaccia OpenAI ChatGPT e l'interfaccia Langchain.

Costruito su una comunità forte e in crescita.
7,881+
Stelle su GitHub
Perché usare GPTCache?
Prestazioni migliorate
Memorizzare le risposte degli LLM in una cache può ridurre significativamente il tempo necessario per recuperare la risposta, specialmente quando è stata già richiesta in precedenza ed è già presente nella cache. Memorizzare le risposte in una cache può migliorare le prestazioni complessive della tua applicazione.
Costi ridotti
La maggior parte dei servizi LLM addebita costi basati su una combinazione del numero di richieste e del conteggio dei token. Memorizzare in cache le risposte degli LLM può ridurre il numero di chiamate API effettuate al servizio, tradursi in risparmi. La memorizzazione nella cache è particolarmente rilevante quando si gestiscono livelli elevati di traffico, dove i costi delle chiamate API possono essere sostanziali.
Migliore scalabilità
Memorizzare in cache le risposte degli LLM può migliorare la scalabilità della tua applicazione riducendo il carico sul servizio LLM. La cache aiuta a evitare colli di bottiglia e garantisce che l'applicazione possa gestire un numero crescente di richieste.
Costi di sviluppo minimizzati
Una cache semantica può essere uno strumento prezioso per ridurre i costi durante la fase di sviluppo di un'app LLM. Un'applicazione LLM richiede una connessione alle API LLM anche durante lo sviluppo, il che potrebbe diventare costoso. GPTCache offre la stessa interfaccia delle API LLM e può memorizzare dati generati o simulati dagli LLM. GPTCache aiuta a verificare le funzionalità della tua applicazione senza connettersi alle API LLM o alla rete.
Latenza di rete ridotta
Una cache semantica posizionata più vicino alla macchina dell'utente riduce il tempo necessario per recuperare i dati dal servizio LLM. Riducendo la latenza di rete, puoi migliorare l'esperienza complessiva dell'utente.
Disponibilità migliorata
I servizi LLM applicano spesso limiti di frequenza, che sono vincoli che le API pongono sul numero di volte in cui un utente o un client può accedere al server entro un determinato periodo di tempo. Raggiungere un limite di frequenza significa che ulteriori richieste verranno bloccate fino a quando non sarà trascorso un certo periodo, causando un'interruzione del servizio. Con GPTCache, puoi scalare rapidamente per gestire un volume crescente di query, garantendo prestazioni costanti con l'espansione della base utenti della tua applicazione.
In sintesi, sviluppare una cache semantica per memorizzare le risposte degli LLM può offrire vari benefici, tra cui prestazioni migliorate, costi ridotti, migliore scalabilità, personalizzazione e ridotta latenza di rete.
Come funziona GPTCache
GPTCache sfrutta la località dei dati nei servizi online memorizzando i dati a cui si accede più frequentemente, riducendo il tempo di recupero e alleggerendo il carico sul server backend. A differenza dei sistemi di cache tradizionali, GPTCache utilizza una cache semantica, identificando e memorizzando query simili o correlate per migliorare i tassi di successo della cache.
Utilizzando algoritmi di embedding, GPTCache converte le query in embedding e utilizza un archivio vettoriale per la ricerca di similarità, consentendo il recupero di query correlate dalla cache. Il design modulare di GPTCache consente agli utenti di personalizzare la propria cache semantica con varie implementazioni per ciascun modulo.
Sebbene la cache semantica possa produrre falsi positivi e negativi, GPTCache offre tre metriche di prestazione per aiutare gli sviluppatori a ottimizzare i propri sistemi di caching.
Questo processo consente a GPTCache di identificare e recuperare query simili o correlate dall'archivio della cache, come illustrato nel diagramma seguente.
Customize semantic cache
GPTCache was built with a modular design to make it easy for users to customize their semantic cache. Each module has options for the users to choose from to fit their needs.
Pre-processor Pre-processor manages, analyzes, and formats the queries sent to LLMs, including removing redundant information from inputs, compressing input information, cutting long texts, and performing other related tasks.
LLM Adapter The LLM Adapter integrates with different LLM models and is standardized on the OpenAI API, unifying their APIs and request protocols. The LLM Adapter allows for easier experimentation and testing with various LLM models, as you can switch between them without having to rewrite your code or learn a new API. Support is available for:
- OpenAI ChatGPT API
- langchain
- Minigpt4
- Llamacpp
- Roadmap — Hugging Face Hub, Bard and Anthropic
Embedding Generator The Embedding Generator generates embeddings with the model of your choice from the request queue to execute a similarity search. Models that are supported include OpenAI embedding API. ONNX with the GPTCache/paraphrase-albert-onnx model, Hugging Face embedding API, Cohere embedding API, fastText embedding API, and the SentenceTransformers embedding API and Timm models for image embeddings.
Cache Store Cache Store 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. GPTCache supports SQLite, PostgreSQL, MySQL, MariaDB, SQL Server, and Oracle. Supporting popular databases means that users can choose the database that best fits their needs, depending on performance, scalability, and cost.
Vector Store options GPTCache supports a Vector Store module, which helps to find the K most similar requests based on the extracted embeddings from the input request. This functionality can help assess the similarity between requests. In addition, GPTCache provides a user-friendly interface that supports various vector stores, including Milvus, Zilliz Cloud, Milvus Lite, Hnswlib, PGVector, Chroma, DocArray and FAISS. These options give users a range of choices for vector stores, which can affect the efficiency and accuracy of the similarity search functionality in GPTCache. GPTCache aims to provide flexibility and cater to a broader range of use cases by supporting multiple vector stores.
Eviction Policy Management Cache Manager in GPTCache controls the operations of both the Cache Store and Vector Store modules. When the cache becomes full, a replacement policy determines which data to evict to make room for new data. GPTCache currently supports two basic options: - LRU (Least Recently Used) eviction policy - FIFO (First In, First Out) eviction policy These are both standard eviction policies used in caching systems.
Similarity Evaluator The Similarity Evaluator module in GPTCache collects data from Cache Storage and Vector Store. It uses various strategies to determine the similarity between the input request and the requests from the Vector Store. The similarity determines whether a request matches the cache. GPTCache provides a standardized interface for integrating various similarity strategies and a collection of implementations. These different similarity strategies allow GPTCache to provide flexibility in determining cache matches based on other use cases and needs.
Post-Processor Post-processor prepares the final response to return to the user when the cache is hit. If the answer is not in the cache, the LLM Adapter requests responses from LLM and writes them back to the Cache Manager.
