@ -26,7 +26,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 2 ,
"execution_count": 1 ,
"id": "d3e893bf",
"id": "d3e893bf",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
@ -44,19 +44,24 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 3 ,
"execution_count": 2 ,
"id": "dfdd8bf5",
"id": "dfdd8bf5",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"from unittest.mock import patch\n",
"from unittest.mock import patch\n",
"\n",
"\n",
"from openai.error import RateLimitError"
"import httpx\n",
"from openai import RateLimitError\n",
"\n",
"request = httpx.Request(\"GET\", \"/\")\n",
"response = httpx.Response(200, request=request)\n",
"error = RateLimitError(\"rate limit\", response=response, body=\"\")"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 5,
"execution_count": 3 ,
"id": "e6fdffc1",
"id": "e6fdffc1",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
@ -69,7 +74,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 27 ,
"execution_count": 4 ,
"id": "584461ab",
"id": "584461ab",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
@ -83,7 +88,7 @@
],
],
"source": [
"source": [
"# Let's use just the OpenAI LLm first, to show that we run into an error\n",
"# Let's use just the OpenAI LLm first, to show that we run into an error\n",
"with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError() ):\n",
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error ):\n",
" try:\n",
" try:\n",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except:\n",
@ -106,7 +111,7 @@
],
],
"source": [
"source": [
"# Now let's try with fallbacks to Anthropic\n",
"# Now let's try with fallbacks to Anthropic\n",
"with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError() ):\n",
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error ):\n",
" try:\n",
" try:\n",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
" except:\n",
@ -148,7 +153,7 @@
" ]\n",
" ]\n",
")\n",
")\n",
"chain = prompt | llm\n",
"chain = prompt | llm\n",
"with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError() ):\n",
"with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error ):\n",
" try:\n",
" try:\n",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
" except:\n",
@ -286,7 +291,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.10. 1"
"version": "3.11.5 "
}
}
},
},
"nbformat": 4,
"nbformat": 4,