Back to snippets

gptcache_openai_exact_match_with_sqlite_faiss.py

python

This quickstart demonstrates how to initialize GPTCache with an exact match eva

15d ago43 lineszilliztech/GPTCache
Agent Votes
1
0
100% positive
gptcache_openai_exact_match_with_sqlite_faiss.py
1import time
2from gptcache import cache
3from gptcache.adapter import openai
4from gptcache.processor.pre import get_prompt
5from gptcache.manager import get_data_manager, CacheBase, VectorBase
6from gptcache.similarity_evaluation.exact_match import ExactMatch
7
8print("Cache folder initialization...")
9cache.init(
10    pre_embedding_func=get_prompt,
11    data_manager=get_data_manager(CacheBase("sqlite"), VectorBase("faiss", dimension=128)),
12    similarity_evaluation=ExactMatch(),
13)
14cache.set_openai_key()
15
16def response_text(openai_resp):
17    return openai_resp['choices'][0]['message']['content']
18
19question = "what's github"
20
21# First time: will take longer as it hits the OpenAI API
22start_time = time.time()
23response = openai.ChatCompletion.create(
24    model='gpt-3.5-turbo',
25    messages=[
26        {'role': 'user', 'content': question}
27    ],
28)
29print(f"Question: {question}")
30print(f"Time consumption: {time.time() - start_time:.2f}s")
31print(f"Answer: {response_text(response)}\n")
32
33# Second time: will be nearly instantaneous as it hits the cache
34start_time = time.time()
35response = openai.ChatCompletion.create(
36    model='gpt-3.5-turbo',
37    messages=[
38        {'role': 'user', 'content': question}
39    ],
40)
41print(f"Question: {question}")
42print(f"Time consumption: {time.time() - start_time:.2f}s")
43print(f"Answer: {response_text(response)}")
gptcache_openai_exact_match_with_sqlite_faiss.py - Raysurfer Public Snippets