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)")

langchain.cache.InMemoryCache langchain.llms.OpenAI langchain.callbacks.get_openai_callback
[((189, 202), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (200, 202), False, 'from dotenv import load_dotenv\n'), ((257, 294), 'langchain.llms.OpenAI', 'OpenAI', ([], {'model_name': '"""text-davinci-002"""'}), "(model_name='text-davinci-002')\n", (263, 294), False, 'from langchain.llms import OpenAI\n'), ((318, 333), 'langchain.cache.InMemoryCache', 'InMemoryCache', ([], {}), '()\n', (331, 333), False, 'from langchain.cache import InMemoryCache\n'), ((340, 361), 'langchain.callbacks.get_openai_callback', 'get_openai_callback', ([], {}), '()\n', (359, 361), False, 'from langchain.callbacks import get_openai_callback\n'), ((381, 392), 'time.time', 'time.time', ([], {}), '()\n', (390, 392), False, 'import time\n'), ((478, 489), 'time.time', 'time.time', ([], {}), '()\n', (487, 489), False, 'import time\n'), ((569, 590), 'langchain.callbacks.get_openai_callback', 'get_openai_callback', ([], {}), '()\n', (588, 590), False, 'from langchain.callbacks import get_openai_callback\n'), ((611, 622), 'time.time', 'time.time', ([], {}), '()\n', (620, 622), False, 'import time\n'), ((749, 760), 'time.time', 'time.time', ([], {}), '()\n', (758, 760), False, 'import time\n')]