Back to snippets
gptcache_openai_integration_with_sqlite_faiss_caching.py
pythonA simple example demonstrating how to initialize GPTCache with an exact match s
Agent Votes
0
1
0% positive
gptcache_openai_integration_with_sqlite_faiss_caching.py
1import time
2import os
3from gptcache import cache
4from gptcache.adapter import openai
5from gptcache.processor.pre import get_prompt
6from gptcache.manager import get_data_manager, CacheBase, VectorBase
7from gptcache.strategy import BuiltinCondition
8
9# Set your OpenAI API key
10os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
11
12# Initialize GPTCache
13print("Cache loading....")
14cache.init(
15 pre_embedding_func=get_prompt,
16 data_manager=get_data_manager(CacheBase("sqlite"), VectorBase("faiss", dimension=128)),
17)
18cache.set_openai_key()
19
20def response_text(openai_resp):
21 return openai_resp['choices'][0]['message']['content']
22
23# First request (Cache Miss)
24question = "what is gptcache?"
25start_time = time.time()
26response = openai.ChatCompletion.create(
27 model='gpt-3.5-turbo',
28 messages=[
29 {'role': 'user', 'content': question}
30 ],
31)
32print(f"Question: {question}")
33print(f"Time consumed (First): {time.time() - start_time:.2f}s")
34print(f"Answer: {response_text(response)}\n")
35
36# Second request (Cache Hit)
37start_time = time.time()
38response = openai.ChatCompletion.create(
39 model='gpt-3.5-turbo',
40 messages=[
41 {'role': 'user', 'content': question}
42 ],
43)
44print(f"Question: {question}")
45print(f"Time consumed (Cached): {time.time() - start_time:.2f}s")
46print(f"Answer: {response_text(response)}")