Update fallback cases (#14164)

### Description

The `RateLimitError` initialization method has changed after openai v1,
and the usage of `patch` needs to be changed.

### Twitter handle

[lin_bob57617](https://twitter.com/lin_bob57617)
pull/14200/head
Bob Lin 7 months ago committed by GitHub
parent 62505043be
commit 1ea48a31da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "d3e893bf",
"metadata": {},
"outputs": [],
@ -44,19 +44,24 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "dfdd8bf5",
"metadata": {},
"outputs": [],
"source": [
"from unittest.mock import patch\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",
"execution_count": 5,
"execution_count": 3,
"id": "e6fdffc1",
"metadata": {},
"outputs": [],
@ -69,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 4,
"id": "584461ab",
"metadata": {},
"outputs": [
@ -83,7 +88,7 @@
],
"source": [
"# 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",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
@ -106,7 +111,7 @@
],
"source": [
"# 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",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
@ -148,7 +153,7 @@
" ]\n",
")\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",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
@ -286,7 +291,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.11.5"
}
},
"nbformat": 4,

@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 1,
"id": "d3e893bf",
"metadata": {},
"outputs": [],
@ -46,19 +46,24 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 2,
"id": "dfdd8bf5",
"metadata": {},
"outputs": [],
"source": [
"from unittest.mock import patch\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",
"execution_count": 24,
"execution_count": 3,
"id": "e6fdffc1",
"metadata": {},
"outputs": [],
@ -71,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 4,
"id": "584461ab",
"metadata": {},
"outputs": [
@ -85,7 +90,7 @@
],
"source": [
"# 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",
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
@ -108,7 +113,7 @@
],
"source": [
"# 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",
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
" except:\n",
@ -150,7 +155,7 @@
" ]\n",
")\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",
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
" except:\n",
@ -431,7 +436,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.5"
}
},
"nbformat": 4,

Loading…
Cancel
Save