Skip to content

Commit

Permalink
docs: clean Jupyter notebooks and test (ruff/mypy)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Oct 19, 2024
1 parent 8ee6f72 commit 25079b0
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 504 deletions.
67 changes: 29 additions & 38 deletions libs/redis/docs/cache.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
}
],
"source": [
"# ruff: noqa: T201\n",
"import os\n",
"\n",
"# Use the environment variable if set, otherwise default to localhost\n",
Expand All @@ -82,23 +83,14 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_redis import RedisCache, RedisSemanticCache\n",
"from langchain_openai import OpenAIEmbeddings, OpenAI\n",
"import time\n",
"from getpass import getpass\n",
"\n",
"from langchain.globals import set_llm_cache\n",
"from langchain.schema import Generation\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import langchain_core\n",
"import langchain_openai\n",
"import openai\n",
"import redis"
"from langchain_openai import OpenAI, OpenAIEmbeddings\n",
"\n",
"from langchain_redis import RedisCache, RedisSemanticCache"
]
},
{
Expand All @@ -110,7 +102,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -121,7 +113,7 @@
]
},
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your OpenAI API key: ········\n"
Expand All @@ -143,8 +135,6 @@
}
],
"source": [
"from getpass import getpass\n",
"\n",
"# Check if OPENAI_API_KEY is already set in the environment\n",
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
Expand All @@ -168,7 +158,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -179,15 +169,15 @@
"Result: \n",
"\n",
"Caching is the process of storing frequently accessed data in a temporary storage location for faster retrieval. This helps to reduce the time and resources needed to access the data from its original source. Caching is commonly used in computer systems, web browsers, and databases to improve performance and efficiency.\n",
"Time: 1.57 seconds\n",
"Time: 1.60 seconds\n",
"\n",
"Second call (cached):\n",
"Result: \n",
"\n",
"Caching is the process of storing frequently accessed data in a temporary storage location for faster retrieval. This helps to reduce the time and resources needed to access the data from its original source. Caching is commonly used in computer systems, web browsers, and databases to improve performance and efficiency.\n",
"Time: 0.05 seconds\n",
"\n",
"Speed improvement: 34.59x faster\n",
"Speed improvement: 32.54x faster\n",
"Cache cleared\n"
]
}
Expand Down Expand Up @@ -236,28 +226,29 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22:09:26 redisvl.index.index INFO Index already exists, not overwriting.\n",
"Original query:\n",
"Prompt: What is the capital of France?\n",
"Result: \n",
"\n",
"The capital of France is Paris.\n",
"Time: 1.13 seconds\n",
"Time: 2.01 seconds\n",
"\n",
"Similar query:\n",
"Prompt: Can you tell me the capital city of France?\n",
"Result: \n",
"\n",
"The capital of France is Paris.\n",
"Time: 0.25 seconds\n",
"Time: 0.35 seconds\n",
"\n",
"Speed improvement: 4.52x faster\n",
"Speed improvement: 5.80x faster\n",
"Semantic cache cleared\n"
]
}
Expand All @@ -284,16 +275,14 @@
"# Original query\n",
"original_prompt = \"What is the capital of France?\"\n",
"result1, time1 = test_semantic_cache(original_prompt)\n",
"print(\n",
" f\"Original query:\\nPrompt: {original_prompt}\\nResult: {result1}\\nTime: {time1:.2f} seconds\\n\"\n",
")\n",
"print(f\"Original query:\\nPrompt: {original_prompt}\\n\")\n",
"print(f\"Result: {result1}\\nTime: {time1:.2f} seconds\\n\")\n",
"\n",
"# Semantically similar query\n",
"similar_prompt = \"Can you tell me the capital city of France?\"\n",
"result2, time2 = test_semantic_cache(similar_prompt)\n",
"print(\n",
" f\"Similar query:\\nPrompt: {similar_prompt}\\nResult: {result2}\\nTime: {time2:.2f} seconds\\n\"\n",
")\n",
"print(f\"Similar query:\\nPrompt: {similar_prompt}\\n\")\n",
"print(f\"Result: {result2}\\nTime: {time2:.2f} seconds\\n\")\n",
"\n",
"print(f\"Speed improvement: {time1 / time2:.2f}x faster\")\n",
"\n",
Expand All @@ -318,7 +307,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -348,9 +337,10 @@
"\n",
"# Try to retrieve the expired entry\n",
"expired_result = ttl_cache.lookup(\"test_prompt\", \"test_llm\")\n",
"print(\n",
" f\"Result after TTL: {expired_result[0].text if expired_result else 'Not found (expired)'}\"\n",
")"
"if expired_result:\n",
" print(f\"Result after TTL: {expired_result[0].text}\")\n",
"else:\n",
" print(\"Not found (expired)\")"
]
},
{
Expand All @@ -362,13 +352,14 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22:09:35 redisvl.index.index INFO Index already exists, not overwriting.\n",
"Original result: \n",
"\n",
"The largest planet in our solar system is Jupiter.\n",
Expand Down Expand Up @@ -430,7 +421,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.10"
}
},
"nbformat": 4,
Expand Down
25 changes: 8 additions & 17 deletions libs/redis/docs/chat_history.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
}
],
"source": [
"# ruff: noqa: T201\n",
"import os\n",
"\n",
"# Use the environment variable if set, otherwise default to localhost\n",
Expand All @@ -82,12 +83,12 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_redis import RedisChatMessageHistory\n",
"from langchain_core.chat_history import BaseChatMessageHistory\n",
"from langchain_core.messages import HumanMessage, AIMessage\n",
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
"from langchain_openai import ChatOpenAI"
"from langchain_openai import ChatOpenAI\n",
"\n",
"from langchain_redis import RedisChatMessageHistory"
]
},
{
Expand Down Expand Up @@ -153,7 +154,7 @@
]
},
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your OpenAI API key: ········\n"
Expand Down Expand Up @@ -189,21 +190,11 @@
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in RootListenersTracer.on_chain_end callback: ValueError()\n",
"Error in callback coroutine: ValueError()\n",
"Error in RootListenersTracer.on_chain_end callback: ValueError()\n",
"Error in callback coroutine: ValueError()\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"AI Response 1: Hello, Alice! It's nice to meet you. How can I assist you today?\n",
"AI Response 1: Hello Alice! How can I assist you today?\n",
"AI Response 2: Your name is Alice.\n"
]
}
Expand Down Expand Up @@ -271,7 +262,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Custom History: [HumanMessage(content='This is a message with custom configuration.')]\n"
"Custom History: [HumanMessage(content='This is a message with custom configuration.', additional_kwargs={}, response_metadata={})]\n"
]
}
],
Expand Down Expand Up @@ -377,7 +368,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.10"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 25079b0

Please sign in to comment.