forked from onlyphantom/llm-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_caching_sqlite.py
More file actions
49 lines (37 loc) · 1.53 KB
/
13_caching_sqlite.py
File metadata and controls
49 lines (37 loc) · 1.53 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import time
from dotenv import load_dotenv
import langchain
from langchain_openai import OpenAI
from langchain_community.callbacks import get_openai_callback
from langchain_text_splitters import CharacterTextSplitter
from langchain_core.documents import Document
from langchain_community.cache import SQLiteCache
load_dotenv()
# Configure cache for the specific LLM
llm = OpenAI(model="gpt-3.5-turbo-instruct", cache=SQLiteCache(database_path=".langchain.db"))
no_cache_llm = OpenAI(model="gpt-3.5-turbo-instruct", cache=None)
text_splitter = CharacterTextSplitter()
with open("news/summary.txt") as f:
news = f.read()
texts = text_splitter.split_text(news)
print(texts)
docs = [Document(page_content=t) for t in texts[:3]]
# Create a simple summarization chain using LCEL
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
summarize_prompt = ChatPromptTemplate.from_template("Summarize the following text:\n\n{text}")
chain = summarize_prompt | llm | StrOutputParser()
with get_openai_callback() as cb:
start = time.time()
result = chain.invoke({"text": "\n".join([doc.page_content for doc in docs])})
end = time.time()
print("--- result1")
print(result)
print(str(cb) + f" ({end - start:.2f} seconds)")
with get_openai_callback() as cb2:
start = time.time()
result = chain.invoke({"text": "\n".join([doc.page_content for doc in docs])})
end = time.time()
print("--- result2")
print(result)
print(str(cb2) + f" ({end - start:.2f} seconds)")