forked from onlyphantom/llm-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_caching_memory.py
More file actions
31 lines (26 loc) · 874 Bytes
/
13_caching_memory.py
File metadata and controls
31 lines (26 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import time
from dotenv import load_dotenv
import langchain
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callback
from langchain.cache import InMemoryCache
load_dotenv()
# to make caching obvious, we use a slow model
llm = OpenAI(model_name="text-davinci-002")
langchain.llm_cache = InMemoryCache()
with get_openai_callback() as cb:
start = time.time()
result = llm("What doesn't fall far from the tree?")
print(result)
end = time.time()
print("--- cb")
print(str(cb) + f" ({end - start:.2f} seconds)")
with get_openai_callback() as cb2:
start = time.time()
result2 = llm("What doesn't fall far from the tree?")
result3 = llm("What doesn't fall far from the tree?")
end = time.time()
print(result2)
print(result3)
print("--- cb2")
print(str(cb2) + f" ({end - start:.2f} seconds)")