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
32 lines (27 loc) · 958 Bytes
/
13_caching_memory.py
File metadata and controls
32 lines (27 loc) · 958 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
32
import time
from dotenv import load_dotenv
import langchain
from langchain_openai import OpenAI
from langchain_community.callbacks import get_openai_callback
from langchain_core.caches import InMemoryCache
from langchain_core.globals import set_llm_cache
load_dotenv()
# to make caching obvious, we use a slower model
llm = OpenAI(model="gpt-3.5-turbo-instruct")
set_llm_cache(InMemoryCache())
with get_openai_callback() as cb:
start = time.time()
result = llm.invoke("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.invoke("What doesn't fall far from the tree?")
result3 = llm.invoke("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)")