mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 01:45:27 +03:00
Created a generic SQLAlchemyCache class to plug any database supported by SQAlchemy. (I am using Postgres). I also based the class SQLiteCache class on this class SQLAlchemyCache. As a side note, I'm questioning the need for two distinct class LLMCache, FullLLMCache. Shouldn't we merge both ?
10 KiB
10 KiB
In [1]:
from langchain.llms import OpenAIIn [2]:
llm = OpenAI(model_name="text-ada-001", n=2, best_of=2)In [3]:
llm("Tell me a joke")Out [3]:
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'
In [4]:
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15)In [5]:
len(llm_result.generations)Out [5]:
30
In [6]:
llm_result.generations[0]Out [6]:
[Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'), Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!')]
In [7]:
llm_result.generations[-1]Out [7]:
[Generation(text="\n\nA rose by the side of the road\n\nIs all I need to find my way\n\nTo the place I've been searching for\n\nAnd my heart is singing with joy\n\nWhen I look at this rose\n\nIt reminds me of the love I've found\n\nAnd I know that wherever I go\n\nI'll always find my rose by the side of the road."), Generation(text="\n\nWhen I was younger\nI thought that love\nI was something like a fairytale\nI would find my prince and they would be my people\nI was naïve\nI thought that\n\nLove was a something that happened\nWhen I was younger\nI was it for my fairytale prince\nNow I realize\nThat love is something that waits\nFor when my prince comes\nAnd when I am ready to be his wife\nI'll tell you a poem\n\nWhen I was younger\nI thought that love\nI was something like a fairytale\nI would find my prince and they would be my people\nI was naïve\nI thought that\n\nLove was a something that happened\nAnd I would be happy\nWhen my prince came\nAnd I was ready to be his wife")]
In [8]:
# Provider specific info
llm_result.llm_outputOut [8]:
{'token_usage': {'completion_tokens': 3722,
'prompt_tokens': 120,
'total_tokens': 3842}}In [9]:
llm.get_num_tokens("what a joke")Out [9]:
3
In [3]:
import langchain
from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()In [4]:
# To make the caching really obvious, lets use a slower model.
llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)In [5]:
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")Out [5]:
CPU times: user 31.2 ms, sys: 11.8 ms, total: 43.1 ms Wall time: 1.75 s
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'
In [6]:
%%time
# The second time it is, so it goes faster
llm("Tell me a joke")Out [6]:
CPU times: user 51 µs, sys: 1 µs, total: 52 µs Wall time: 67.2 µs
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'
In [7]:
# We can do the same thing with a SQLite cache
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")In [8]:
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")Out [8]:
CPU times: user 26.6 ms, sys: 11.2 ms, total: 37.7 ms Wall time: 1.89 s
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
In [9]:
%%time
# The second time it is, so it goes faster
llm("Tell me a joke")Out [9]:
CPU times: user 2.69 ms, sys: 1.57 ms, total: 4.27 ms Wall time: 2.73 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
In [ ]:
# You can use SQLAlchemyCache to cache with any SQL database supported by SQLAlchemy.
from langchain.cache import SQLAlchemyCache
from sqlalchemy import create_engine
engine = create_engine("postgresql://postgres:postgres@localhost:5432/postgres")
langchain.llm_cache = SQLAlchemyCache(engine)